ObjectName jbossServerName = new ObjectName("jboss.system:type=Server"); MBeanServer jbossServer = MBeanServerLocator.locateJBoss(); jbossServer.addNotificationListener(jbossServerName, new ShutdownListener(), null, null);
Shutdown code should stop some RHQ services gracefully.
Currently running Quartz jobs should be able to finish.
This is something important as job code may not be resilient to interrupts
The server should be switched to maintenance mode.
The start script that RHQ server ships sends a TERM signal every two seconds as long as the server remains up.
The shutdown code was executed in a JMX notification listener. On startup, we registered the ShutdownListener:
ObjectName jbossServerName = new ObjectName("jboss.system:type=Server"); MBeanServer jbossServer = MBeanServerLocator.locateJBoss(); jbossServer.addNotificationListener(jbossServerName, new ShutdownListener(), null, null);
Then the ShutdownListener would react on the STOP_NOTIFICATION event:
if (org.jboss.system.server.Server.STOP_NOTIFICATION_TYPE.equals(notification.getType())) // Do shutdown }
No component was undeployed before the ShutdownListener was fully executed.
The ShutdownListener has been turned into a @Singleton @Startup bean. The shutdown code is inside its @PreDestroy. This implementation has some issues.
When the shutdown code is executed, there is no guarantee that all our beans will still be deployed. The Wildfly team suggested to use @DependsOn annotation to declare dependencies of the ShutdownListener. This is doable for the few dependencies needed to switch the server to maintenance mode. But the Quartz jobs still running during shutdown could virtually depend on any RHQ bean. Obviously we cannot manually add all RHQ beans as dependencies of the ShutdownListener.
Some of RHQ's EJBs require RHQ's MXBeans (like CoreServer) to be deployed. MXBeans are declared in the services SAR. There is no guarantee that MXBeans will still be deployed while the shutdown code is executed.
Besides, the SchedulerService MXBean shutdowns the Quartz scheduler when it is stopped, without waiting for running jobs to complete. So the Quartz scheduler could even be stopped before the ShutdownListener code is executed.
The Quartz scheduler uses the NoTxRHQDS to persist its state to the database. But there is no guarantee that the NoTxRHQDS will still be deployed while the scheduler is shutdown.
This will prevent the NoTxRHQDS from being undeployed before the Quartz scheduler finishes to gracefully shutdown.
The MXBeans classes are already part of the the server EJBs JAR. @Singleton beans will get all features of session beans (injection, dependencies... etc). They will still be exposed via JMX for backward compatibility (registration to the MBeanServer in the @PostConstruct method).
The services SAR module will be deleted.
Add a mark to RHQ EJB JAR deployment (see Capedwarf example)
Inside the RHQ EJB JAR deployment, make a "special" @Singleton (maybe annotated with a new @RhqLifecycleManager annotation) automatically depend on all over beans (see Wildfly EjbDependsOnMergingProcessor)
As a result, the @PostConstruct method of the @RhqLifecycleManager annotated bean would be called only when every other bean is deployed, and the @PreDestroy method before any other bean is undeployed.
These deployment processors would be part of a new RHQ AS7 extension which the RHQ installer would setup.