Unable to access java:comp namespace from a non JavaEE spawned thread such as Quartz in JBoss EAP 6

Solution Verified - Updated -

Issue

  • We are packaging quartz in our application and schedule a Quartz Job, which when it runs tries to look up an EJB java:comp/env/ejb/HelloBean but it fails to find it, even though the ejb-local-ref is defined in the web.xml

The web.xml contains:

<web-app version="3.0"
         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_3_0.xsd" >
    ...
    <ejb-local-ref>
        <ejb-ref-name>ejb/HelloBean</ejb-ref-name>
        <ejb-ref-type>Session</ejb-ref-type>
        <local>com.jboss.examples.ejb.HelloLocal</local>
        <ejb-link>HelloBean</ejb-link>
    </ejb-local-ref>
    ...
</web-app>

The exception when HelloJob Quartz Job runs:

ERROR [stderr] (DefaultQuartzScheduler_Worker-9) javax.naming.NameNotFoundException: java:comp/env/ejb/HelloBean
ERROR [stderr] (DefaultQuartzScheduler_Worker-9)    at org.jboss.as.naming.InitialContext.findContext(InitialContext.java:198)
ERROR [stderr] (DefaultQuartzScheduler_Worker-9)    at org.jboss.as.naming.InitialContext.lookup(InitialContext.java:118)
ERROR [stderr] (DefaultQuartzScheduler_Worker-9)    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:183)
ERROR [stderr] (DefaultQuartzScheduler_Worker-9)    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:179)
ERROR [stderr] (DefaultQuartzScheduler_Worker-9)    at javax.naming.InitialContext.lookup(InitialContext.java:411)
ERROR [stderr] (DefaultQuartzScheduler_Worker-9)    at com.jboss.examples.quartz.HelloJob.execute(HelloJob.java:26)
ERROR [stderr] (DefaultQuartzScheduler_Worker-9)    at org.quartz.core.JobRunShell.run(JobRunShell.java:178)
ERROR [stderr] (DefaultQuartzScheduler_Worker-9)    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:487)

The Quartz Job looks like this:

import org.quartz.*;
import javax.naming.InitialContext;

public class HelloJob implements Job {
  public void execute(JobExecutionContext context) throws JobExecutionException {
      String jndiAddr = "java:comp/env/ejb/HelloBean";
      HelloLocal bean = (HelloLocal) new InitialContext().lookup(jndiAddr);
      System.out.println("response : " + bean.hello("Quartz"));
  }
}
  • JNDI lookup failure javax.naming.NameNotFoundException java:comp/UserTransaction, When openening an transaction inside a quartz job following exception is thrown:
Unable to lookup JNDI name [java:comp/UserTransaction]
...

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP)
    • 6.x

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase of over 48,000 articles and solutions.

Current Customers and Partners

Log in for full access

Log In