3

I am working on Jersey based JAX-RS2 app running on Websphere 8.5. I am using async feature to spawn a new thread. The issue is that the new thread is not getting the Java EE context required for jndi lookups. The error that I get is:

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.

There is feature in Java EE 7 for ManagedExecutorService that can be configured in websphere. I am not able to use that as Websphere 8.5 supports Java EE 6 only. I am not able to do the lookups in advance as there are third party jars included that need the Java EE context to work.

I want to propagate Java EE context to the newly spawnned thread. Please suggest if that is possible.

Bruno Borges
  • 853
  • 7
  • 24
pawinder gupta
  • 1,225
  • 16
  • 35
  • 1
    It isn't possible. Also the word is spawn (and spawned), not span and spanned (unless you're talking about a bridge, I'm told there's one outside a' Moosejaw). – Elliott Frisch May 22 '15 at 02:42
  • I don't know what is your project timeframe, but maybe you could consider [WebSphere Liberty Beta](http://www-01.ibm.com/support/knowledgecenter/was_beta_liberty/com.ibm.websphere.wlp.nd.multiplatform.doc/ae/rwlp_feat.html) for now. It has support for JAX-RS 2.0, so you dont need external Jersey and support for ManagedExecutorService via concurrent-1.0 feature. – Gas May 22 '15 at 10:41
  • 1
    And for full WebSphere 8.5.5 you could consider instead of spawning new thread use session EJB with async invocation. See [Configuring EJB 3.1 session bean methods to be asynchronous](http://www-01.ibm.com/support/knowledgecenter/SSAW57_8.5.5/com.ibm.websphere.nd.doc/ae/tejb_dvamethods.html?cp=SSAW57_8.5.5%2F1-3-0-11-1-4&lang=en) – Gas May 22 '15 at 10:45
  • Thanks Gas for the suggestions. I was able to achieve this by configuring a work manager in Websphere App Server. – pawinder gupta May 27 '15 at 04:30

1 Answers1

2

It is possible to submit the task to a new thread having J2EE context by creating a Work Manager in WebSphere Application Server. For WAS 8 and above the WorkManager that is available in the full profile is now also an ExecutorService. The steps for this are:

  1. On WAS admin console goto Resources -->Asynchronous Beans--> Work manager and created a new work manager with jndi name wm/myWM.
  2. In java code do a jndi lookup for the work manager.

    ExecutorService execService = (ExecutorService) initialContext.lookup("wm/myWM");

  3. Submit the task to Executor Service.

    execService.submit(new AsyncJob(inputData, asyncResponse));

On Websphere Liberty profile, this can be configured as managedExecutorService. Following additions are required in server.xml

 <feature>concurrent-1.0</feature> 

<managedExecutorService jndiName="wm/myWM">
    <contextService>
        <jeeMetadataContext/>
        <classloaderContext/>
        <securityContext/>
    </contextService>
</managedExecutorService>

More details are in the pdf at this link: ManagedService WAS 8.5

pawinder gupta
  • 1,225
  • 16
  • 35