How to create a lifecycle listener for an application deployed to WebSphere Application Server?

I have an EAR application deployed to WebSphere. How can I create a lifecycle listener for an application that needs to call a one-time initialization code every time the application starts? I need something similar to the WebLogic Server classes weblogic.application.ApplicationLifeCyleListener and weblogic.application.ApplicationLifecycleEvent .

+4
source share
3 answers

The EJB 3.1 specification has been added to singleton session beans, which can be used to initialize an application in a portable, vendor-independent manner.

Quote from Designing a Singleton Beans Session , the following example illustrates a single-line bean session with initialization of a run using the @Startup annotation:

 @Singleton @Startup public class ConfigurationBean implements Configuration { @PostConstruct public void initialize() { // 1. Create the database table if it does not exist. // 2. Initialize settings from the database table. // 3. Load a cache. // 4. Initiate asynchronous work (for example, work to a messaging queue or to // calls to asynchronous session bean methods. } // ... } 

If you are using EJB 3.1, which is part of the Java EE 6 specification, this is the standard way to initialize the application. WebSphere 8 and 8.5 support this specification level.

If you are using an outdated version of WebSphere or a specification, and you do not like the update, you can use Startup Beans , the WebSphere extension used for this purpose in previous versions.

Also +1 for Udo's answer.

+6
source

I'm not sure if there is a lifecycle listener for websphere. However, you can create a dummy servlet that initializes at startup.

 <servlet> <display-name>YourServlet</display-name> <servlet-name>YourServlet</servlet-name> <servlet-class>com.example.YourServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>YourServlet</servlet-name> <url-pattern>/YourServlet</url-pattern> </servlet-mapping> 

You do not need to call this servlet. He will load himself.

+1
source
+1
source

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


All Articles