Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have an application EAR deployed to WebSphere. How can I create a life cycle listener for the application that should invoke a one-time initialization code every time the application is launched? I need to have something similar to WebLogic Server's classes weblogic.application.ApplicationLifeCyleListener and weblogic.application.ApplicationLifecycleEvent.

like image 304
Ayush Jindal Avatar asked Dec 26 '22 16:12

Ayush Jindal


2 Answers

EJB 3.1 specification added singleton session beans which may be used for application initialization in a portable, vendor-independent way.

Quoting from Developing Singleton Session Beans , the following example illustrates a singleton session bean with startup initialization using @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 Java EE 6 spec, this is the standard way of application initialization. WebSphere 8 and 8.5 support this specification level.

If you are using an older version of WebSphere or specification and you don't feel like upgrading, you may then use Startup Beans, a WebSphere extension used for this purpose in previous versions.

Also +1 to Udo's answer.

like image 127
Kurtcebe Eroglu Avatar answered Mar 23 '23 13:03

Kurtcebe Eroglu


I'm not sure if there is a lifecycle listener for websphere. However you can create a dummy servlet that is initialized on 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 don't need to call that servlet. It will load itself.

like image 23
Udo Held Avatar answered Mar 23 '23 11:03

Udo Held