diff --git a/activemq-tooling/activemq-maven-plugin/pom.xml b/activemq-tooling/activemq-maven-plugin/pom.xml index 6f231e4369..e7c3ae9a55 100644 --- a/activemq-tooling/activemq-maven-plugin/pom.xml +++ b/activemq-tooling/activemq-maven-plugin/pom.xml @@ -26,7 +26,7 @@ activemq-maven-plugin maven-plugin - ActiveMQ :: StartUp Plugin + ActiveMQ :: StartUp/Stop Plugin diff --git a/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/Broker.java b/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/Broker.java new file mode 100644 index 0000000000..14a0275a9b --- /dev/null +++ b/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/Broker.java @@ -0,0 +1,120 @@ +/** + * 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.maven; + +import java.util.Properties; + +import org.apache.activemq.broker.BrokerFactory; +import org.apache.activemq.broker.BrokerService; +import org.apache.maven.plugin.MojoExecutionException; + +public class Broker { + + private static BrokerService broker; + + private static boolean[] shutdown; + + private static Thread shutdownThread; + + public static void start(boolean fork, String configUri) throws MojoExecutionException { + + if (broker != null) { + throw new MojoExecutionException("A local broker is already running"); + } + + try { + broker = BrokerFactory.createBroker(configUri); + if (fork) { + new Thread(new Runnable() { + @Override + public void run() { + try { + broker.start(); + waitForShutdown(); + } catch (Exception e) { + e.printStackTrace(); + } + } + }).start(); + } else { + broker.start(); + waitForShutdown(); + } + } catch (Exception e) { + throw new MojoExecutionException("Failed to start the ActiveMQ Broker", e); + } + } + + public static void stop() throws MojoExecutionException { + + if (broker == null) { + throw new MojoExecutionException("The local broker is not running"); + } + + try { + broker.stop(); + broker.waitUntilStopped(); + broker = null; + + Runtime.getRuntime().removeShutdownHook(shutdownThread); + + // Terminate the shutdown hook thread + synchronized (shutdown) { + shutdown[0] = true; + shutdown.notify(); + } + } catch (Exception e) { + throw new MojoExecutionException("Failed to stop the ActiveMQ Broker", e); + } + } + + /** + * Wait for a shutdown invocation elsewhere + * + * @throws Exception + */ + protected static void waitForShutdown() throws Exception { + shutdown = new boolean[] { false }; + + shutdownThread = new Thread() { + @Override + public void run() { + synchronized (shutdown) { + shutdown[0] = true; + shutdown.notify(); + } + } + }; + + Runtime.getRuntime().addShutdownHook(shutdownThread); + + // Wait for any shutdown event + synchronized (shutdown) { + while (!shutdown[0]) { + try { + shutdown.wait(); + } catch (InterruptedException e) { + } + } + } + + // Stop broker + if (broker != null) { + broker.stop(); + } + } +} diff --git a/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/BrokerMojo.java b/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/BrokerMojo.java deleted file mode 100644 index 6eabdbea3e..0000000000 --- a/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/BrokerMojo.java +++ /dev/null @@ -1,179 +0,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. - */ -package org.apache.activemq.maven; - -/** - * 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. - */ - -import java.util.Properties; - -import org.apache.activemq.broker.BrokerFactory; -import org.apache.activemq.broker.BrokerService; -import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.project.MavenProject; - -/** - * Goal which starts an activemq broker. - * - * @goal run - * @phase process-sources - */ -public class BrokerMojo extends AbstractMojo { - /** - * The maven project. - * - * @parameter property="project" default-value="${project}" - * @required - * @readonly - */ - protected MavenProject project; - - /** - * The broker configuration uri The list of currently supported URI syntaxes - * is described here - * - * @parameter property="configUri" - * default-value="broker:(tcp://localhost:61616)?useJmx=false&persistent=false" - * @required - */ - private String configUri; - - /** - * Indicates whether to fork the broker, useful for integration tests. - * - * @parameter property="fork" default-value="false" - */ - private boolean fork; - - /** - * System properties to add - * - * @parameter property="systemProperties" - */ - private Properties systemProperties; - - /** - * Skip execution of the ActiveMQ Broker plugin if set to true - * - * @parameter property="skip" - */ - private boolean skip; - - @Override - public void execute() throws MojoExecutionException { - try { - if (skip) { - getLog().info("Skipped execution of ActiveMQ Broker"); - return; - } - - setSystemProperties(); - - getLog().info("Loading broker configUri: " + configUri); - if (XBeanFileResolver.isXBeanFile(configUri)) { - getLog().debug("configUri before transformation: " + configUri); - configUri = XBeanFileResolver.toUrlCompliantAbsolutePath(configUri); - getLog().debug("configUri after transformation: " + configUri); - } - - final BrokerService broker = BrokerFactory.createBroker(configUri); - if (fork) { - new Thread(new Runnable() { - @Override - public void run() { - try { - broker.start(); - waitForShutdown(broker); - } catch (Exception e) { - e.printStackTrace(); - } - } - }).start(); - } else { - broker.start(); - waitForShutdown(broker); - } - } catch (Exception e) { - throw new MojoExecutionException("Failed to start ActiveMQ Broker", e); - } - } - - /** - * Wait for a shutdown invocation elsewhere - * - * @throws Exception - */ - protected void waitForShutdown(BrokerService broker) throws Exception { - final boolean[] shutdown = new boolean[] { - false - }; - Runtime.getRuntime().addShutdownHook(new Thread() { - @Override - public void run() { - synchronized (shutdown) { - shutdown[0] = true; - shutdown.notify(); - } - } - }); - - // Wait for any shutdown event - synchronized (shutdown) { - while (!shutdown[0]) { - try { - shutdown.wait(); - } catch (InterruptedException e) { - } - } - } - - // Stop broker - broker.stop(); - } - - /** - * Set system properties - */ - protected void setSystemProperties() { - // Set the default properties - System.setProperty("activemq.base", project.getBuild().getDirectory() + "/"); - System.setProperty("activemq.home", project.getBuild().getDirectory() + "/"); - System.setProperty("org.apache.activemq.UseDedicatedTaskRunner", "true"); - System.setProperty("org.apache.activemq.default.directory.prefix", project.getBuild().getDirectory() + "/"); - System.setProperty("derby.system.home", project.getBuild().getDirectory() + "/"); - System.setProperty("derby.storage.fileSyncTransactionLog", "true"); - - // Overwrite any custom properties - System.getProperties().putAll(systemProperties); - } -} diff --git a/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/StartBrokerMojo.java b/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/StartBrokerMojo.java new file mode 100644 index 0000000000..eef68aaa93 --- /dev/null +++ b/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/StartBrokerMojo.java @@ -0,0 +1,110 @@ +/** + * 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.maven; + +import java.util.Properties; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.project.MavenProject; + +/** + * Goal which starts an activemq broker. + * + * @goal run + * @phase process-sources + */ +public class StartBrokerMojo extends AbstractMojo { + + /** + * The maven project. + * + * @parameter property="project" default-value="${project}" + * @required + * @readonly + */ + protected MavenProject project; + + /** + * The broker configuration uri The list of currently supported URI syntaxes + * is described here + * + * @parameter property="configUri" + * default-value="broker:(tcp://localhost:61616)?useJmx=false&persistent=false" + * @required + */ + private String configUri; + + /** + * Indicates whether to fork the broker, useful for integration tests. + * + * @parameter property="fork" default-value="false" + */ + private boolean fork; + + /** + * System properties to add + * + * @parameter property="systemProperties" + */ + private Properties systemProperties; + + /** + * Skip execution of the ActiveMQ Broker plugin if set to true + * + * @parameter property="skip" + */ + private boolean skip; + + @Override + public void execute() throws MojoExecutionException { + if (skip) { + getLog().info("Skipped execution of ActiveMQ Broker"); + return; + } + + setSystemProperties(); + + getLog().info("Loading broker configUri: " + configUri); + if (XBeanFileResolver.isXBeanFile(configUri)) { + getLog().debug("configUri before transformation: " + configUri); + configUri = XBeanFileResolver.toUrlCompliantAbsolutePath(configUri); + getLog().debug("configUri after transformation: " + configUri); + } + + Broker.start(fork, configUri); + + getLog().info("Started the ActiveMQ Broker"); + } + + /** + * Set system properties + */ + protected void setSystemProperties() { + // Set the default properties + System.setProperty("activemq.base", project.getBuild().getDirectory() + "/"); + System.setProperty("activemq.home", project.getBuild().getDirectory() + "/"); + System.setProperty("org.apache.activemq.UseDedicatedTaskRunner", "true"); + System.setProperty("org.apache.activemq.default.directory.prefix", project.getBuild().getDirectory() + "/"); + System.setProperty("derby.system.home", project.getBuild().getDirectory() + "/"); + System.setProperty("derby.storage.fileSyncTransactionLog", "true"); + + // Overwrite any custom properties + System.getProperties().putAll(systemProperties); + } +} diff --git a/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/StopBrokerMojo.java b/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/StopBrokerMojo.java new file mode 100644 index 0000000000..a0b15eb1dd --- /dev/null +++ b/activemq-tooling/activemq-maven-plugin/src/main/java/org/apache/activemq/maven/StopBrokerMojo.java @@ -0,0 +1,47 @@ +/** + * 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.maven; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; + +/** + * Goal which stops an activemq broker. + * + * @goal stop + * @phase process-sources + */ +public class StopBrokerMojo extends AbstractMojo { + + /** + * Skip execution of the ActiveMQ Broker plugin if set to true + * + * @parameter property="skip" + */ + private boolean skip; + + public void execute() throws MojoExecutionException { + if (skip) { + getLog().info("Skipped execution of ActiveMQ Broker"); + return; + } + + Broker.stop(); + + getLog().info("Stopped the ActiveMQ Broker"); + } +}