1

Do some can help me with a EJB initialization in Quartz job

I have Quartz like this:

@DisallowConcurrentExecution
public class TestJob implements Job{

private TestEJBServiceLocal testEJBService;

private void initEJB() {
   this.testEJBService = new JNDIUtil()
            .getByJndiName("java:comp/env/ejb/TestEJBService");
}
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
    // DO STUFF
}
}

In web XML I have:

<ejb-local-ref>
        <description />
        <ejb-ref-name>ejb/TestEJBService</ejb-ref-name>
        <ejb-ref-type>Session</ejb-ref-type>
        <local-home />
        <local>com.ibm.blue.ejb.TestEJBServiceLocal</local>
        <ejb-link>TestEJBService</ejb-link>
    </ejb-local-ref>

Was Liberty profile can not initialize EJB with an exception:

2015-06-04 16:18:00 ERROR JNDIUtil:38 - JNDIUtil lookup error;
javax.naming.NamingException: CWNEN1000E: A JNDI operation on a java:comp/env name cannot be completed because the current thread is not associated with a Java Enterprise Edition application component. This condition can occur when the JNDI client using the java:comp/env name does not occur on the thread of a server application request. Make sure that a Java EE application does not run JNDI operations on java:comp/env names within static code blocks or in threads created by that application. Such code does not necessarily run on the thread of a server application request and therefore is not supported by JNDI operations on java:comp/env names.
    at com.ibm.ws.injectionengine.osgi.internal.naming.InjectionJavaColonHelper.getInjectionScopeData(InjectionJavaColonHelper.java:110)
    at com.ibm.ws.injectionengine.osgi.internal.naming.InjectionJavaColonHelper.getObjectInstance(InjectionJavaColonHelper.java:67)
    at com.ibm.ws.jndi.url.contexts.javacolon.internal.JavaURLContext.lookup(JavaURLContext.java:319)
    at com.ibm.ws.jndi.url.contexts.javacolon.internal.JavaURLContext.lookup(JavaURLContext.java:357)
    at org.apache.aries.jndi.DelegateContext.lookup(DelegateContext.java:161)
    at javax.naming.InitialContext.lookup(InitialContext.java:423)
    at JNDIUtil.lookup(JNDIUtil.java:36)
    at getByJndiName(JNDIUtil.java:24)
    at Job.initEJB(Job.java:52)
    at execute(Job.java:68)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
2015-06-04 16:18:00 ERROR JNDIUtil:38 - JNDIUtil lookup error;

I am running IBM Liberty profile V. 8.5.5.5

UPDATE

I have tried to update JNDI lookup string (the global JNDI is printed during app startup in trace.log) I still geting the same exception. Here is my updates in a JOB:

private void initEJB() {
   Context context;
   String jndi = "java:global/TestEAR/TestEJBS/TestEJBService!com.ibm.blue.ejb.TestEJBServiceLocal"
   try{
      context = new InitialContext();
      this.testEJBService = (TestEJBServiceLocal)context.lookup(jndi); 
       } catch (NamingException e) {
        System.out.println("-->Test with InitialContext: Error");
        e.printStackTrace();
    }
  try {
        this.testEJBService = new JNDIUtil().getByJndiName(jndi);
    } catch (Exception e) {
        System.err.println("-->Test with JNDIUtil: Error");
        e.printStackTrace();
    }
 }

Here is output log:

