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?
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?
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.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.
Using a non-persistent timer allows the timer to be re-created if the application server is restarted.
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 yourweb.xml
and get a callback when the server starts or is shutting down.Example:
and then add this configuration to your
web.xml
deployment descriptor.$WAR_ROOT/WEB-INF/web.xml
.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.
You can use the
@Startup
and@PostConstruct
annotations to perform tasks on application startup.