How to access an EJB bean through a contextual search from a ServletContextListener

You must call the EJB service from the servlet context listener method contextInitialized() . The application runs on JBOSS, although the context listener is working fine, I cannot access the EJB bean through JNDI lookup.

Since web deployment in JBOSS happens before the EJB beans are associated with the JNDI tree. How to overcome this? Is there a way to configure a JNDI connection earlier or start a web deployment later when the EJBs are fully deployed?

I sent Thread.sleep() before calling the service in the contextInitialized() method, it works fine in my JBoss5.1.0 GA, and the same doesn't work on other JBoss machines of the same version.

Applications need this because we want to download some basic data from the database and make it available in the web layer (type of caching). Does JBOSS startupmbean meet this requirement? If so, how can I make the data available to the web layer?

Also, if there are alternative ways, please suggest.

+6
source share
1 answer
  • Poll for EJB in contextInitialized() . So instead of just sleeping for a while, try connecting to EJB. If this fails, sleep and try again until EJB is available. In this case, context initialization is blocked.

  • Implement the cache as lazy: fill the cache during the first request (and use the same polling procedure: connect to the EJB, try again until it becomes available). In this case, the cache is blocked.

  • You can split the deployment into two parts: one for EJB, one for web application. Then deploy the first and tighten the deployment of the web application until the EJBs are connected (either by viewing the log file, or by trying to bind to the EJB from the command line application).

+1
source

Source: https://habr.com/ru/post/971033/


All Articles