public void stop()

in activemq-broker/src/main/java/org/apache/activemq/broker/BrokerService.java [810:924]


    public void stop() throws Exception {
        final ServiceStopper stopper = new ServiceStopper();

        //The preShutdownHooks need to run before stopping.compareAndSet()
        //so there is a separate AtomicBoolean so the hooks only run once
        //We want to make sure the hooks are run before stop is initialized
        //including setting the stopping variable - See AMQ-6706
        if (preShutdownHooksInvoked.compareAndSet(false, true)) {
            for (Runnable hook : preShutdownHooks) {
                try {
                    hook.run();
                } catch (Throwable e) {
                    stopper.onException(hook, e);
                }
            }
        }

        if (!stopping.compareAndSet(false, true)) {
            LOG.trace("Broker already stopping/stopped");
            return;
        }

        setStartException(new BrokerStoppedException("Stop invoked"));
        MDC.put("activemq.broker", brokerName);

        if (systemExitOnShutdown) {
            new Thread() {
                @Override
                public void run() {
                    System.exit(systemExitOnShutdownExitCode);
                }
            }.start();
        }

        LOG.info("Apache ActiveMQ {} ({}, {}) is shutting down", new Object[]{ getBrokerVersion(), getBrokerName(), brokerId} );

        removeShutdownHook();
        if (this.scheduler != null) {
            this.scheduler.stop();
            this.scheduler = null;
        }
        if (services != null) {
            for (Service service : services) {
                stopper.stop(service);
            }
        }
        stopAllConnectors(stopper);
        this.slave = true;
        // remove any VMTransports connected
        // this has to be done after services are stopped,
        // to avoid timing issue with discovery (spinning up a new instance)
        BrokerRegistry.getInstance().unbind(getBrokerName());
        VMTransportFactory.stopped(getBrokerName());
        if (broker != null) {
            stopper.stop(broker);
            broker = null;
        }

        if (jobSchedulerStore != null) {
            jobSchedulerStore.stop();
            jobSchedulerStore = null;
        }
        if (tempDataStore != null) {
            tempDataStore.stop();
            tempDataStore = null;
        }
        try {
            stopper.stop(getPersistenceAdapter());
            persistenceAdapter = null;
            if (isUseJmx()) {
                stopper.stop(managementContext);
                managementContext = null;
            }
            // Clear SelectorParser cache to free memory
            SelectorParser.clearCache();
        } finally {
            started.set(false);
            stopped.set(true);
            stoppedLatch.countDown();
        }

        if (this.taskRunnerFactory != null) {
            this.taskRunnerFactory.shutdown();
            this.taskRunnerFactory = null;
        }
        if (this.executor != null) {
            ThreadPoolUtils.shutdownNow(executor);
            this.executor = null;
        }

        this.destinationInterceptors = null;
        this.destinationFactory = null;

        if (startDate != null) {
            LOG.info("Apache ActiveMQ {} ({}, {}) uptime {}", new Object[]{ getBrokerVersion(), getBrokerName(), brokerId, getUptime()});
        }
        LOG.info("Apache ActiveMQ {} ({}, {}) is shutdown", new Object[]{ getBrokerVersion(), getBrokerName(), brokerId});

        synchronized (shutdownHooks) {
            for (Runnable hook : shutdownHooks) {
                try {
                    hook.run();
                } catch (Throwable e) {
                    stopper.onException(hook, e);
                }
            }
        }

        MDC.remove("activemq.broker");

        // and clear start date
        startDate = null;

        stopper.throwFirstException();
    }