11 Replies Latest reply on Oct 28, 2009 10:10 PM by steeven steeven

    java:comp/env not found in @service POJO bean

    steeven steeven Newbie

      I'm trying to use Service POJO bean ( jboss extension of EJB3), but I found that PersitentUnit was not found!

      @Service(name = MyService.SERVICE_NAME)
      @Management(IMyService.class)
      //@PersistenceContext(name="em") not work too
      public class MyService implements IMyService {
       public final static String SERVICE_NAME = "MyService";
       @PersistenceContext()
       private EntityManager em;
       public void create() throws Exception {
       log.info("Ems created");
       }
      
       @TransactionAttribute(TransactionAttributeType.REQUIRED)
       public void start() throws Exception {
       log.info("Ems started");
       //em.xxx this line worked
       new InitialContext().lookup("java:comp/env/em") not work.
       }
      }


      ExceptioN:
      javax.naming.NameNotFoundException: env not bound

      I'm retrieving EnityManager from some util class, that nest called in other class, it will be great trouble to pass em inside.

      Why no Enc in service POJO? any work around?

        • 1. Re: java:comp/env not found in @service POJO bean
          jaikiran pai Master

          Please post the entire exception stacktrace and also the piece of client code which gets hold of the service.

          • 2. Re: java:comp/env not found in @service POJO bean
            steeven steeven Newbie

            here is the full error stacktrace:

            09:12:17,884 ERROR [AbstractKernelController] Error installing to Start: name=jboss.j2ee:ear=my.ear,jar=myserver.jar,name=MyService,service=EJB3 state=Create
            java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
             at org.jboss.ejb3.service.ServiceContainer.invokeOptionalMethod(ServiceContainer.java:381)
             at org.jboss.ejb3.service.ServiceContainer.lockedStart(ServiceContainer.java:264)
             at org.jboss.ejb3.EJBContainer.start(EJBContainer.java:884)
             at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
             at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
             at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
             at java.lang.reflect.Method.invoke(Method.java:597)
             at org.jboss.reflect.plugins.introspection.ReflectionUtils.invoke(ReflectionUtils.java:59)
             at org.jboss.reflect.plugins.introspection.ReflectMethodInfoImpl.invoke(ReflectMethodInfoImpl.java:150)
             at org.jboss.joinpoint.plugins.BasicMethodJoinPoint.dispatch(BasicMethodJoinPoint.java:66)
             at org.jboss.kernel.plugins.dependency.KernelControllerContextAction$JoinpointDispatchWrapper.execute(KernelControllerContextAction.java:241)
             at org.jboss.kernel.plugins.dependency.ExecutionWrapper.execute(ExecutionWrapper.java:47)
             at org.jboss.kernel.plugins.dependency.KernelControllerContextAction.dispatchExecutionWrapper(KernelControllerContextAction.java:109)
             at org.jboss.kernel.plugins.dependency.KernelControllerContextAction.dispatchJoinPoint(KernelControllerContextAction.java:70)
             at org.jboss.kernel.plugins.dependency.LifecycleAction.installActionInternal(LifecycleAction.java:221)
             at org.jboss.kernel.plugins.dependency.InstallsAwareAction.installAction(InstallsAwareAction.java:54)
             at org.jboss.kernel.plugins.dependency.InstallsAwareAction.installAction(InstallsAwareAction.java:42)
             at org.jboss.dependency.plugins.action.SimpleControllerContextAction.simpleInstallAction(SimpleControllerContextAction.java:62)
             at org.jboss.dependency.plugins.action.AccessControllerContextAction.install(AccessControllerContextAction.java:71)
             at org.jboss.dependency.plugins.AbstractControllerContextActions.install(AbstractControllerContextActions.java:51)
             at org.jboss.dependency.plugins.AbstractControllerContext.install(AbstractControllerContext.java:348)
             at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:1631)
             at org.jboss.dependency.plugins.AbstractController.incrementState(AbstractController.java:934)
             at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:1082)
             at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:984)
             at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:774)
             at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:540)
             at org.jboss.deployers.vfs.deployer.kernel.BeanMetaDataDeployer.deploy(BeanMetaDataDeployer.java:121)
             at org.jboss.deployers.vfs.deployer.kernel.BeanMetaDataDeployer.deploy(BeanMetaDataDeployer.java:51)
             at org.jboss.deployers.spi.deployer.helpers.AbstractSimpleRealDeployer.internalDeploy(AbstractSimpleRealDeployer.java:62)
             at org.jboss.deployers.spi.deployer.helpers.AbstractRealDeployer.deploy(AbstractRealDeployer.java:50)
             at org.jboss.deployers.plugins.deployers.DeployerWrapper.deploy(DeployerWrapper.java:171)
             at org.jboss.deployers.plugins.deployers.DeployersImpl.doDeploy(DeployersImpl.java:1439)
             at org.jboss.deployers.plugins.deployers.DeployersImpl.doInstallParentFirst(DeployersImpl.java:1157)
             at org.jboss.deployers.plugins.deployers.DeployersImpl.doInstallParentFirst(DeployersImpl.java:1178)
             at org.jboss.deployers.plugins.deployers.DeployersImpl.doInstallParentFirst(DeployersImpl.java:1210)
             at org.jboss.deployers.plugins.deployers.DeployersImpl.install(DeployersImpl.java:1098)
             at org.jboss.dependency.plugins.AbstractControllerContext.install(AbstractControllerContext.java:348)
             at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:1631)
             at org.jboss.dependency.plugins.AbstractController.incrementState(AbstractController.java:934)
             at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:1082)
             at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:984)
             at org.jboss.dependency.plugins.AbstractController.change(AbstractController.java:822)
             at org.jboss.dependency.plugins.AbstractController.change(AbstractController.java:553)
             at org.jboss.deployers.plugins.deployers.DeployersImpl.process(DeployersImpl.java:781)
             at org.jboss.deployers.plugins.main.MainDeployerImpl.process(MainDeployerImpl.java:702)
             at org.jboss.system.server.profileservice.repository.MainDeployerAdapter.process(MainDeployerAdapter.java:117)
             at org.jboss.system.server.profileservice.repository.ProfileDeployAction.install(ProfileDeployAction.java:70)
             at org.jboss.system.server.profileservice.repository.AbstractProfileAction.install(AbstractProfileAction.java:53)
             at org.jboss.system.server.profileservice.repository.AbstractProfileService.install(AbstractProfileService.java:361)
             at org.jboss.dependency.plugins.AbstractControllerContext.install(AbstractControllerContext.java:348)
             at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:1631)
             at org.jboss.dependency.plugins.AbstractController.incrementState(AbstractController.java:934)
             at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:1082)
             at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:984)
             at org.jboss.dependency.plugins.AbstractController.change(AbstractController.java:822)
             at org.jboss.dependency.plugins.AbstractController.change(AbstractController.java:553)
             at org.jboss.system.server.profileservice.repository.AbstractProfileService.activateProfile(AbstractProfileService.java:306)
             at org.jboss.system.server.profileservice.ProfileServiceBootstrap.start(ProfileServiceBootstrap.java:271)
             at org.jboss.bootstrap.AbstractServerImpl.start(AbstractServerImpl.java:461)
             at org.jboss.Main.boot(Main.java:221)
             at org.jboss.Main$1.run(Main.java:556)
             at java.lang.Thread.run(Thread.java:619)
            Caused by: java.lang.reflect.InvocationTargetException
             at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
             at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
             at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
             at java.lang.reflect.Method.invoke(Method.java:597)
             at org.jboss.ejb3.service.ServiceContainer.invokeOptionalMethod(ServiceContainer.java:369)
             ... 62 more
            Caused by: javax.naming.NameNotFoundException: env not bound
             at org.jnp.server.NamingServer.getBinding(NamingServer.java:771)
             at org.jnp.server.NamingServer.getBinding(NamingServer.java:779)
             at org.jnp.server.NamingServer.getObject(NamingServer.java:785)
             at org.jnp.server.NamingServer.lookup(NamingServer.java:396)
             at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:726)
             at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:833)
             at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:686)
             at javax.naming.InitialContext.lookup(InitialContext.java:392)
             at org.steeven.server.MyService.start(EmsServer.java:43)
             ... 67 more


            This exception happened in Service POJO start() method, no client code.

            An interesting thing: if an EJB is invoked first, new InitialContext().lookup("java:comp/env/em") will work without exception.

            • 3. Re: java:comp/env not found in @service POJO bean
              jaikiran pai Master

              Which exact version of JBoss AS is this? Use our latest EJB3 plugin http://www.jboss.org/ejb3/ejb3plugin.html against JBoss AS 5.1.0 and try it out.

              The latest plugin can be downloaded from here http://www.jboss.org/ejb3/downloads.html

              • 4. Re: java:comp/env not found in @service POJO bean
                paata lominadze Master

                which as are you using ?
                i can give you my full working example.
                1. Management Interface:

                @Management
                public interface CommonServiceMan {
                 void create() throws SomeMyException;
                 void start() throws SomeMyException;
                 void stop() throws SomeMyException;
                 void destroy() throws SomeMyException;
                }
                

                2.Remote Interface
                @Remote
                public interface CommonServiceInt {
                 public String testMethod(String name) throws SomeMyException;
                }
                

                3.Implementation
                @Remote(CommonServiceInt.class)
                @Service(objectName = "jboss:custom=CommonServiceBean")
                public class CommonServiceBean implements CommonServiceInt,
                 CommonServiceMan {
                 public String testMethod(String name) throws SomeMyException{
                 System.out.println("my test method call");
                 return "Hello : "+name;
                 }
                 public void create() throws SomeMyException {
                 System.out.println("Create method ....") ;
                 }
                 public void start() throws SomeMyException {
                 System.out.println("Start method ....") ;
                 }
                 public void stop() throws SomeMyException {
                 System.out.println("Stop method ....") ;
                 }
                 public void destroy() throws SomeMyException {
                 System.out.println("Destroy method ....") ;
                 }
                }
                
                



                This example is working fine for me.
                I test it on jboss as 4.2.1, 4.2.2, 4.2.3 and 5.1.0 (i use each one for different projects.)

                ______________________
                Regards,
                Paata Lominadze.
                Magticom LTD.



                • 5. Re: java:comp/env not found in @service POJO bean
                  paata lominadze Master

                  sorry i missed EntityManager declaration into implementation.

                  @PersistenceContext(unitName = "CustomerCareOracle")
                  private EntityManager oracleManager;
                  


                  • 6. Re: java:comp/env not found in @service POJO bean
                    steeven steeven Newbie

                    Thanks you guys all, I'm using AS5.1.0

                    "grdzeli_kaci" wrote:
                    sorry i missed EntityManager declaration into implementation.
                    @PersistenceContext(unitName = "CustomerCareOracle")
                    private EntityManager oracleManager;
                    

                    This code work for me, but try to invoke some method to oracleManager, exception with raise out.

                    • 7. Re: java:comp/env not found in @service POJO bean
                      paata lominadze Master

                      ok, then you must update latest ejb 3 plugin.
                      link is posted above.

                      I use this code into my application on jboss 5.1.0.GA with have no any problem.



                      ______________________
                      Regards,
                      Paata Lominadze.
                      Magticom LTD.

                      • 8. Re: java:comp/env not found in @service POJO bean
                        steeven steeven Newbie

                        grdzeli_kaci,

                        Thanks very much~
                        Are you sure oracleManager worked in start() method?

                        • 9. Re: java:comp/env not found in @service POJO bean
                          paata lominadze Master

                           

                          "steeven" wrote:
                          grdzeli_kaci,

                          Thanks very much~
                          Are you sure oracleManager worked in start() method?



                          yes of course,
                          It is very useful pattern.
                          Singleton service is very useful for me.
                          I load 90 % of database table into memory, because it contains maximum 100 rows each table, these table are configuration table, i have no transaction table into memory only.

                          i load all table in start method, and then if i change something into database, i'll make reload concrete table(s) or row.


                          I'll be very glad that into ejb 3.1 will be singleton service, because now my projects is application server dependent, you know that @Service is jboss ejb3 extension.


                          ______________________
                          Regards,
                          Paata Lominadze.
                          Magticom LTD.





                          • 10. Re: java:comp/env not found in @service POJO bean
                            steeven steeven Newbie

                            Thanks very much, I'll try to ask EJB to do load data instead of invoking EnityManager directly.

                            • 11. Re: java:comp/env not found in @service POJO bean
                              steeven steeven Newbie

                              Thanks, I will try to call @EJB instead of EntityManager to avoid this problem. really appreciated.