Controlling Order Of Service Shutdown
jim_b_o Aug 22, 2013 2:51 PMI have a @Singleton @Startup EJB that I use to bootstrap some application specific services, e.g. an outbound ResourceAdapter. As far as possible I use CDI to define/inject dependencies. During an orderly shutdown (e.g. via Ctrl-C) I want to complete any pending processing, e.g. cease sending outbound requests but wait up to some predetermined time for any pending responses. I do this processing in the @PreDestroy method of the aforementioned EJB. However, as shown in the log snippet below, a number of the services that I depend on are stopped either before the @PreDestroy callback or in parallel shortly thereafter.
19:12:48,734 INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-2) JBAS010409: Unbound data source [java:/com.XXX]
19:12:48,737 INFO [org.jboss.as.connector.deployment] (MSC service thread 1-2) JBAS010410: Unbound JCA ConnectionFactory [java:/JmsXA]
19:12:48,737 INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-12) JBAS010409: Unbound data source [java:jboss/datasources/ExampleDS]
19:12:48,738 INFO [org.wildfly.extension.undertow] (MSC service thread 1-2) JBAS018224: Unregister web context: /hornetq-server
19:12:48,740 INFO [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-13) JBAS010418: Stopped Driver service with driver-name = h2
19:12:48,740 INFO [org.jboss.as.messaging] (ServerService Thread Pool -- 63) JBAS011605: Unbound messaging object to jndi name java:global/jms/XXX
19:12:48,741 INFO [org.wildfly.extension.undertow] (MSC service thread 1-11) Stopping host default-host
19:12:48,742 INFO [org.jboss.as.messaging] (ServerService Thread Pool -- 63) JBAS011605: Unbound messaging object to jndi name java:jboss/exported/jms/XXX
19:12:48,744 INFO [com.XXX] (ServerService Thread Pool -- 73) ----- @PreDestroy -----
19:12:48,745 INFO [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-5) JBAS010418: Stopped Driver service with driver-name = jtds-driver_net.sourceforge.jtds.jdbc.Driver_1_3
19:12:48,745 INFO [org.jboss.as.messaging] (ServerService Thread Pool -- 72) JBAS011605: Unbound messaging object to jndi name java:global/jms/XXX
19:12:48,756 INFO [org.jboss.as.server.deployment] (MSC service thread 1-3) JBAS015877: Stopped deployment jtds-driver (runtime-name: jtds-driver) in 23ms
19:12:48,762 INFO [org.hornetq.ra] (MSC service thread 1-11) HQ151003: HornetQ resource adaptor stopped
I would appreciate any advice on how to control this shutdown ordering. I need DataSources, HornetQ, custom RA etc to remain active until the main EJB @PreDestroy method completes.
The lack of symmetry in service start versus stop ordering seems a little dangerous.
I've used a similar approach on JBoss 4.2.3.GA in the past using the init() / destroy() methods on a Servlet and <load-on-startup> to provide the bootstrap and shutdown hooks with no issues. Maybe I'm assuming too much by expecting @Startup to express that @PreDestroy becomes an equivalent of the non-existent @Shutdown.