Skip to content

Commit

Permalink
ARTEMIS-5141 Add or remove plugins on configuration reload
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonRoskvist committed Nov 1, 2024
1 parent 9f1681a commit 3a9ae21
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1365,6 +1365,11 @@ default boolean isJDBC() {
*/
void registerBrokerPlugin(ActiveMQServerBasePlugin plugin);

/**
* @param plugin
*/
void registerConfiguredBrokerPlugin(ActiveMQServerBasePlugin plugin);

/**
* @param plugin
*/
Expand All @@ -1375,6 +1380,11 @@ default boolean isJDBC() {
*/
List<ActiveMQServerBasePlugin> getBrokerPlugins();

/**
* @return
*/
List<ActiveMQServerBasePlugin> getConfiguredBrokerPlugins();

/**
* @return
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ public class ConfigurationImpl implements Configuration, Serializable {
private MetricsConfiguration metricsConfiguration = null;

private final List<ActiveMQServerBasePlugin> brokerPlugins = new CopyOnWriteArrayList<>();
private final List<ActiveMQServerBasePlugin> configuredBrokerPlugins = new CopyOnWriteArrayList<>();
private final List<ActiveMQServerConnectionPlugin> brokerConnectionPlugins = new CopyOnWriteArrayList<>();
private final List<ActiveMQServerSessionPlugin> brokerSessionPlugins = new CopyOnWriteArrayList<>();
private final List<ActiveMQServerConsumerPlugin> brokerConsumerPlugins = new CopyOnWriteArrayList<>();
Expand Down Expand Up @@ -2270,6 +2271,14 @@ public MetricsConfiguration getMetricsConfiguration() {
return this.metricsConfiguration;
}

@Override
public void registerConfiguredBrokerPlugin(final ActiveMQServerBasePlugin plugin) {
if (!configuredBrokerPlugins.contains(plugin)) {
configuredBrokerPlugins.add(plugin);
registerBrokerPlugin(plugin);
}
}

@Override
public void registerBrokerPlugins(final List<ActiveMQServerBasePlugin> plugins) {
plugins.forEach(plugin -> registerBrokerPlugin(plugin));
Expand Down Expand Up @@ -2321,7 +2330,9 @@ public void registerBrokerPlugin(final ActiveMQServerBasePlugin plugin) {

@Override
public void unRegisterBrokerPlugin(final ActiveMQServerBasePlugin plugin) {
configuredBrokerPlugins.remove(plugin);
brokerPlugins.remove(plugin);

if (plugin instanceof ActiveMQServerConnectionPlugin) {
brokerConnectionPlugins.remove(plugin);
}
Expand Down Expand Up @@ -2365,6 +2376,11 @@ public List<ActiveMQServerBasePlugin> getBrokerPlugins() {
return brokerPlugins;
}

@Override
public List<ActiveMQServerBasePlugin> getConfiguredBrokerPlugins() {
return configuredBrokerPlugins;
}

// for properties type inference
public void addBrokerPlugin(ActiveMQServerBasePlugin type) {
registerBrokerPlugin(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ private void parseBrokerPlugins(final Element e, final Configuration config) {
NodeList list = node.getElementsByTagName(BROKER_PLUGIN_ELEMENT_NAME);
for (int i = 0; i < list.getLength(); i++) {
ActiveMQServerPlugin plugin = parseActiveMQServerPlugin(list.item(i));
config.registerBrokerPlugin(plugin);
config.registerConfiguredBrokerPlugin(plugin);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4657,6 +4657,7 @@ private void reloadConfigurationFile(URL uri) throws Exception {
configuration.parseProperties(propertiesFileUrl);
updateStatus(ServerStatus.CONFIGURATION_COMPONENT, configuration.getStatus());
deployReloadableConfigFromConfiguration();
deployConfiguredBrokerPlugins(config);
}
}

Expand Down Expand Up @@ -4788,6 +4789,26 @@ private void deployReloadableConfigFromConfiguration() throws Exception {
}
}

private void deployConfiguredBrokerPlugins(Configuration newConfiguration) {
ActiveMQServerLogger.LOGGER.reloadingConfiguration("broker plugins");

if (hasBrokerPlugins() || !newConfiguration.getConfiguredBrokerPlugins().isEmpty()) {

for (ActiveMQServerBasePlugin plugin : configuration.getConfiguredBrokerPlugins()) {
if (!newConfiguration.getConfiguredBrokerPlugins().contains(plugin)) {
unRegisterBrokerPlugin(plugin);
}
}

for (ActiveMQServerBasePlugin plugin : newConfiguration.getConfiguredBrokerPlugins()) {
if (!configuration.getConfiguredBrokerPlugins().contains(plugin)) {
configuration.registerConfiguredBrokerPlugin(plugin);
}
}

}
}

public Set<ActivateCallback> getActivateCallbacks() {
return activateCallbacks;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
import org.apache.activemq.artemis.core.server.cluster.impl.RemoteQueueBindingImpl;
import org.apache.activemq.artemis.core.server.embedded.EmbeddedActiveMQ;
import org.apache.activemq.artemis.core.server.impl.AddressInfo;
import org.apache.activemq.artemis.core.server.plugin.impl.LoggingActiveMQServerPlugin;
import org.apache.activemq.artemis.core.server.plugin.impl.NotificationActiveMQServerPlugin;
import org.apache.activemq.artemis.core.server.reload.ReloadManager;
import org.apache.activemq.artemis.core.settings.impl.AddressFullMessagePolicy;
import org.apache.activemq.artemis.core.settings.impl.AddressSettings;
Expand Down Expand Up @@ -233,6 +235,64 @@ public void testRedeployConnector() throws Exception {
}
}

@Test
public void testRedeployPlugin() throws Exception {
Path brokerXML = getTestDirfile().toPath().resolve("broker.xml");
URL url1 = RedeployTest.class.getClassLoader().getResource("reload-plugin.xml");
URL url2 = RedeployTest.class.getClassLoader().getResource("reload-plugin-updated.xml");
Files.copy(url1.openStream(), brokerXML);

EmbeddedActiveMQ embeddedActiveMQ = new EmbeddedActiveMQ();
embeddedActiveMQ.setConfigResourcePath(brokerXML.toUri().toString());
embeddedActiveMQ.start();

final ReusableLatch latch = new ReusableLatch(1);

Runnable tick = latch::countDown;

embeddedActiveMQ.getActiveMQServer().getReloadManager().setTick(tick);

try {
latch.await(10, TimeUnit.SECONDS);

assertEquals(0, embeddedActiveMQ.getActiveMQServer().getBrokerPlugins().size());

embeddedActiveMQ.getActiveMQServer().registerBrokerPlugin(new NotificationActiveMQServerPlugin());

assertEquals(1, embeddedActiveMQ.getActiveMQServer().getBrokerPlugins().size());
assertTrue(embeddedActiveMQ.getActiveMQServer().getBrokerPlugins().stream()
.anyMatch(plugin ->
plugin instanceof NotificationActiveMQServerPlugin));

Files.copy(url2.openStream(), brokerXML, StandardCopyOption.REPLACE_EXISTING);
brokerXML.toFile().setLastModified(System.currentTimeMillis() + 1000);
latch.setCount(1);
embeddedActiveMQ.getActiveMQServer().getReloadManager().setTick(tick);
latch.await(10, TimeUnit.SECONDS);

assertEquals(2, embeddedActiveMQ.getActiveMQServer().getBrokerPlugins().size());
assertTrue(embeddedActiveMQ.getActiveMQServer().getBrokerPlugins().stream()
.anyMatch(plugin ->
plugin instanceof NotificationActiveMQServerPlugin));
assertTrue(embeddedActiveMQ.getActiveMQServer().getBrokerPlugins().stream()
.anyMatch(plugin ->
plugin instanceof LoggingActiveMQServerPlugin));

Files.copy(url1.openStream(), brokerXML, StandardCopyOption.REPLACE_EXISTING);
brokerXML.toFile().setLastModified(System.currentTimeMillis() + 1000);
latch.setCount(1);
embeddedActiveMQ.getActiveMQServer().getReloadManager().setTick(tick);
latch.await(10, TimeUnit.SECONDS);

assertEquals(1, embeddedActiveMQ.getActiveMQServer().getBrokerPlugins().size());
assertTrue(embeddedActiveMQ.getActiveMQServer().getBrokerPlugins().stream()
.anyMatch(plugin ->
plugin instanceof NotificationActiveMQServerPlugin));
} finally {
embeddedActiveMQ.stop();
}
}

@Test
public void testRedeploySecuritySettings() throws Exception {
Path brokerXML = getTestDirfile().toPath().resolve("broker.xml");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version='1.0'?>
<!--
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.
-->

<configuration xmlns="urn:activemq"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:activemq /schema/artemis-configuration.xsd">

<core xmlns="urn:activemq:core">

<configuration-file-refresh-period>100</configuration-file-refresh-period>

<persistence-enabled>false</persistence-enabled>

<broker-plugins>
<broker-plugin class-name="org.apache.activemq.artemis.core.server.plugin.impl.LoggingActiveMQServerPlugin">
<property key="LOG_ALL_EVENTS" value="true"/>
<property key="LOG_CONNECTION_EVENTS" value="true"/>
<property key="LOG_SESSION_EVENTS" value="true"/>
<property key="LOG_CONSUMER_EVENTS" value="true"/>
<property key="LOG_DELIVERING_EVENTS" value="true"/>
<property key="LOG_SENDING_EVENTS" value="true"/>
<property key="LOG_INTERNAL_EVENTS" value="true"/>
</broker-plugin>
</broker-plugins>

</core>
</configuration>
32 changes: 32 additions & 0 deletions tests/integration-tests/src/test/resources/reload-plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version='1.0'?>
<!--
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.
-->

<configuration xmlns="urn:activemq"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:activemq /schema/artemis-configuration.xsd">

<core xmlns="urn:activemq:core">

<configuration-file-refresh-period>100</configuration-file-refresh-period>

<persistence-enabled>false</persistence-enabled>

</core>
</configuration>

0 comments on commit 3a9ae21

Please sign in to comment.