Executing task after deployment of Java EE application

后端 未结 4 1061
天命终不由人
天命终不由人 2021-02-04 04:52

I have a Java EE application which should start a synchronization process with an external system once after its deployment.

How could I implement this requirement?

相关标签:
4条回答
  • 2021-02-04 05:07

    Below are listed a couple of popular methods for getting lifecycle callbacks in JavaEE apps.

    Create a javax.servlet.ServletContextListener implementation

    If you have a web component to your .ear file (embedded .war) or your deployment is a .war by itself you can add a ServletContextListener to your web.xml and get a callback when the server starts or is shutting down.

    Example:

    package com.stackoverflow.question
    
    import javax.servlet.ServletContextListener;
    import javax.servlet.ServletContextEvent;
    
    public class MyServletContextListener implements ServletContextListener{
    
       @Override
       public void contextInitialized(ServletContextEvent contextEvent) {
            /* Do Startup stuff. */
       }
    
       @Override
       public void contextDestroyed(ServletContextEvent contextEvent) {
            /* Do Shutdown stuff. */
       }
    
    }
    

    and then add this configuration to your web.xml deployment descriptor.
    $WAR_ROOT/WEB-INF/web.xml.

    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee">
    
        <listener>
          <listener-class>com.stackoverflow.question.MyServletContextListener</listener-class>
        </listener>
    
    </web-app>
    

    Create an EJB 3.1 @Startup Bean

    This method uses an EJB 3.1 singleton to get a startup and shutdown callback from the server.

    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    import javax.ejb.Startup;
    import javax.ejb.Singleton;
    
    @Singleton
    @Startup
    public class LifecycleBean {
    
      @PostConstruct
      public void init() {
        /* Startup stuff here. */
      }
    
      @PreDestroy
      public void destroy() {
        /* Shutdown stuff here */
      }
    
    }
    
    0 讨论(0)
  • 2021-02-04 05:09

    You can use the @Startup and @PostConstruct annotations to perform tasks on application startup.

    0 讨论(0)
  • 2021-02-04 05:11

    I tested the suggested solution which uses the @Startup and @PostConstruct annotations. It turned out that Glassfish does not complete the deployment of an application until all methods annotated with @PostConstruct have finished. So in my case the deployment would take from several minutes up to an hour.

    But I figured out a different way to achive what I want. The best solution seems to be a timer callback method which cancels its timer after its execution.

    @Stateless
    public class SynchronisationService {
        @Schedule(hour = "*", minute = "*", persistent = false)
        protected void init(Timer timer)
        {
           doTheSync();
    
           timer.cancel();
        }
     }
    

    Using a non-persistent timer allows the timer to be re-created if the application server is restarted.

    0 讨论(0)
  • 2021-02-04 05:17

    Using a ServletContextListener, or a servlet that is initialized at startup, for example. Of course, this becomes much harder if you have multiple deployments of the application in a cluster, and only want this process to be run once.

    0 讨论(0)
提交回复
热议问题