From 21d9e3bbbfa75e07f7c0c107f474ab44a103141b Mon Sep 17 00:00:00 2001 From: brusdev Date: Sun, 19 Apr 2020 09:18:09 +0200 Subject: [PATCH] ARTEMIS-2723 Read the default CLI connector from the related broker Read the CLI connector from the related broker instance if it isn't set by user. --- .../commands/messages/ConnectionAbstract.java | 65 ++++++++++++++++++- .../apache/activemq/cli/test/ArtemisTest.java | 15 ++++- .../artemis/utils/uri/BeanSupport.java | 3 + .../impl/netty/TransportConstants.java | 3 + 4 files changed, 82 insertions(+), 4 deletions(-) diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/messages/ConnectionAbstract.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/messages/ConnectionAbstract.java index 1d252c3a19..b889ed62de 100644 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/messages/ConnectionAbstract.java +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/messages/ConnectionAbstract.java @@ -21,16 +21,29 @@ import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.JMSException; import javax.jms.JMSSecurityException; +import java.io.File; +import java.net.InetAddress; +import java.net.URI; +import java.util.Map; import io.airlift.airline.Option; +import org.apache.activemq.artemis.api.core.TransportConfiguration; +import org.apache.activemq.artemis.cli.commands.ActionContext; import org.apache.activemq.artemis.cli.commands.InputAbstract; +import org.apache.activemq.artemis.core.config.FileDeploymentManager; +import org.apache.activemq.artemis.core.config.impl.FileConfiguration; +import org.apache.activemq.artemis.core.remoting.impl.netty.TransportConstants; import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; +import org.apache.activemq.artemis.utils.ConfigurationHelper; +import org.apache.activemq.artemis.utils.uri.SchemaConstants; import org.apache.qpid.jms.JmsConnectionFactory; public class ConnectionAbstract extends InputAbstract { + private static final String DEFAULT_BROKER_URL = "tcp://localhost:61616"; + @Option(name = "--url", description = "URL towards the broker. (default: tcp://localhost:61616)") - protected String brokerURL = "tcp://localhost:61616"; + protected String brokerURL = DEFAULT_BROKER_URL; @Option(name = "--user", description = "User used to connect") protected String user; @@ -79,6 +92,56 @@ public class ConnectionAbstract extends InputAbstract { this.protocol = protocol; } + @Override + public Object execute(ActionContext context) throws Exception { + super.execute(context); + + if (brokerURL == DEFAULT_BROKER_URL) { + String brokerURLInstance = getBrokerURLInstance(); + + if (brokerURLInstance != null) { + brokerURL = brokerURLInstance; + } + } + + System.out.println("Connection brokerURL = " + brokerURL); + + return null; + } + + private String getBrokerURLInstance() { + if (getBrokerInstance() != null) { + try { + FileConfiguration fileConfiguration = new FileConfiguration(); + String brokerConfiguration = new File(new File(getBrokerEtc()), "broker.xml").toURI().toASCIIString(); + FileDeploymentManager fileDeploymentManager = new FileDeploymentManager(brokerConfiguration); + fileDeploymentManager.addDeployable(fileConfiguration); + fileDeploymentManager.readConfiguration(); + + for (TransportConfiguration acceptorConfiguration: fileConfiguration.getAcceptorConfigurations()) { + if (acceptorConfiguration.getName().equals("artemis")) { + Map acceptorParams = acceptorConfiguration.getParams(); + String scheme = ConfigurationHelper.getStringProperty(TransportConstants.SCHEME_PROP_NAME, SchemaConstants.TCP, acceptorParams); + String host = ConfigurationHelper.getStringProperty(TransportConstants.HOST_PROP_NAME, "localhost", acceptorParams); + int port = ConfigurationHelper.getIntProperty(TransportConstants.PORT_PROP_NAME, 61616, acceptorParams); + + if (InetAddress.getByName(host).isAnyLocalAddress()) { + host = "localhost"; + } + + return new URI(scheme, null, host, port, null, null, null).toString(); + } + } + } catch (Exception e) { + if (isVerbose()) { + System.out.print("Can not get the broker url instance: " + e.toString()); + } + } + } + + return null; + } + protected ConnectionFactory createConnectionFactory() throws Exception { if (protocol.equals("core")) { return createCoreConnectionFactory(); diff --git a/artemis-cli/src/test/java/org/apache/activemq/cli/test/ArtemisTest.java b/artemis-cli/src/test/java/org/apache/activemq/cli/test/ArtemisTest.java index 92f7c3d0ae..6579e843df 100644 --- a/artemis-cli/src/test/java/org/apache/activemq/cli/test/ArtemisTest.java +++ b/artemis-cli/src/test/java/org/apache/activemq/cli/test/ArtemisTest.java @@ -909,6 +909,11 @@ public class ArtemisTest extends CliTestBase { } + @Test + public void testCustomPort() throws Exception { + testSimpleRun("server", 61696); + } + @Test public void testPerfJournal() throws Exception { File instanceFolder = temporaryFolder.newFolder("server1"); @@ -924,6 +929,10 @@ public class ArtemisTest extends CliTestBase { public void testSimpleRun(String folderName) throws Exception { + testSimpleRun(folderName, 61616); + } + + public void testSimpleRun(String folderName, int acceptorPort) throws Exception { File instanceFolder = temporaryFolder.newFolder(folderName); setupAuth(instanceFolder); @@ -933,7 +942,7 @@ public class ArtemisTest extends CliTestBase { // This is usually set when run from the command line via artemis.profile Run.setEmbedded(true); - Artemis.main("create", instanceFolder.getAbsolutePath(), "--force", "--silent", "--no-web", "--queues", queues, "--addresses", addresses, "--no-autotune", "--require-login"); + Artemis.main("create", instanceFolder.getAbsolutePath(), "--force", "--silent", "--no-web", "--queues", queues, "--addresses", addresses, "--no-autotune", "--require-login", "--default-port", Integer.toString(acceptorPort)); System.setProperty("artemis.instance", instanceFolder.getAbsolutePath()); @@ -941,7 +950,7 @@ public class ArtemisTest extends CliTestBase { // Some exceptions may happen on the initialization, but they should be ok on start the basic core protocol Artemis.internalExecute("run"); - try (ServerLocator locator = ServerLocatorImpl.newLocator("tcp://localhost:61616"); + try (ServerLocator locator = ServerLocatorImpl.newLocator("tcp://localhost:" + acceptorPort); ClientSessionFactory factory = locator.createSessionFactory(); ClientSession coreSession = factory.createSession("admin", "admin", false, true, true, false, 0)) { for (String str : queues.split(",")) { @@ -973,7 +982,7 @@ public class ArtemisTest extends CliTestBase { assertEquals(Integer.valueOf(10), Artemis.internalExecute("producer", "--destination", "queue://q1", "--message", "message", "--message-count", "10", "--user", "admin", "--password", "admin")); assertEquals(Integer.valueOf(10), Artemis.internalExecute("consumer", "--destination", "queue://q1", "--break-on-null", "--receive-timeout", "100", "--user", "admin", "--password", "admin")); - ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616"); + ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:" + acceptorPort); Connection connection = cf.createConnection("admin", "admin"); Session session = connection.createSession(true, Session.SESSION_TRANSACTED); MessageProducer producer = session.createProducer(ActiveMQDestination.createDestination("queue://q1", ActiveMQDestination.TYPE.QUEUE)); diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/BeanSupport.java b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/BeanSupport.java index 94a589d970..d1719ea07c 100644 --- a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/BeanSupport.java +++ b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/uri/BeanSupport.java @@ -108,6 +108,9 @@ public class BeanSupport { Set allowableProperties, Map query, Map extraProps) { + if (allowableProperties.contains("scheme")) { + properties.put("scheme", "" + uri.getScheme()); + } if (allowableProperties.contains("host")) { properties.put("host", "" + uri.getHost()); } diff --git a/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/remoting/impl/netty/TransportConstants.java b/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/remoting/impl/netty/TransportConstants.java index 5b715dc541..d5b12ce100 100644 --- a/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/remoting/impl/netty/TransportConstants.java +++ b/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/remoting/impl/netty/TransportConstants.java @@ -79,6 +79,8 @@ public class TransportConstants { public static final String PROTOCOLS_PROP_NAME = "protocols"; + public static final String SCHEME_PROP_NAME = "scheme"; + public static final String HOST_PROP_NAME = "host"; public static final String PORT_PROP_NAME = "port"; @@ -362,6 +364,7 @@ public class TransportConstants { //noinspection deprecation allowableAcceptorKeys.add(TransportConstants.PROTOCOL_PROP_NAME); allowableAcceptorKeys.add(TransportConstants.PROTOCOLS_PROP_NAME); + allowableAcceptorKeys.add(TransportConstants.SCHEME_PROP_NAME); allowableAcceptorKeys.add(TransportConstants.HOST_PROP_NAME); allowableAcceptorKeys.add(TransportConstants.PORT_PROP_NAME); allowableAcceptorKeys.add(TransportConstants.KEYSTORE_PROVIDER_PROP_NAME);