activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clebertsuco...@apache.org
Subject [activemq-artemis] branch master updated: ARTEMIS-2723 Read the default CLI connector from the related broker
Date Mon, 20 Apr 2020 18:38:52 GMT
This is an automated email from the ASF dual-hosted git repository.

clebertsuconic pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/activemq-artemis.git


The following commit(s) were added to refs/heads/master by this push:
     new 21d9e3b  ARTEMIS-2723 Read the default CLI connector from the related broker
     new b20bca2  This closes #3092
21d9e3b is described below

commit 21d9e3bbbfa75e07f7c0c107f474ab44a103141b
Author: brusdev <bruscinodf@gmail.com>
AuthorDate: Sun Apr 19 09:18:09 2020 +0200

    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.
---
 .../cli/commands/messages/ConnectionAbstract.java  | 65 +++++++++++++++++++++-
 .../org/apache/activemq/cli/test/ArtemisTest.java  | 15 ++++-
 .../activemq/artemis/utils/uri/BeanSupport.java    |  3 +
 .../remoting/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 1d252c3..b889ed6 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<String, Object> 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 92f7c3d..6579e84 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
@@ -910,6 +910,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");
       setupAuth(instanceFolder);
@@ -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 94a589d..d1719ea 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<String> allowableProperties,
                               Map<String, String> query,
                               Map<String, Object> 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 5b715dc..d5b12ce 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);


Mime
View raw message