Improving bootstraping / CLI

- Added a method on Bootstrap class that could be used on embedding a server
This commit is contained in:
Clebert Suconic 2015-08-24 20:06:53 -04:00
parent a440682ec0
commit 0771ba511c
7 changed files with 85 additions and 49 deletions

View File

@ -25,6 +25,7 @@ import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* <p>
@ -35,19 +36,31 @@ import java.util.Comparator;
public class Artemis {
public static void main(String[] args) throws Throwable {
ArrayList<File> dirs = new ArrayList<File>();
String home = System.getProperty("artemis.home");
if (home != null) {
dirs.add(new File(new File(home), "lib"));
}
File fileHome = home != null ? new File(home) : null;
String instance = System.getProperty("artemis.instance");
File instanceFile = null;
if (instance != null) {
instanceFile = new File(instance);
dirs.add(new File(instanceFile, "lib"));
File fileInstance = instance != null ? new File(instance) : null;
execute(fileHome, fileInstance, args);
}
/** This is a good method for booting an embedded command */
public static void execute(File artemisHome, File artemisInstance, List<String> args) throws Throwable {
execute(artemisHome, artemisInstance, (String[]) args.toArray(new String[args.size()]));
}
/** This is a good method for booting an embedded command */
public static void execute(File fileHome, File fileInstance, String ... args) throws Throwable {
ArrayList<File> dirs = new ArrayList<File>();
if (fileHome != null) {
dirs.add(new File(fileHome, "lib"));
}
if (fileInstance != null) {
dirs.add(new File(fileInstance, "lib"));
}
ArrayList<URL> urls = new ArrayList<URL>();
for (File bootdir : dirs) {
@ -75,8 +88,8 @@ public class Artemis {
}
}
if (instance != null) {
System.setProperty("java.io.tmpdir", new File(new File(instance), "tmp").getCanonicalPath());
if (fileInstance != null) {
System.setProperty("java.io.tmpdir", new File(fileInstance, "tmp").getCanonicalPath());
}
// Lets try to covert the logging.configuration setting to a valid URI
@ -86,23 +99,28 @@ public class Artemis {
}
// Without the etc on the config, things like JGroups configuration wouldn't be loaded
if (instanceFile != null) {
File etcFile = new File(instance, "etc");
if (fileInstance != null) {
File etcFile = new File(fileInstance, "etc");
// Adding etc to the classLoader so modules can lookup for their configs
urls.add(etcFile.toURI().toURL());
}
ClassLoader originalCL = Thread.currentThread().getContextClassLoader();
// Now setup our classloader..
URLClassLoader loader = new URLClassLoader(urls.toArray(new URL[urls.size()]));
Thread.currentThread().setContextClassLoader(loader);
Class<?> clazz = loader.loadClass("org.apache.activemq.artemis.cli.Artemis");
Method method = clazz.getMethod("main", args.getClass());
Method method = clazz.getMethod("execute", File.class, File.class, args.getClass());
try {
method.invoke(null, (Object) args);
method.invoke(null, fileHome, fileInstance, args);
}
catch (InvocationTargetException e) {
throw e.getTargetException();
}
finally {
Thread.currentThread().setContextClassLoader(originalCL);
}
}

View File

@ -44,13 +44,26 @@ public class Artemis {
@SuppressWarnings("unchecked")
public static void main(String... args) throws Exception {
execute(null, null, args);
}
public static Object internalExecute(String... args) throws Exception {
return internalExecute(null, null, args);
}
public static Object execute(File artemisHome, File artemisInstance, List<String> args) throws Exception {
return execute(artemisHome, artemisInstance, (String[]) args.toArray(new String[args.size()]));
}
public static Object execute(File artemisHome, File artemisInstance, String... args) throws Exception {
try {
execute(args);
return internalExecute(artemisHome, artemisInstance, args);
}
catch (ConfigurationException configException) {
System.err.println(configException.getMessage());
System.out.println();
System.out.println("Configuration should be specified as 'scheme:location'. Default configuration is 'xml:${ARTEMIS_INSTANCE}/etc/bootstrap.xml'");
return null;
}
catch (RuntimeException re) {
System.err.println(re.getMessage());
@ -59,18 +72,13 @@ public class Artemis {
Cli<Action> parser = builder(null).build();
parser.parse("help").execute(ActionContext.system());
return null;
}
}
public static Object execute(String... args) throws Exception {
return execute(null, null, args);
}
public static Object execute(File artemisHome, File artemisInstance, List<String> args) throws Exception {
return execute(artemisHome, artemisInstance, (String[]) args.toArray(new String[args.size()]));
}
public static Object execute(File artemisHome, File artemisInstance, String... args) throws Exception {
/** This method is used to validate exception returns.
* Useful on test cases */
public static Object internalExecute(File artemisHome, File artemisInstance, String[] args) throws Exception {
Action action = builder(artemisInstance).build().parse(args);
action.setHomeValues(artemisHome, artemisInstance);

View File

@ -27,6 +27,10 @@ public class ActionContext {
this.err = err;
}
public ActionContext() {
this(System.in, System.out, System.err);
}
public InputStream in;
public PrintStream out;
public PrintStream err;

View File

@ -91,7 +91,7 @@ public class ArtemisTest {
Artemis.main("create", temporaryFolder.getRoot().getAbsolutePath(), "--force", "--silent", "--no-web", "--queues", queues, "--topics", topics);
System.setProperty("artemis.instance", temporaryFolder.getRoot().getAbsolutePath());
// Some exceptions may happen on the initialization, but they should be ok on start the basic core protocol
Artemis.execute("run");
Artemis.internalExecute("run");
try (ServerLocator locator = ServerLocatorImpl.newLocator("tcp://localhost:61616");
ClientSessionFactory factory = locator.createSessionFactory();
@ -106,8 +106,8 @@ public class ArtemisTest {
}
}
Assert.assertEquals(Integer.valueOf(1000), Artemis.execute("producer", "--message-count", "1000", "--verbose"));
Assert.assertEquals(Integer.valueOf(1000), Artemis.execute("consumer", "--verbose", "--break-on-null", "--receive-timeout", "100"));
Assert.assertEquals(Integer.valueOf(1000), Artemis.internalExecute("producer", "--message-count", "1000", "--verbose"));
Assert.assertEquals(Integer.valueOf(1000), Artemis.internalExecute("consumer", "--verbose", "--break-on-null", "--receive-timeout", "100"));
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection = cf.createConnection();
@ -128,22 +128,22 @@ public class ArtemisTest {
connection.close();
cf.close();
Assert.assertEquals(Integer.valueOf(1), Artemis.execute("browser", "--txt-size", "50", "--verbose", "--filter", "fruit='banana'"));
Assert.assertEquals(Integer.valueOf(1), Artemis.internalExecute("browser", "--txt-size", "50", "--verbose", "--filter", "fruit='banana'"));
Assert.assertEquals(Integer.valueOf(100), Artemis.execute("browser", "--txt-size", "50", "--verbose", "--filter", "fruit='orange'"));
Assert.assertEquals(Integer.valueOf(100), Artemis.internalExecute("browser", "--txt-size", "50", "--verbose", "--filter", "fruit='orange'"));
Assert.assertEquals(Integer.valueOf(101), Artemis.execute("browser", "--txt-size", "50", "--verbose"));
Assert.assertEquals(Integer.valueOf(101), Artemis.internalExecute("browser", "--txt-size", "50", "--verbose"));
// should only receive 10 messages on browse as I'm setting messageCount=10
Assert.assertEquals(Integer.valueOf(10), Artemis.execute("browser", "--txt-size", "50", "--verbose", "--message-count", "10"));
Assert.assertEquals(Integer.valueOf(10), Artemis.internalExecute("browser", "--txt-size", "50", "--verbose", "--message-count", "10"));
// Nothing was consumed until here as it was only browsing, check it's receiving again
Assert.assertEquals(Integer.valueOf(1), Artemis.execute("consumer", "--txt-size", "50", "--verbose", "--break-on-null", "--receive-timeout", "100", "--filter", "fruit='banana'"));
Assert.assertEquals(Integer.valueOf(1), Artemis.internalExecute("consumer", "--txt-size", "50", "--verbose", "--break-on-null", "--receive-timeout", "100", "--filter", "fruit='banana'"));
// Checking it was acked before
Assert.assertEquals(Integer.valueOf(100), Artemis.execute("consumer", "--txt-size", "50", "--verbose", "--break-on-null", "--receive-timeout", "100"));
Assert.assertEquals(Integer.valueOf(100), Artemis.internalExecute("consumer", "--txt-size", "50", "--verbose", "--break-on-null", "--receive-timeout", "100"));
Artemis.execute("stop");
Artemis.internalExecute("stop");
Assert.assertTrue(Run.latchRunning.await(5, TimeUnit.SECONDS));
Assert.assertEquals(0, LibaioContext.getTotalMaxIO());

View File

@ -21,7 +21,6 @@ import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import org.apache.activemq.artemis.cli.Artemis;
import org.apache.activemq.artemis.cli.commands.Run;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.apache.maven.plugin.MojoExecutionException;
@ -31,6 +30,7 @@ import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.activemq.artemis.boot.Artemis;
@Mojo(name = "cli", defaultPhase = LifecyclePhase.VERIFY)
public class ArtemisCLIPlugin extends ArtemisAbstractPlugin {
@ -158,7 +158,7 @@ public class ArtemisCLIPlugin extends ArtemisAbstractPlugin {
org.apache.activemq.artemis.cli.process.ProcessBuilder.cleanupProcess();
}
catch (Exception e) {
catch (Throwable e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}

View File

@ -29,7 +29,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.activemq.artemis.cli.Artemis;
import org.apache.activemq.artemis.boot.Artemis;
import org.apache.activemq.artemis.utils.FileUtil;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
@ -351,7 +351,7 @@ public class ArtemisCreatePlugin extends ArtemisAbstractPlugin {
getLog().info("###################################################################################################");
}
catch (Exception e) {
catch (Throwable e) {
getLog().error(e);
throw new MojoFailureException(e.getMessage());
}

View File

@ -148,15 +148,10 @@ public class RemotingServiceImpl implements RemotingService, ConnectionLifeCycle
this.protocolMap.put(coreProtocolManagerFactory.getProtocols()[0], coreProtocolManagerFactory.createProtocolManager(server, coreProtocolManagerFactory.filterInterceptors(incomingInterceptors), coreProtocolManagerFactory.filterInterceptors(outgoingInterceptors)));
if (config.isResolveProtocols()) {
ServiceLoader<ProtocolManagerFactory> serviceLoader = ServiceLoader.load(ProtocolManagerFactory.class, this.getClass().getClassLoader());
if (serviceLoader != null) {
for (ProtocolManagerFactory next : serviceLoader) {
String[] protocols = next.getProtocols();
for (String protocol : protocols) {
ActiveMQServerLogger.LOGGER.addingProtocolSupport(protocol, next.getModuleName());
protocolMap.put(protocol, next.createProtocolManager(server, next.filterInterceptors(incomingInterceptors), next.filterInterceptors(outgoingInterceptors)));
}
}
resolveProtocols(server, this.getClass().getClassLoader());
if (this.getClass().getClassLoader() != Thread.currentThread().getContextClassLoader()) {
resolveProtocols(server, Thread.currentThread().getContextClassLoader());
}
}
@ -171,7 +166,18 @@ public class RemotingServiceImpl implements RemotingService, ConnectionLifeCycle
}
}
// RemotingService implementation -------------------------------
private void resolveProtocols(ActiveMQServer server, ClassLoader loader) {
ServiceLoader<ProtocolManagerFactory> serviceLoader = ServiceLoader.load(ProtocolManagerFactory.class, loader);
if (serviceLoader != null) {
for (ProtocolManagerFactory next : serviceLoader) {
String[] protocols = next.getProtocols();
for (String protocol : protocols) {
ActiveMQServerLogger.LOGGER.addingProtocolSupport(protocol, next.getModuleName());
protocolMap.put(protocol, next.createProtocolManager(server, next.filterInterceptors(incomingInterceptors), next.filterInterceptors(outgoingInterceptors)));
}
}
}
}
private void setInterceptors(Configuration configuration) {
incomingInterceptors.addAll(serviceRegistry.getIncomingInterceptors(configuration.getIncomingInterceptorClassNames()));