Application lifecycle listeners in EAP 6 & 7

Solution Verified - Updated

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP)
    • 6, 7

Issue

  • Does EAP have application lifecycle listeners ?
  • What methods in EAP are equivalent to WebLogic ApplicationLifecycleListener?

Resolution

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

1) Create a ServletContextListener

The ServletContextListener class provides two methods that are analoguous to postStart() and postStop() methods in the WebLogic ApplicationLifecycleListener class. Since 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());
    }
}

Add the following to web.xml in the WEB-INF directory of your war file

  <listener>
        <listener-class>ContextListener</listener-class>
    </listener>
2) Create 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");
    }
}
Components
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.