diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ActiveMQServer.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ActiveMQServer.java index 47e645ac137..7e5b24aa9f4 100644 --- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ActiveMQServer.java +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/ActiveMQServer.java @@ -276,6 +276,10 @@ enum SERVER_STATE { void registerBrokerPlugins(List plugins); + void afterStartBrokerPlugin(List plugins); + + void beforeStopBrokerPlugin(List plugins); + List getBrokerPlugins(); List getBrokerConnectionPlugins(); diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ActiveMQServerImpl.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ActiveMQServerImpl.java index cbea15b8037..590047f38a8 100644 --- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ActiveMQServerImpl.java +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ActiveMQServerImpl.java @@ -1260,6 +1260,11 @@ private void stop(boolean failoverOnServerShutdown, if (state == SERVER_STATE.STOPPED || state == SERVER_STATE.STOPPING) { return; } + + if (hasBrokerPlugins()) { + beforeStopBrokerPlugin(getBrokerPlugins()); + } + state = SERVER_STATE.STOPPING; ServerStatus.stopping(this); @@ -2671,6 +2676,16 @@ public void unRegisterBrokerPlugin(final ActiveMQServerBasePlugin plugin) { plugin.unregistered(this); } + @Override + public void afterStartBrokerPlugin(final List plugins) { + plugins.forEach(plugin -> plugin.afterStarted(this)); + } + + @Override + public void beforeStopBrokerPlugin(final List plugins) { + plugins.forEach(plugin ->plugin.beforeStopped(this)); + } + @Override public List getBrokerPlugins() { return configuration.getBrokerPlugins(); @@ -3644,6 +3659,11 @@ public void completeActivation(boolean replicated) throws Exception { getRemotingService().startAcceptors(); activationLatch.countDown(); callActivationCompleteCallbacks(); + + if (hasBrokerPlugins()) { + afterStartBrokerPlugin(getBrokerPlugins()); + } + } @Override diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/plugin/ActiveMQServerBasePlugin.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/plugin/ActiveMQServerBasePlugin.java index cc0483a3cee..d167d71c301 100644 --- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/plugin/ActiveMQServerBasePlugin.java +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/plugin/ActiveMQServerBasePlugin.java @@ -46,4 +46,21 @@ default void registered(ActiveMQServer server) { */ default void unregistered(ActiveMQServer server) { } + + /** + * The server has been started + * + * @param server The ActiveMQServer that has been started + */ + default void afterStarted(ActiveMQServer server) { + } + + /** + * The server is about to stop + * + * @param server The ActiveMQServer that is about to stop + */ + default void beforeStopped(ActiveMQServer server) { + } + } diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/plugin/MethodCalledVerifier.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/plugin/MethodCalledVerifier.java index f1e2a99bb3f..40368d4cb65 100644 --- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/plugin/MethodCalledVerifier.java +++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/plugin/MethodCalledVerifier.java @@ -36,6 +36,7 @@ import org.apache.activemq.artemis.core.postoffice.QueueBinding; import org.apache.activemq.artemis.core.postoffice.RoutingStatus; import org.apache.activemq.artemis.core.security.SecurityAuth; +import org.apache.activemq.artemis.core.server.ActiveMQServer; import org.apache.activemq.artemis.core.server.HandleStatus; import org.apache.activemq.artemis.core.server.MessageReference; import org.apache.activemq.artemis.core.server.Queue; @@ -111,6 +112,8 @@ public class MethodCalledVerifier implements ActiveMQServerPlugin { public static final String FEDERATED_QUEUE_CONDITIONAL_CREATE_CONSUMER = "federatedQueueConditionalCreateConsumer"; public static final String BEFORE_FEDERATED_QUEUE_CONSUMER_MESSAGE_HANDLED = "beforeFederatedQueueConsumerMessageHandled"; public static final String AFTER_FEDERATED_QUEUE_CONSUMER_MESSAGE_HANDLED = "afterFederatedQueueConsumerMessageHandled"; + public static final String AFTER_STARTED = "afterStarted"; + public static final String BEFORE_STOPPED = "beforeStopped"; public MethodCalledVerifier(Map methodCalls) { super(); @@ -474,6 +477,18 @@ public boolean federatedQueueConditionalCreateConsumer(ServerConsumer consumer) return true; } + @Override + public void afterStarted(ActiveMQServer server) { + Objects.requireNonNull(server); + methodCalled(AFTER_STARTED); + } + + @Override + public void beforeStopped(ActiveMQServer server) { + Objects.requireNonNull(server); + methodCalled(BEFORE_STOPPED); + } + public void validatePluginMethodsEquals(int count, String... names) { validatePluginMethodsEquals(count, Wait.MAX_WAIT_MILLIS, Wait.SLEEP_MILLIS); } diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/plugin/ServerBasePluginTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/plugin/ServerBasePluginTest.java new file mode 100644 index 00000000000..eba591a02c9 --- /dev/null +++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/plugin/ServerBasePluginTest.java @@ -0,0 +1,72 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.activemq.artemis.tests.integration.plugin; + +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.activemq.artemis.core.server.ActiveMQServer; +import org.apache.activemq.artemis.core.server.plugin.ActiveMQServerBasePlugin; +import org.apache.activemq.artemis.tests.util.ActiveMQTestBase; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class ServerBasePluginTest extends ActiveMQTestBase { + + private final MethodCalledVerifier verifier = new MethodCalledVerifier(); + + @Override + @BeforeEach + public void setUp() throws Exception { + super.setUp(); + } + + @Test + public void testBasePluginLifecycleNotification() throws Exception { + AtomicInteger count = new AtomicInteger(0); + + ActiveMQServer server = createServer(false, createDefaultInVMConfig()); + server.getConfiguration().registerBrokerPlugin(verifier); + + server.getConfiguration().registerBrokerPlugin(new ActiveMQServerBasePlugin() { + @Override + public void afterStarted(ActiveMQServer server) { + assertTrue(server.isStarted()); + count.getAndIncrement(); + } + + @Override + public void beforeStopped(ActiveMQServer server) { + assertTrue(server.isStarted()); + count.getAndIncrement(); + } + }); + + verifier.validatePluginMethodsEquals(0, MethodCalledVerifier.AFTER_STARTED, MethodCalledVerifier.BEFORE_STOPPED); + server.start(); + verifier.validatePluginMethodsEquals(1, 1000L, 10L, MethodCalledVerifier.AFTER_STARTED); + verifier.validatePluginMethodsEquals(0, 1000L, 10L, MethodCalledVerifier.BEFORE_STOPPED); + server.stop(); + verifier.validatePluginMethodsEquals(1, 1000L, 10L, MethodCalledVerifier.AFTER_STARTED, MethodCalledVerifier.BEFORE_STOPPED); + assertEquals(2, count.get()); + + } + +}