How to access EJB bean through context lookup from ServletContextListener
Asked Answered
A

1

6

Need to call a EJB service from the servlet context listener's contextInitialized() method. Application is running on JBOSS, though the context listener works fine, I'm not able to access the EJB bean through JNDI look up.

Because the web deployment in JBOSS happens before EJB beans are bound with JNDI tree. How to overcome from this? Is there a way to configure JNDI bind early or start the web deployment later once EJB's are completely deployed?

I had put Thread.sleep() before the service call in the contextInitialized() method, it is working fine in my JBoss5.1.0 GA, and the same did not work in other machines JBoss of same version.

Applications needs this because, we want to load some master data from the DB and make it available in the web layer (kind of caching). Does JBOSS startupmbean suit this requirement? If yes how can I make the data available to web layer?

Also if any alternative ways are available, please suggest.

Allegorical answered 19/6, 2014 at 7:6 Comment(0)
G
1
  • Poll for the EJBs in contextInitialized(). So instead of just sleeping for a certain time, try to connect to the EJB. If that fails, sleep, and retry, until the EJBs are available. In this case the context initialization is blocked.

  • Implement the cache as a lazy one: Fill the cache during the first query (and use the same polling procedure: connect to the EJB, retry until it becomes available). In this case the cache blocks.

  • You could split your deployment into two parts: One for the EJBs, one for the web application. Then deploy the first, and delay deployment of the web application until the EJBs are bound (either by watching the log file or by trying bind to the EJB from a command line app)

Greaten answered 24/6, 2014 at 15:22 Comment(2)
Is there a way to do delay the deployment of web? Where EJB and WEB are bundeled in one EARAllegorical
I don't know, if this is possible. There is an implicit dependency between a war module and an EJB module in the same .ear file; I have not found a way neither in application.xml nor in jboss-app.xml to define serial loading of modules. Of course you can have a look into the JBoss source code. Somehow I consider this to be a JBoss bug; a web module should not be loaded before all EJBs are deployed, so if you can't apply one of the workarounds, you could file a bug.Greaten

© 2022 - 2024 — McMap. All rights reserved.