9 Replies Latest reply on Jul 28, 2011 1:09 PM by Andrew Murphy

    How to lookup SSB via JNDI

    JEE 4 Hire Newbie

      How can I lookup the following SSB via JNDI?


      @Named
      @Stateless
      public class TestBean {
      ...
      }

        • 1. Re: How to lookup SSB via JNDI
          Jason Porter Master

          Since you're using CDI, it begs the question why you would want to look it up via JNDI. Also, you could use the @EJB annotation.

          • 2. Re: How to lookup SSB via JNDI
            JEE 4 Hire Newbie

            The @Inject does not work inside a quartz job.
            I would like to lookup a Seam component via JNDI to enforce CDI to occur.
            This way, I can use @Inject inside the SSB to gain access to the database.
            I think this facade will work.


            Many thanks for all of your support.

            • 3. Re: How to lookup SSB via JNDI
              Jason Porter Master

              You will have to lookup a CDI bean via the BeanManager to insure injections occur. They are not bound to JNDI. The only CDI related object bound to JNDI is the BeanManager. As soon as you have a BeanManager you can lookup an instance of a bean via the BeanManager API.

              • 4. Re: How to lookup SSB via JNDI
                JEE 4 Hire Newbie

                http://docs.jboss.org/seam/3/latest/api/
                I failed to lookup java:comp/BeanManager according to the docs.
                How can I get it registered in JBoss AS 7, so I can look it up via JNDI?


                • 5. Re: How to lookup SSB via JNDI
                  Stuart Douglas Master

                  You can look up the bean from one of the following portable JNDI names. These names are printed in the server log on AS7 start:


                  java:module/TestBean
                  java:app/${myModuleName}/TestBean
                  java:global/${myAppName}/${myModuleName}/TestBean


                  EJB's looked up in this manner will be injected as normal, there is no need for the BeanManager.

                  • 6. Re: How to lookup SSB via JNDI
                    Stuart Douglas Master

                    If you want help on why your BeanManager lookup fails you are going to need to post more details, as the BM should be bound to java:comp/BeanManager in AS7.

                    • 7. Re: How to lookup SSB via JNDI
                      JEE 4 Hire Newbie
                      When the quartz scheduler retrieves the job (instance of HelloJob) from the database and attempts to run it, there are no active contexts.
                      How can I get a handle to perform CRUD operations from within a quartz job?


                      import org.quartz.Job;

                      public class HelloJob implements Job {
                        public void execute(JobExecutionContext context) throws JobExecutionException {
                          ...
                          lookupBean();
                        }

                        private void lookupBean() throws Exception {
                          InitialContext ctx = new InitialContext();
                          log.info("0000 bean.getInfo():");
                          TestBean bean = (TestBean) ctx.lookup("java:global/TestBean!TestBean");
                          log.info(bean.getInfo());
                        }
                      }
                      ##############################################################
                      @Named
                      @Stateless
                      public class TestBean {
                        @Inject
                        private Logger log;
                        @Inject
                        private EntityManager em;
                        private List<Account> accountList = null;

                        public String getInfo() {
                          return "getAccounts().size(): " + getAccounts().size();
                        }

                        public List<Account> getAccounts() {
                          if (null == accountList) {
                            accountList = em.createQuery("from Account").getResultList();
                            log.info("Loading account: size(): " + accountList.size());
                          }
                          return accountList;
                        }
                      }
                      ##############################################################
                      error:

                      07:54:00,079 INFO  [class HelloJob] (QuartzScheduler00_Worker-1) 0000 bean.getInfo():
                      07:54:00,084 WARN  [org.jboss.as.ejb3.component.EJBComponent] (QuartzScheduler00_Worker-1) EJBTHREE-2120: deprecated getTransactionAttributeType method called (dev problem)
                      07:54:00,097 ERROR [stderr] (QuartzScheduler00_Worker-1) javax.ejb.EJBException: org.jboss.weld.context.ContextNotActiveException: WELD-001303 No active contexts for scope type javax.enterprise.context.ConversationScoped
                      07:54:00,098 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:206)
                      07:54:00,098 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:275)
                      07:54:00,098 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.required(CMTTxInterceptor.java:377)
                      07:54:00,099 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.invoke(CMTTxInterceptor.java:233)
                      07:54:00,099 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:47)
                      07:54:00,099 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
                      07:54:00,099 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
                      07:54:00,099 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.as.ee.component.ViewDescription$1.processInvocation(ViewDescription.java:146)
                      07:54:00,099 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
                      07:54:00,100 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
                      07:54:00,100 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.as.ee.component.ProxyInvocationHandler.invoke(ProxyInvocationHandler.java:76)
                      07:54:00,100 ERROR [stderr] (QuartzScheduler00_Worker-1)        at TestBean$$$view22.getInfo(Unknown Source)
                      07:54:00,100 ERROR [stderr] (QuartzScheduler00_Worker-1)        at HelloJob.lookupBean(HelloJob.java:299)
                      07:54:00,100 ERROR [stderr] (QuartzScheduler00_Worker-1)        at HelloJob.execute(HelloJob.java:139)
                      07:54:00,101 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.quartz.core.JobRunShell.run(JobRunShell.java:206)
                      07:54:00,101 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:548)
                      07:54:00,101 ERROR [stderr] (QuartzScheduler00_Worker-1) Caused by: org.jboss.weld.context.ContextNotActiveException: WELD-001303 No active contexts for scope type javax.enterprise.context.ConversationScoped
                      07:54:00,101 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.weld.manager.BeanManagerImpl.getContext(BeanManagerImpl.java:667)
                      07:54:00,101 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:77)
                      07:54:00,102 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:124)
                      07:54:00,102 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.weld.proxies.EntityManager$ManagedPersistenceContext$2106413589$Proxy$_$$_WeldClientProxy.createQuery(EntityManager$ManagedPersistenceContext$2106413589$Proxy$_$$_WeldClientProxy.java)
                      07:54:00,102 ERROR [stderr] (QuartzScheduler00_Worker-1)        at TestBean.getRouters(TestBean.java:33)
                      07:54:00,102 ERROR [stderr] (QuartzScheduler00_Worker-1)        at TestBean.getInfo(TestBean.java:28)
                      07:54:00,102 ERROR [stderr] (QuartzScheduler00_Worker-1)        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                      07:54:00,102 ERROR [stderr] (QuartzScheduler00_Worker-1)        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                      07:54:00,103 ERROR [stderr] (QuartzScheduler00_Worker-1)        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                      07:54:00,103 ERROR [stderr] (QuartzScheduler00_Worker-1)        at java.lang.reflect.Method.invoke(Method.java:597)
                      07:54:00,103 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.as.ee.component.ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptor.java:51)
                      07:54:00,103 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
                      07:54:00,103 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:370)
                      07:54:00,104 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.doMethodInterception(Jsr299BindingsInterceptor.java:114)
                      07:54:00,104 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.as.weld.ejb.Jsr299BindingsInterceptor.processInvocation(Jsr299BindingsInterceptor.java:122)
                      07:54:00,104 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
                      07:54:00,104 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.as.jpa.interceptor.SBInvocationInterceptor.processInvocation(SBInvocationInterceptor.java:45)
                      07:54:00,104 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
                      07:54:00,104 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:44)
                      07:54:00,105 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
                      07:54:00,105 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.as.ee.component.TCCLInterceptor.processInvocation(TCCLInterceptor.java:45)
                      07:54:00,105 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
                      07:54:00,105 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.invocation.InitialInterceptor.processInvocation(InitialInterceptor.java:21)
                      07:54:00,105 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
                      07:54:00,106 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
                      07:54:00,106 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.as.ee.component.ViewDescription$ComponentDispatcherInterceptor.processInvocation(ViewDescription.java:202)
                      07:54:00,106 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
                      07:54:00,106 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.as.ejb3.component.session.SessionInvocationContextInterceptor.processInvocation(SessionInvocationContextInterceptor.java:67)
                      07:54:00,106 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
                      07:54:00,106 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.as.ejb3.component.pool.PooledInstanceInterceptor.processInvocation(PooledInstanceInterceptor.java:51)
                      07:54:00,107 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287)
                      07:54:00,107 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:370)
                      07:54:00,107 ERROR [stderr] (QuartzScheduler00_Worker-1)        at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:271)
                      • 8. Re: How to lookup SSB via JNDI
                        Jason Porter Master

                        As you're doing this in an EJB, I'd suggest having the EJB container inject the persistence context and see if that works. Another idea would be to create your own scope and a producer for the EntityManager for that scope.

                        • 9. Re: How to lookup SSB via JNDI
                          Andrew Murphy Newbie

                          As a quartz job is instantiated by quartz it is a non-managed bean. In order to reference a managed bean (e.g. EJB) within the quartz job you will need to look it up using JNDI. Here is an example:


                          @EJB(name = "ManagedEJB", beanInterface = ManagedEJB.class)
                          public class QuartzJob implements Job {
                          
                              private ManagedEJB managedEJB;
                          
                              public QuartzJob() {
                                  // Cannot @Inject into non-managed bean, use JNDI
                                  // look-up instead (requires class-level EJB annotation
                                  // - use EJBs annotation to define > 1)
                                  try {
                                      final Context ctx = new InitialContext();
                                      managedEJB = (ManagedEJB) ctx.lookup("java:global/test/ManagedEJB");
                          
                                  } catch (final NamingException ne) {
                                      ne.printStackTrace();
                                  }
                              }
                          
                              @Override
                              public void execute(final JobExecutionContext context)
                                      throws JobExecutionException {
                                  //...quartz logic here
                                  managedEJB.doSomething();
                              }
                          
                              public ManagedEJB getManagedEJB() {
                                  return managedEJB;
                              }
                          }
                          


                          I hope this helps.