Migrate Oracle WebLogic Server ApplicationLifecycleListener Code to Red Hat JBoss Enterprise Application Platform 6 or 7

Updated -

Summary

The WebLogic ApplicationLifecycleListener abstract class is used to perform functions or schedule jobs at Oracle WebLogic Server start and stop.

WebLogic ApplicationLifecycleListener Methods

The following table shows the four stages in the application lifecycle available for listener registration in Oracle WebLogic Server.

WebLogic ApplicationLifecycleListener Methods

WebLogic Method Description
void preStart(ApplicationLifecycleEvent evt Provides hooks for listeners before the application finishes initialization.
void postStart(ApplicationLifecycleEvent evt) Provides hooks for listeners after the application finishes initialization.
void preStop(ApplicationLifecycleEvent evt) Provides hooks for listeners when the application begins the shutdown process.
void postStop(ApplicationLifecycleEvent evt) Provides hooks for listeners after the application ends the shutdown process.

Replace Application Start and Stop Events

In JBoss Enterprise Application Platform, there is no equivalent to intercept the preStart or preStop processing, but you can use one of the following methods to achieve results similar to the postStart and postStop methods.

Replace the Events Using a ServletContextListener

The ServletContextListener class provides two methods that are analoguous to postStart() and postStop() methods in the WebLogic ApplicationLifecycleListener class. Because the code must access the context root of the application using the ServletContext, the servlet must be deployed to a WAR file. The following is an example of code that uses the ServletContextListener to perform tasks after application server start and stop.

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class ContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent evt)  {
        ServletContext ctx = evt.getServletContext();
        System.out.println("contextInitialized(): ServerInfo: " + 
            ctx.getServerInfo() + " " + System.currentTimeMillis());
        System.out.println("contextInitialized(): ContextPath: " + 
            ctx.getContextPath() + " " + System.currentTimeMillis());
    }

    @Override
    public void contextDestroyed(ServletContextEvent evt)  {
        ServletContext ctx = evt.getServletContext();
        System.out.println("contextDestroyed(): ServerInfo: " + 
            ctx.getServerInfo() + " " + System.currentTimeMillis());
        System.out.println("contextDestroyed(): ContextPath: " + 
            ctx.getContextPath() + " " + System.currentTimeMillis());
    }
}

Replace the Events Using a Singleton Stateless Session Bean

Create a singleton bean using the @Singleton annotation. Use the @Startup annotation to tell the container to initialize the singleton session bean at application start. Use the @PostConstruct annotation to specify the method to invoke at the start of the application lifecyle and the @PostDestroy annotation to specify the method to invoke at the end of the application life cycle. The following is an example of a stateless session bean:

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Singleton;
import javax.ejb.Startup;

@Startup
@Singleton
public class StartupBean
{
    @PostConstruct
    void startup() {
        System.out.println("startup()", "@PostContruct called");
    }

    @PreDestroy
    void shutdown() {
        System.out.println("shutdown()", "@PreDestroy called");
    }
}