How to spawn a new thread with in web Container, that has access to JNDI resources defined on web container?

phanirv2
phanirv2 used Ask the Experts™
on
Hi,

My requirement is do some background process (saving to DB) when user performs a request.
However, it has to be asynchronous. So I forked a separate thread with in my servlet code, using
java.util.concurrent.ExecutorService interface. The run(...) of this thread, basically does a JNDI lookup
for my session bean to perform the DB operation.

Problem: The lookup is failing with NamingException : java:comp/env not found. The thread executing the look
up is not part of container process.

javaURLContex E   NMSV0310E: A JNDI operation on a "java:" name cannot be completed because the server runtime is not able to associate the operation's thread with any J2EE application component. This condition can occur when the JNDI client using the "java:" name is not executed on the thread of a server application request. Make sure that a J2EE application does not execute JNDI operations on "java:" names within static code blocks or in threads created by that J2EE 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:" names.

How would I make sure that the new thread I spun can be able to lookup resources?




Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Your class should implement javax.servlet.ServletRequestListener to be notified every time the request comes in.

This interface has method:

    public void requestInitialized(ServletRequestEvent rre)

and ServletRequestEvent has these two methods:

    public ServletRequest getServletRequest()
    public ServletContext getServletContext()
there is no "legal" way to open a thread on a J2EE container. if you want to stick to specs, your options are as follows:
1 - use JMS. have your servlet fire an JMS messege to a queue, and have a listener perform all the "heavy lifting" - this will be done asynchronously (you can have an MDB listening to a JMS queue)
2 - if you want to run your own threads - look at the JCA (Java COnnector Architecture) - JCA Connectors have access to the application server's thread pool.

about your JNDI lookup - the java:comp/env JNDI namespace is only accessible within the same context. your thread is not an application-server managed object and is therefore not part of he context.
check to see if your resources are bound to a lobal JNDI name as well as a context-specific one. this depends on the application server youre deploying to (JBoss, for instance, binds EJBs under /[ear-name]/[bean class name]/[remote | local]

Author

Commented:
I have used commonj Woker thread , so that it will be more generic. I did configure jbosscommonj1.0 for dev environment, and common J on WAS RT for my prod.

Thanks for the link.
HonorGodSoftware Engineer

Commented:
Thanks for the grade & points.

Good luck & have a great day.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial