14 Replies Latest reply on Apr 20, 2008 11:38 AM by phantasmo

    EJB3 example

    phantasmo
      I've got myself a Java EE book and started learning. So, the first EJB3 example was as following:
      package beans;
      import javax.ejb.Remote;
      @Remote
      public interface SimpleSession
      {
          public String getEchoString(String clientString);
      }
      package beans;
      import javax.ejb.Stateless;
      @Stateless
      public class SimpleSessionBean implements SimpleSession {
            public String getEchoString(String clientString) {
               return clientString + " - from session bean";
            }
      }
      package client;
      import beans.SimpleSession;
      import javax.naming.InitialContext;
      public class SimpleSessionClient {
         public static void main(String[] args) throws Exception
         {
            InitialContext ctx = new InitialContext();
            SimpleSession simpleSessio = (SimpleSession) ctx.lookup(SimpleSession.class.getName());
            for (int i = 0; i < args.length; i++) {
               String returnedString = simpleSession.getEchoString(args);
      System.out.println("sent string: " + args[i] + ", received string: " + returnedString);
      }
      }
      }
      For some reason, I can't run this. I opened the command prompt, typed
      set CLASSPATH=.;C:\java\jboss\lib\concurrent.jar;C:\java\jboss\lib\jboss-common.jar;C:\java\jboss\server\all\lib\jboss.jar;C:\java\jboss\server\all\lib\jboss-remoting.jar;C:\java\jboss\server\all\lib\jboss-transaction.jar;C:\java\jboss\server\all\lib\jnpserver.jar;C:\java\jboss\server\all\deploy\ejb3.deployer\jboss-ejb3.jar;C:\java\jboss\server\all\deploy\ejb3.deployer\jboss-ejb3x.jar;C:\java\jboss\server\all\deploy\jboss-aop.deployer\jboss-aop.jar;C:\java\jboss\server\all\deploy\jboss-aop.deployer\jboss-aspect-library.jar
      My CLASSPATH sys variable is set to include every jar in JavaEE\lib directory. I did "jar cf SimpleSessionApp.ejb3 beans\*.java" and copied the resulting ejb3 into jboss\server\all\deploy. Then, when  tried running the accursed thing with 
      java -Djava.naming.factory.initial=org.jnp.interfaces.NamingContextFactory -Djava.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces -Djava.naming.provider.url=localhost client.SimpleSessionClient Now is the time for all good men
      it said 
      Exception in thread "main" javax.naming.NameNotFoundException: beans.SimpleSession not bound
      at org.jnp.server.NamingServer.getBinding(NamingServer.java:529)
      I know it has to be a very bothersome question, but could you please help me understanding what happened here and how should I resolve it? I get the concept of binding a name to a resource beforehand and asking for it later, but what I do not get is where should I have done it so that I do not get this error...                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        • 1. Re: EJB3 example
          jwenting
          Another case of RTFM I'm afraid.
          Your JNDI URI is wrong.
          But even when you fix that the name you look up is still wrong.
          • 2. Re: EJB3 example
            phantasmo
            Another case of RTFM I'm afraid.
            I was hoping you wouldn't say that... because I don't know where to look...
            Your JNDI URI is wrong.
            But even when you fix that the name you look up is still wrong.>
            You mean this: SimpleSession.class.getName()?
            Well, I suspected as much, but being completely unaware of how things function in Java EE and specifically in JBoss, I had a hard time locating the list of JNDI names, so I was trying with "SimpleSessionBean/remote" etc. And when I had finally found it in the JMX Console, it showed me that not only was the name I used wrong, but that there is no binding for SimpleSession whatsoever.
            Any ideas here?
            But even when you fix that the name you look up is still wrong.
            Ummm... Didn't get this one...
            • 3. Re: EJB3 example
              jwenting
              The URI is wrong.
              The jndi properties needed are as follows (by default for JBoss)
              java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
              java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
              java.naming.provider.url=jnp://localhost:1099
              Easiest is to create a file named jndi.properties on your classpath containing those 3 lines, that way you won't have to type it all the time.

              For the jndi name of an EJB3 entity bean JBoss by default uses classname/remote (and classname/local for the local interface). Unless overridden by annotations or deployment descriptors of course.
              When deployed as part of an enterprise applications (so in an earfile) that becomes applicationname/beanname/remote or applicationname/beanname/local
              • 4. Re: EJB3 example
                phantasmo
                Still no results... I put the jndi.properties to my classpath, changed the lookup string to "SimpleSession/remote", but nothing changed...
                The thing that really puzzles me is why is there no mention of SimpleSession in the list of JNDI names? Could it be that the deployment was unsuccesful? Am I right to think that during the deployment a JNDI name will be assigned to my EJB or should I do something more? When I started JBoss it said: [EJB3 Deployer] Deploying C:\Java\Jboss\server\all\deploy\SimpleSessionApp.ejb3. No errors or some such...

                Thanks for the time you spent on me....
                • 5. Re: EJB3 example
                  jwenting
                  did you deploy it at all?
                  Check your deployment logs :)

                  and the classname isn't "SimpleSession", it's "SimpleSessionBean".
                  • 6. Re: EJB3 example
                    phantasmo
                    I just checked. It seems to my untrained eye that everything is ok. I tried with "SimpleSessionBean/remote" too but it doesn't really matter as it's not bound to ANY name... Here's what the log says:
                    2008-04-18 19:05:49,171 DEBUG [org.jboss.deployment.MainDeployer] Starting deployment of package: file:/C:/Java/jboss/server/all/deploy/SimpleSessionApp.ejb3
                    2008-04-18 19:05:49,171 DEBUG [org.jboss.deployment.MainDeployer] Starting deployment (init step) of package at: file:/C:/Java/jboss/server/all/deploy/SimpleSessionApp.ejb3
                    2008-04-18 19:05:49,171 DEBUG [org.jboss.deployment.MainDeployer] Copying file:/C:/Java/jboss/server/all/deploy/SimpleSessionApp.ejb3 -> C:\Java\jboss\server\all\tmp\deploy\tmp45409SimpleSessionApp.ejb3
                    2008-04-18 19:05:49,171 DEBUG [org.jboss.deployment.MainDeployer] using deployer MBeanProxyExt[jboss.ejb3:service=EJB3Deployer]
                    2008-04-18 19:05:49,187 DEBUG [org.jboss.ejb3.EJB3Deployer] looking for nested deployments in : file:/C:/Java/jboss/server/all/deploy/SimpleSessionApp.ejb3
                    2008-04-18 19:05:49,187 DEBUG [org.jboss.deployment.DeploymentInfo] createLoaderRepository from config: LoaderRepositoryConfig(repositoryName: JMImplementation:service=LoaderRepository,name=Default, repositoryClassName: null, configParserClassName: null, repositoryConfig: null)
                    2008-04-18 19:05:49,187 DEBUG [org.jboss.mx.loading.RepositoryClassLoader] setRepository, repository=org.jboss.mx.loading.UnifiedLoaderRepository3@4aeb52, cl=org.jboss.mx.loading.UnifiedClassLoader3@206be6{ url=file:/C:/Java/jboss/server/all/tmp/deploy/tmp45409SimpleSessionApp.ejb3 ,addedOrder=0}
                    2008-04-18 19:05:49,187 DEBUG [org.jboss.mx.loading.RepositoryClassLoader] setRepository, repository=org.jboss.mx.loading.UnifiedLoaderRepository3@4aeb52, cl=org.jboss.mx.loading.UnifiedClassLoader3@206be6{ url=file:/C:/Java/jboss/server/all/tmp/deploy/tmp45409SimpleSessionApp.ejb3 ,addedOrder=0}
                    2008-04-18 19:05:49,187 DEBUG [org.jboss.mx.loading.UnifiedLoaderRepository3] Adding org.jboss.mx.loading.UnifiedClassLoader3@206be6{ url=file:/C:/Java/jboss/server/all/tmp/deploy/tmp45409SimpleSessionApp.ejb3 ,addedOrder=0}
                    2008-04-18 19:05:49,187 DEBUG [org.jboss.deployment.MainDeployer] found 0 subpackages of file:/C:/Java/jboss/server/all/deploy/SimpleSessionApp.ejb3
                    2008-04-18 19:05:49,187 DEBUG [org.jboss.deployment.MainDeployer] Watching new file: file:/C:/Java/jboss/server/all/deploy/SimpleSessionApp.ejb3
                    2008-04-18 19:05:49,187 DEBUG [org.jboss.deployment.MainDeployer] create step for deployment file:/C:/Java/jboss/server/all/deploy/SimpleSessionApp.ejb3
                    2008-04-18 19:05:49,187 DEBUG [org.jboss.ejb3.EJB3Deployer] create, SimpleSessionApp.ejb3
                    2008-04-18 19:05:49,203 DEBUG [org.jboss.ejb3.EJB3Deployer] Deploying: file:/C:/Java/jboss/server/all/deploy/SimpleSessionApp.ejb3
                    2008-04-18 19:05:49,203 DEBUG [org.jboss.system.ServiceController] Creating service jboss.j2ee:service=EJB3,module=SimpleSessionApp.ejb3
                    2008-04-18 19:05:49,203 DEBUG [org.jboss.ejb3.Ejb3Module] Creating jboss.j2ee:service=EJB3,module=SimpleSessionApp.ejb3
                    2008-04-18 19:05:49,484 DEBUG [org.jboss.ejb3.Ejb3Deployment] EJB3 deployment time took: 281
                    2008-04-18 19:05:49,484 DEBUG [org.jboss.ejb3.Ejb3Module] Created jboss.j2ee:service=EJB3,module=SimpleSessionApp.ejb3
                    2008-04-18 19:05:49,484 DEBUG [org.jboss.system.ServiceController] Creating dependent components for: jboss.j2ee:service=EJB3,module=SimpleSessionApp.ejb3 dependents are: []
                    2008-04-18 19:05:49,484 DEBUG [org.jboss.deployment.MainDeployer] Done with create step of deploying SimpleSessionApp.ejb3
                    2008-04-18 19:05:49,484 DEBUG [org.jboss.deployment.MainDeployer] Begin deployment start file:/C:/Java/jboss/server/all/deploy/SimpleSessionApp.ejb3
                    2008-04-18 19:05:49,484 DEBUG [org.jboss.ejb3.EJB3Deployer] start application, deploymentInfo: org.jboss.deployment.DeploymentInfo@f9f1cc98 { url=file:/C:/Java/jboss/server/all/deploy/SimpleSessionApp.ejb3 }
                      deployer: MBeanProxyExt[jboss.ejb3:service=EJB3Deployer]
                      status: Starting
                      state: START_DEPLOYER
                      watch: file:/C:/Java/jboss/server/all/deploy/SimpleSessionApp.ejb3
                      altDD: null
                      lastDeployed: 1208559949187
                      lastModified: 1208559949171
                      mbeans:
                    , short name: SimpleSessionApp.ejb3, parent short name: null
                    2008-04-18 19:05:49,484 DEBUG [org.jboss.system.ServiceController] starting service jboss.j2ee:service=EJB3,module=SimpleSessionApp.ejb3
                    2008-04-18 19:05:49,484 DEBUG [org.jboss.ejb3.Ejb3Module] Starting jboss.j2ee:service=EJB3,module=SimpleSessionApp.ejb3
                    2008-04-18 19:05:49,484 DEBUG [org.jboss.ejb3.Ejb3Module] Started jboss.j2ee:service=EJB3,module=SimpleSessionApp.ejb3
                    2008-04-18 19:05:49,484 DEBUG [org.jboss.system.ServiceController] Starting dependent components for: jboss.j2ee:service=EJB3,module=SimpleSessionApp.ejb3 dependent components: []
                    2008-04-18 19:05:49,484 INFO  [org.jboss.ejb3.EJB3Deployer] Deployed: file:/C:/Java/jboss/server/all/deploy/SimpleSessionApp.ejb3
                    2008-04-18 19:05:49,500 DEBUG [org.jboss.deployment.MainDeployer] End deployment start on package: SimpleSessionApp.ejb3
                    2008-04-18 19:05:49,500 DEBUG [org.jboss.deployment.MainDeployer] Deployed package: file:/C:/Java/jboss/server/all/deploy/SimpleSessionApp.ejb3
                    2008-04-18 19:05:49,500 DEBUG [org.jboss.deployment.scanner.URLDeploymentScanner] Watch URL for: file:/C:/Java/jboss/server/all/deploy/SimpleSessionApp.ejb3 -> file:/C:/Java/jboss/server/all/deploy/SimpleSessionApp.ejb3
                    • 7. Re: EJB3 example
                      jwenting
                      Now look in the JBoss JMX console to find the jndi name.
                      http://localhost:8080/jmx-console is the default URL for it.
                      Find the JNDIView service, and inside that select the List service.
                      Somewhere near the bottom you should find your EJB.
                      The exact name may differ, usually it's applicationName/beanName/remote (it will be shown as a treeview in the console).
                      • 8. Re: EJB3 example
                        phantasmo
                        Been there, done that... nothing... No mention of SimpleSession is JNDI list...
                        Is here anything else I should configure in JBoss before deploying? I just extracted it, ran it, and deployed my EJB as an *.ejb3 file (got it with "jar cf SimpleSessionApp.ejb3 beans\*.java" and copied it to JBOSS_HOME\server\all\deploy).

                        Maybe I should get a tutorial on Glassfish and try with that...
                        • 9. Re: EJB3 example
                          jwenting
                          oh boy, you didn't even compile the classes beforehand?
                          I should have noticed no deployment of the actual EJB being mentioned in that serverlog...

                          The EJB container won't compile your classes for you, you have to do that yourself. It will only generate the stubs from them that it needs, relieving you of having to use rmic.
                          So compile the sources, pack them into an EJB jar, optionally (for the future certainly) pack that into an ear file, and deploy that.
                          • 10. Re: EJB3 example
                            phantasmo
                            Oh my, I actually did deploy uncompiled files... Thanks a lot for pointing that out!
                            Now I'm getting a JNDI name and the lookup works but I'm getting another exception:
                            Exception in thread "main" java.lang.NoClassDefFoundError: [Lorg/jboss/aop/advic
                            e/Interceptor;
                                    at java.lang.Class.getDeclaredFields0(Native Method)
                             ...
                            2)
                                    at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:710)
                                    at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:627)
                                    at javax.naming.InitialContext.lookup(Unknown Source)
                                    at client.SimpleSessionClient.main(SimpleSessionClient.java:9)
                            One more time, I'm very gateful to you for all your effort.
                            • 11. Re: EJB3 example
                              jwenting
                              put the proper client jar from JBoss on the classpath of your client application.
                              I guess you put something there but not the right one.

                              filename is jbossall-client.jar from your JBoss/client directory.
                              • 12. Re: EJB3 example
                                phantasmo
                                Yet again, your advice has helped me, and I am very grateful.
                                After adding all the necessary jars from JBOSS_HOME\client, I'm happy to say, the program finally works and I'm on my way to learning EJBs.

                                Your patience throughout all this is greatly appreciated and if I ever get a chance I'll be sure to return the favor.

                                Thank you :)
                                • 13. Re: EJB3 example
                                  jwenting
                                  you need only that one jar. It contains everything contained in the others :)
                                  • 14. Re: EJB3 example
                                    phantasmo
                                    Yeah, I realize that now. I had a bunch of jars from Sun's reference implementation in my classpath system variable that I've completely forgotten about and that was causing all the trouble with class definition exceptions. I removed them all and added just the JBoss one you've mentioned. It works properly now.

                                    Thanks a bunch :)