1 2 Previous Next 16 Replies Latest reply on Apr 2, 2011 4:54 AM by Anish Nath

    Can someone look at this application? help understanding why

    Rick Reumann Newbie

      I'm trying to use the @EJB annotation on a stateless session bean in a servlet and having troubles getting it to work. I've read some other posts on the issue and supposedly it 'should' work, so I'd love some help trying to figure out what I'm doing wrong.

      I posted the simple source code here:

      http://dl-client.getdropbox.com/u/86998/learntechnology-maven-jee.zip

      If you run "mvn install" it will build a user-adminstration.ear in the lt-ear/target directory. When deployed to JBoss .. try to access the URL:
      http://localhost:8080/sample/hello

      The Stateless session bean never gets set. (You end up with a NPE.)

      I'll post some of the relevant code. ..

      //lt-ejb-jar:

      @Local
      public interface UserService {
       public String helloWorld(String s);
      }
      


      @Stateless
      public class UserServiceBean implements UserService {
      
       public String helloWorld(String s) {
       return "Hello World: "+s;
       }
      }
      


      //lt-web.war...
      public class SayHelloServlet extends HttpServlet {
       private static final long serialVersionUID = 1L;
      
       //not working: @EJB(mappedName="user-administration/UserServiceBean/local")
       //works: private UserService userService = (UserService) ServiceLocator.getEJB("user-administration/UserServiceBean/local");
      
       //not working:
       @EJB
       private UserService userService;
      
       public void init() throws ServletException {
       super.init();
       }
      
       protected void doGet(HttpServletRequest req, HttpServletResponse resp)
       throws ServletException, IOException {
       doPost(req, resp);
       }
      
       protected void doPost(HttpServletRequest req, HttpServletResponse response)
       throws ServletException, IOException {
       System.out.println("userService = " + userService);
       response.setContentType("text/plain");
       response.getOutputStream().println("Servlet Test: ");
       response.getOutputStream().println(userService.helloWorld("My test"));
       }
      }
      


      web.xml
      <?xml version="1.0" encoding="UTF-8"?>
      
      <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
       http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
       version="2.4">
      
       <servlet>
       <servlet-name>SayHelloServlet</servlet-name>
       <servlet-class>
       net.learntechnology.web.SayHelloServlet
       </servlet-class>
       <load-on-startup>0</load-on-startup>
       </servlet>
      
       <servlet-mapping>
       <servlet-name>SayHelloServlet</servlet-name>
       <url-pattern>/hello</url-pattern>
       </servlet-mapping>
      
      </web-app>
      


      Thanks for any help with this.

        • 1. Re: Can someone look at this application? help understanding
          jaikiran pai Master

          Which version of JBoss and Java do you use? Please post the entire exception stacktrace and the console logs when you deploy your application with the following annotation in the servlet:

          @EJB(mappedName="user-administration/UserServiceBean/local")
          private UserService userService;
          





          • 2. Re: Can someone look at this application? help understanding
            Rick Reumann Newbie

             

            "jaikiran" wrote:
            Which version of JBoss and Java do you use? Please post the entire exception stacktrace and the console logs when you deploy your application with the following annotation in the servlet:

            @EJB(mappedName="user-administration/UserServiceBean/local")
            private UserService userService;
            



            That's the odd thing is that there really aren't any Exceptions other the null pointer that occurs because "userService" is never initialized. It's as if the annotation is being totally being ignored in the servlet, yet for the ejb jar obviously the annotations are picked up. I'm not convinced something isn't wrong with my set up though. Maybe someone could try the actual ear in their deployment? I put it out here.. http://dl-client.getdropbox.com/u/86998/user-administration.ear (after it deploys try http://localhost:8080/lt-web/hello )

            Thanks for any help. I do appreciate it.

            here is some server.log info, but not sure how helpful it will be...


            JBoss: 5.0.0.CR2 (build: SVNTag=JBoss_5_0_0_CR2 date=200809171046)
            java -version:

            java version "1.5.0_16"
            Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_16-b06-284)
            Java HotSpot(TM) Client VM (build 1.5.0_16-133, mixed mode, sharing)


            JBoss Bootstrap Environment
            
             JBOSS_HOME: /Users/rick/java/jboss
            
             JAVA: /Users/rick/java/java/bin/java
            
             JAVA_OPTS: -Dprogram.name=run.sh -Xms128m -Xmx512m -XX:MaxPermSize=256m -Dorg.jboss.resolver.warning=true -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000
            
             CLASSPATH: /Users/rick/java/jboss/bin/run.jar
            
            =========================================================================
            
            10:03:55,325 INFO [ServerImpl] Starting JBoss (Microcontainer)...
            10:03:55,326 INFO [ServerImpl] Release ID: JBoss [Morpheus] 5.0.0.CR2 (build: SVNTag=JBoss_5_0_0_CR2 date=200809171046)
            10:03:55,332 INFO [ServerImpl] Home Dir: /Users/rick/java/jboss-5.0.0.CR2
            10:03:55,332 INFO [ServerImpl] Home URL: file:/Users/rick/java/jboss-5.0.0.CR2/
            10:03:55,332 INFO [ServerImpl] Library URL: file:/Users/rick/java/jboss-5.0.0.CR2/lib/
            10:03:55,333 INFO [ServerImpl] Patch URL: null
            10:03:55,333 INFO [ServerImpl] Server Name: default
            10:03:55,333 INFO [ServerImpl] Server Home Dir: /Users/rick/java/jboss-5.0.0.CR2/server/default
            10:03:55,333 INFO [ServerImpl] Server Home URL: file:/Users/rick/java/jboss-5.0.0.CR2/server/default/
            10:03:55,333 INFO [ServerImpl] Server Data Dir: /Users/rick/java/jboss-5.0.0.CR2/server/default/data
            10:03:55,333 INFO [ServerImpl] Server Temp Dir: /Users/rick/java/jboss-5.0.0.CR2/server/default/tmp
            10:03:55,334 INFO [ServerImpl] Server Config URL: file:/Users/rick/java/jboss-5.0.0.CR2/server/default/conf/
            10:03:55,334 INFO [ServerImpl] Server Library URL: file:/Users/rick/java/jboss-5.0.0.CR2/server/default/lib/
            10:03:55,334 INFO [ServerImpl] Root Deployment Filename: jboss-service.xml
            10:03:55,789 INFO [ServerImpl] Starting Microcontainer, bootstrapURL=file:/Users/rick/java/jboss-5.0.0.CR2/server/default/conf/bootstrap.xml
            10:03:56,430 INFO [CopyMechanism] VFS temp dir: /Users/rick/java/jboss-5.0.0.CR2/server/default/tmp
            10:03:56,431 INFO [ZipEntryContext] VFS force nested jars copy-mode is enabled.
            10:03:57,418 INFO [ServerInfo] Java version: 1.5.0_16,Apple Inc.
            10:03:57,419 INFO [ServerInfo] Java VM: Java HotSpot(TM) Client VM 1.5.0_16-133,Apple Inc.
            10:03:57,419 INFO [ServerInfo] OS-System: Mac OS X 10.5.5,i386
            10:03:57,443 INFO [JMXKernel] Legacy JMX core initialized
            
            //THE ERROR when going to the URL
            
            2008-10-01 10:11:10,830 DEBUG [com.arjuna.ats.arjuna.logging.arjLogger] (Thread-12) Periodic recovery - second pass <Wed, 1 Oct 2008 10:11:10>
            2008-10-01 10:11:10,830 DEBUG [com.arjuna.ats.arjuna.logging.arjLogger] (Thread-12) AtomicActionRecoveryModule: Second pass
            2008-10-01 10:11:10,830 DEBUG [com.arjuna.ats.txoj.logging.txojLoggerI18N] (Thread-12) [com.arjuna.ats.internal.txoj.recovery.TORecoveryModule_6] - TORecoveryModule - second pass
            2008-10-01 10:11:10,830 DEBUG [com.arjuna.ats.jta.logging.loggerI18N] (Thread-12) [com.arjuna.ats.internal.jta.recovery.info.secondpass] Local XARecoveryModule - second pass
            2008-10-01 10:11:25,091 INFO [STDOUT] (http-127.0.0.1-8080-1) userService = null
            2008-10-01 10:11:25,092 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/lt-web].[SayHelloServlet]] (http-127.0.0.1-8080-1) Servlet.service() for servlet SayHelloServlet threw exception
            java.lang.NullPointerException
             at net.learntechnology.web.SayHelloServlet.doPost(SayHelloServlet.java:38)
             at net.learntechnology.web.SayHelloServlet.doGet(SayHelloServlet.java:30)
             at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
             at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
             at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
             at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
             at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
             at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
             at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
             at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
             at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
             at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:189)
             at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:91)
             at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:92)
             at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
             at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
             at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
             at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
             at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:325)
             at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:828)
             at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:601)
             at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
             at java.lang.Thread.run(Thread.java:613)
            2008-10-01 10:13:10,827 DEBUG [com.arjuna.ats.arjuna.logging.arjLogger] (Thread-12) Periodic recovery - first pass <Wed, 1 Oct 2008 10:13:10>
            2008-10-01 10:13:10,827 DEBUG [com.arjuna.ats.arjuna.logging.arjLogger] (Thread-12) StatusModule: first pass
            2008-10-01 10:13:10,827 DEBUG [com.arjuna.ats.txoj.logging.txojLoggerI18N] (Thread-12) [com.arjuna.ats.internal.txoj.recovery.TORecoveryModule_3] - TORecoveryModule - first pass
            


            • 3. Re: Can someone look at this application? help understanding
              jaikiran pai Master

               

              "rickcr" wrote:
              Maybe someone could try the actual ear in their deployment? I put it out here.. http://dl-client.getdropbox.com/u/86998/user-administration.ear (after it deploys try http://localhost:8080/lt-web/hello )



              I could have tried it, but i am away from my work place and dont have any access to my test setup for atleast a week and a half.

              "rickcr" wrote:

              here is some server.log info, but not sure how helpful it will be...


              I see that this log does not have the deployment part of your application. Could you upload the server.log or the console log to some place (like you did for the application)? I would have asked you to post the server.log here, but its just going to be too long. From the logs, i am trying to figure out whether the bean is deployed.

              Furthermore, can you post the output of the jndi-tree here. Follow these steps http://wiki.jboss.org/wiki/DisplayTheJDNITreeWithTheJMXConsole to view the jndi tree



              • 4. Re: Can someone look at this application? help understanding
                Rick Reumann Newbie

                Thanks again for trying to help. (I have the feeling it's going to be something really stupid that I'm not doing correctly in my maven build. I've looked over the exploded ear though and it seems ok?)

                Anyway, the EJB seems to deploy ok. I can access it fine grabbing it by the jndi name from the InitialContext.

                here is the ejb showing up in the jndi tree:

                http://dl-client.getdropbox.com/u/86998/jndi.png

                and here is the complete server.log

                http://dl-client.getdropbox.com/u/86998/server.log

                Here's the simple service locator that is working

                • 5. Re: Can someone look at this application? help understanding
                  jaikiran pai Master

                  The application, the logs and the jndi-tree all look good to me. No obvious issues. I don't see a reason why the bean should not be injected.

                  In your web.xml, you have marked the servlet as load-on-startup:

                  <load-on-startup>0</load-on-startup>



                  Could you try removing this load-on-startup and see if it works?


                  • 6. Re: Can someone look at this application? help understanding
                    jaikiran pai Master

                    Wait, i just noticed this in your web.xml:

                    <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
                     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
                     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
                     version="2.4">
                    


                    I just read that the annotation processing will not happen for this version of web-app xsd. Change it to:

                    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


                    and see if it works.


                    • 7. Re: Can someone look at this application? help understanding
                      Rick Reumann Newbie

                      You're a GENIUS ! That was it!

                      I would have never thought it was my web.xml descriptor!!

                      Man, I tried so many different things !!! argghh. Oh well, hopefully this might help someone else in the future that runs into the same issue.

                      I really appreciate you having taken the time to help! I'll buy you a beer or some Taco Bell if you're ever in Florida:)

                      • 9. Re: Can someone look at this application? help understanding
                        Bryan Kearney Newbie

                        Should the use of JBossWS-Metro cause this not to work? I have a simple POJO exposed as a web service. It is deployed into a Jboss5 App Server with JbossWS-Metro installed. The POJO references an @EJB and I get NullPointerExceptions when accessing hte instance variable.

                        -- bk

                        • 10. Re: Can someone look at this application? help understanding
                          James Ward Newbie

                          I'm having the same problem with JBoss 5.0.0 GA. I've updated my web.xml and it's still not injecting the EJB into my servlet. Any other ideas? Do I need any configuration files other than META-INF/application.xml in my ear? Do I need a jboss-web.xml file? Thanks!

                          • 11. Re: Can someone look at this application? help understanding
                            James Ward Newbie

                             

                            "jlward4" wrote:
                            I'm having the same problem with JBoss 5.0.0 GA. I've updated my web.xml and it's still not injecting the EJB into my servlet. Any other ideas? Do I need any configuration files other than META-INF/application.xml in my ear? Do I need a jboss-web.xml file? Thanks!


                            I got this working. Turns out I was packaging my Servlet class into my EJB JAR instead of into my WAR. Whooops!

                            • 12. Re: Can someone look at this application? help understanding
                              Miro Kandic Newbie
                              But, if servlet is  deployed as a part of separate EAR on same JBoss server then this code does not  work (web.xml version is 2.5).
                              According to EJB3.0  specification, this servlet should be considered as a local client but based on  log file it seems to me JBoss 5.1.0.GA is trying to find and inject a remote  interface:
                              "11:51:22,736 WARN [WebEJBRemoteHandler] EJBTHREE-1289: Using legacy  EjbEncInjector,...."
                              EJB 3.0 specification (Chapter 3.2.2):
                              Session beans may have local clients. A local  client is a client that is collocated in the same JVM with
                              the session bean that provides the local client  view and which may be tightly coupled to the bean. A
                              local client of a session bean may be another enterprise  bean or a web component.
                              If I use the next  annotation in servlet code it works fine:
                              @EJB (mappedName="MyEar-2.0.0-SNAPSHOT/MyEjb/local")
                              But,  since I do not want to have dependencies on Maven built EAR file name I tried to  enforce JNDI name of my EJB using not portable solution based on  "mappedName":
                              @Stateless(mappedName="MyEar/MyEjb/local")
                              public class MyEjb implements MyEjbLocal  {
                              ...
                              }
                              and the next  annotation in my servlet
                              @EJB (mappedName="MyEar/MyEjb/local")
                              This approach does not work even though I can see in  JBoss log file that my EJB is deployed also under JNDI name "MyEar/MyEjb/local" (in addition to default JNDI names  defined by JBoss).
                              Please, can somebody advise what to do  here.
                              Thanks
                              • 13. Re: Can someone look at this application? help understanding
                                jaikiran pai Master

                                mkandic wrote:

                                 


                                But,  since I do not want to have dependencies on Maven built EAR file name I tried to  enforce JNDI name of my EJB using not portable solution based on  "mappedName":
                                @Stateless(mappedName="MyEar/MyEjb/local")
                                public class MyEjb implements MyEjbLocal  {
                                ...
                                }

                                That won't work. You need @RemoteBinding or @LocalBinding. See this tutorial

                                • 14. Re: Can someone look at this application? help understanding
                                  Miro Kandic Newbie
                                  But, that is just another not portable solution because those annotations are JBoss specific.
                                  1 2 Previous Next