Search in sources :

Example 1 with ActivateCallback

use of org.apache.activemq.artemis.core.server.ActivateCallback in project wildfly by wildfly.

the class JMSService method doStart.

private synchronized void doStart(final StartContext context) throws StartException {
    final ServiceContainer serviceContainer = context.getController().getServiceContainer();
    ClassLoader oldTccl = WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(getClass());
    try {
        jmsServer = new JMSServerManagerImpl(activeMQServer.getValue(), new WildFlyBindingRegistry(context.getController().getServiceContainer()));
        activeMQServer.getValue().registerActivateCallback(new ActivateCallback() {

            private volatile ServiceController<Void> activeMQActivationController;

            public void preActivate() {
            }

            public void activated() {
                if (overrideInVMSecurity) {
                    activeMQServer.getValue().getRemotingService().allowInvmSecurityOverride(new ActiveMQPrincipal(DefaultCredentials.getUsername(), DefaultCredentials.getPassword()));
                }
                //   ActiveMQ backup server is activated during failover while the WildFly server is shutting down.
                if (serviceContainer.isShutdown()) {
                    return;
                }
                if (activeMQActivationController == null) {
                    activeMQActivationController = serviceContainer.addService(ActiveMQActivationService.getServiceName(serverServiceName), new ActiveMQActivationService()).setInitialMode(Mode.ACTIVE).install();
                } else {
                    activeMQActivationController.setMode(ACTIVE);
                }
            }

            @Override
            public void activationComplete() {
            }

            public void deActivate() {
                // and *not* during AS7 service container shutdown or reload (AS7-6840 / AS7-6881)
                if (activeMQActivationController != null) {
                    if (!activeMQActivationController.getState().in(STOPPING, REMOVED)) {
                        // [WFLY-4597] When Artemis is deactivated during failover, we block until its
                        // activation controller is REMOVED before giving back control to Artemis.
                        // This allow to properly stop any service depending on the activation controller
                        // and avoid spurious warning messages because the resources used by the services
                        // are stopped outside the control of the services.
                        final CountDownLatch latch = new CountDownLatch(1);
                        activeMQActivationController.compareAndSetMode(ACTIVE, REMOVE);
                        activeMQActivationController.addListener(new AbstractServiceListener<Void>() {

                            @Override
                            public void transition(ServiceController<? extends Void> controller, ServiceController.Transition transition) {
                                if (transition.enters(REMOVED)) {
                                    latch.countDown();
                                }
                            }
                        });
                        try {
                            latch.await(5, TimeUnit.SECONDS);
                        } catch (InterruptedException e) {
                        }
                        activeMQActivationController = null;
                    }
                }
            }
        });
        jmsServer.start();
    } catch (StartException e) {
        throw e;
    } catch (Throwable t) {
        throw MessagingLogger.ROOT_LOGGER.failedToStartService(t);
    } finally {
        WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(oldTccl);
    }
}
Also used : ActiveMQPrincipal(org.apache.activemq.artemis.core.security.ActiveMQPrincipal) AbstractServiceListener(org.jboss.msc.service.AbstractServiceListener) CountDownLatch(java.util.concurrent.CountDownLatch) ServiceContainer(org.jboss.msc.service.ServiceContainer) ActiveMQActivationService(org.wildfly.extension.messaging.activemq.ActiveMQActivationService) JMSServerManagerImpl(org.apache.activemq.artemis.jms.server.impl.JMSServerManagerImpl) ServiceController(org.jboss.msc.service.ServiceController) StartException(org.jboss.msc.service.StartException) ActivateCallback(org.apache.activemq.artemis.core.server.ActivateCallback)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)1 ActiveMQPrincipal (org.apache.activemq.artemis.core.security.ActiveMQPrincipal)1 ActivateCallback (org.apache.activemq.artemis.core.server.ActivateCallback)1 JMSServerManagerImpl (org.apache.activemq.artemis.jms.server.impl.JMSServerManagerImpl)1 AbstractServiceListener (org.jboss.msc.service.AbstractServiceListener)1 ServiceContainer (org.jboss.msc.service.ServiceContainer)1 ServiceController (org.jboss.msc.service.ServiceController)1 StartException (org.jboss.msc.service.StartException)1 ActiveMQActivationService (org.wildfly.extension.messaging.activemq.ActiveMQActivationService)1