Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@ enum SERVER_STATE {

void registerBrokerPlugins(List<ActiveMQServerBasePlugin> plugins);

void afterStartBrokerPlugin(List<ActiveMQServerBasePlugin> plugins);

void beforeStopBrokerPlugin(List<ActiveMQServerBasePlugin> plugins);

List<ActiveMQServerBasePlugin> getBrokerPlugins();

List<ActiveMQServerConnectionPlugin> getBrokerConnectionPlugins();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -2671,6 +2676,16 @@ public void unRegisterBrokerPlugin(final ActiveMQServerBasePlugin plugin) {
plugin.unregistered(this);
}

@Override
public void afterStartBrokerPlugin(final List<ActiveMQServerBasePlugin> plugins) {
plugins.forEach(plugin -> plugin.afterStarted(this));
}

@Override
public void beforeStopBrokerPlugin(final List<ActiveMQServerBasePlugin> plugins) {
plugins.forEach(plugin ->plugin.beforeStopped(this));
}

@Override
public List<ActiveMQServerBasePlugin> getBrokerPlugins() {
return configuration.getBrokerPlugins();
Expand Down Expand Up @@ -3644,6 +3659,11 @@ public void completeActivation(boolean replicated) throws Exception {
getRemotingService().startAcceptors();
activationLatch.countDown();
callActivationCompleteCallbacks();

if (hasBrokerPlugins()) {
afterStartBrokerPlugin(getBrokerPlugins());
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, AtomicInteger> methodCalls) {
super();
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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());

}

}