https://issues.apache.org/jira/browse/AMQ-3452

Adds stop goal to the maven activemq plugin.
This commit is contained in:
Timothy Bish 2014-01-20 14:03:39 -05:00
parent f7cbe9fa17
commit a50f011272
5 changed files with 278 additions and 180 deletions

View File

@ -26,7 +26,7 @@
<artifactId>activemq-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
<name>ActiveMQ :: StartUp Plugin</name>
<name>ActiveMQ :: StartUp/Stop Plugin</name>
<dependencies>
<dependency>

View File

@ -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();
}
}
}

View File

@ -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 <a
* href="http://activemq.apache.org/how-do-i-embed-a-broker-inside-a-connection.html">here</a>
*
* @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);
}
}

View File

@ -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 <a
* href="http://activemq.apache.org/how-do-i-embed-a-broker-inside-a-connection.html">here</a>
*
* @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);
}
}

View File

@ -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");
}
}