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
.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With