[err] -->Test with InitialContext: Error
[err] javax.naming.NamingException: CWNEN1000E: A JNDI operation on a java:comp/env name cannot be completed because the current thread is not associated with a Java Enterprise Edition application component. This condition can occur when the JNDI client using the java:comp/env name does not occur on the thread of a server application request. Make sure that a Java EE application does not run JNDI operations on java:comp/env names within static code blocks or in threads created by that application. Such code does not necessarily run on the thread of a server application request and therefore is not supported by JNDI operations on java:comp/env names.
[err]   at com.ibm.ws.injectionengine.osgi.internal.naming.InjectionJavaColonHelper.getInjectionScopeData(InjectionJavaColonHelper.java:110)
[err]   at com.ibm.ws.injectionengine.osgi.internal.naming.InjectionJavaColonHelper.getObjectInstance(InjectionJavaColonHelper.java:67)
[err]   at com.ibm.ws.jndi.url.contexts.javacolon.internal.JavaURLContext.lookup(JavaURLContext.java:319)
[err]   at com.ibm.ws.jndi.url.contexts.javacolon.internal.JavaURLContext.lookup(JavaURLContext.java:357)
[err]   at org.apache.aries.jndi.DelegateContext.lookup(DelegateContext.java:161)
[err]   at javax.naming.InitialContext.lookup(InitialContext.java:423)
[err]   at Job.initEJB(Job.java:38)
[err]   at execute(Job.java:75)
[err]   at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
[err]   at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
[err] -->Test with JNDIUtil: Error
[err] java.lang.RuntimeException: Cannot get bean by JNDI name; JNDI=java:global/TestEAR/TestEJBS/TestEJBService!com.ibm.blue.ejb.TestEJBServiceLocal
[err]   at JNDIUtil.lookup(JNDIUtil.java:39)
[err]   at JNDIUtil.getByJndiName(JNDIUtil.java:24)
[err]   at initEJB(Job.java:62)
[err]   at Job.execute(Job.java:75)
[err]   at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
[err]   at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
[err] Caused by: 
[err] javax.naming.NamingException: CWNEN1000E: A JNDI operation on a java:comp/env name cannot be completed because the current thread is not associated with a Java Enterprise Edition application component. This condition can occur when the JNDI client using the java:comp/env name does not occur on the thread of a server application request. Make sure that a Java EE application does not run JNDI operations on java:comp/env names within static code blocks or in threads created by that application. Such code does not necessarily run on the thread of a server application request and therefore is not supported by JNDI operations on java:comp/env names.
[err]   at com.ibm.ws.injectionengine.osgi.internal.naming.InjectionJavaColonHelper.getInjectionScopeData(InjectionJavaColonHelper.java:110)
[err]   at com.ibm.ws.injectionengine.osgi.internal.naming.InjectionJavaColonHelper.getObjectInstance(InjectionJavaColonHelper.java:67)
[err]   at com.ibm.ws.jndi.url.contexts.javacolon.internal.JavaURLContext.lookup(JavaURLContext.java:319)
[err]   at com.ibm.ws.jndi.url.contexts.javacolon.internal.JavaURLContext.lookup(JavaURLContext.java:357)
[err]   at org.apache.aries.jndi.DelegateContext.lookup(DelegateContext.java:161)
[err]   at javax.naming.InitialContext.lookup(InitialContext.java:423)
[err]   at JNDIUtil.lookup(JNDIUtil.java:36)
[err]   ... 5 more
Mindaugas Jaraminas
  • 3,261
  • 2
  • 24
  • 37

1 Answers1

2

Quartz is using unmanaged threads, so the java:comp/env namespace and reference is not available there.

You can either:

  • use full JNDI name of the EJB <=== UPDATE2 I cannot make it running with local interfaces from unmanaged thread for now (I don't have time to play with it more right now). So you would either need to use Liberty Beta, which supports remote EJB and change your bean to have remote interface. Or you could try to configure Quartz to use managed thread factory provided by Liberty concurrent feature (in similar fashion like here (but this is for full WAS). I didn't try it, so don't know if it is actually possible).

  • or, what would be probably better, instead of Quartz use

    • concurrent-1.0 feature of WebSphere Liberty, which uses managed threads and has access to Java EE context, for example Managed scheduled executor
    • or use EJB Timers and @Schedule annotation.

UPDATE
You can find the full JNDI name in the message log when it is bound by server like this:

[INFO    ] CNTR0167I: The server is binding the test.Hello interface of the Hello enterprise bean in the HelloEJBLocal.war module of the HelloEJBLocal application.  
The binding location is: java:global/HelloEJBLocal/Hello!test.Hello

then you lookup it using (this only works from Java EE application component or managed thread):

ctx.lookup("java:global/HelloEJBLocal/Hello!test.Hello");
Community
  • 1
  • 1
Gas
  • 17,601
  • 4
  • 46
  • 93