ARTEMIS-3691 Build the CLI connector from a broker acceptor

This commit is contained in:
Domenico Francesco Bruscino 2022-02-18 14:23:02 +01:00 committed by Justin Bertram
parent d93d71bb54
commit dd4328b654
No known key found for this signature in database
GPG Key ID: F41830B875BB8633
6 changed files with 191 additions and 13 deletions

View File

@ -32,6 +32,10 @@ import org.apache.activemq.artemis.utils.uri.SchemaConstants;
public abstract class ActionAbstract implements Action {
public static final String DEFAULT_BROKER_URL = "tcp://localhost:61616";
public static final String DEFAULT_BROKER_ACCEPTOR = "artemis";
@Option(name = "--verbose", description = "Adds more information on the execution")
public boolean verbose;
@ -76,13 +80,17 @@ public abstract class ActionAbstract implements Action {
return brokerInstance;
}
protected String getBrokerURLInstance() {
public String getBrokerURLInstance(String acceptor) {
if (getBrokerInstance() != null) {
try {
Configuration brokerConfiguration = getBrokerConfiguration();
if (acceptor == null) {
acceptor = DEFAULT_BROKER_ACCEPTOR;
}
for (TransportConfiguration acceptorConfiguration: brokerConfiguration.getAcceptorConfigurations()) {
if (acceptorConfiguration.getName().equals("artemis")) {
if (acceptorConfiguration.getName().equals(acceptor)) {
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);

View File

@ -30,11 +30,12 @@ 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: Read from current broker.xml or tcp://localhost:61616 if the default cannot be parsed)")
@Option(name = "--url", description = "URL towards the broker. (default: Build URL from acceptors defined in the broker.xml or tcp://localhost:61616 if the default cannot be parsed)")
protected String brokerURL = DEFAULT_BROKER_URL;
@Option(name = "--acceptor", description = "Acceptor used to build URL towards the broker")
protected String acceptor;
@Option(name = "--user", description = "User used to connect")
protected String user;
@ -47,6 +48,23 @@ public class ConnectionAbstract extends InputAbstract {
@Option(name = "--protocol", description = "Protocol used. Valid values are amqp or core. Default=core.")
String protocol = "core";
public String getBrokerURL() {
return brokerURL;
}
public void setBrokerURL(String brokerURL) {
this.brokerURL = brokerURL;
}
public String getAcceptor() {
return acceptor;
}
public ConnectionAbstract setAcceptor(String acceptor) {
this.acceptor = acceptor;
return this;
}
public String getUser() {
return user;
}
@ -95,7 +113,7 @@ public class ConnectionAbstract extends InputAbstract {
// and still honor the one passed by parameter.
// SupressWarnings was added to this method to supress the false positive here from error-prone.
if (brokerURL == DEFAULT_BROKER_URL) {
String brokerURLInstance = getBrokerURLInstance();
String brokerURLInstance = getBrokerURLInstance(acceptor);
if (brokerURLInstance != null) {
brokerURL = brokerURLInstance;

View File

@ -40,11 +40,12 @@ import org.apache.qpid.jms.JmsConnectionFactory;
@Command(name = "transfer", description = "Moves Messages from one destination towards another destination")
public class Transfer extends InputAbstract {
private static final String DEFAULT_BROKER_URL = "tcp://localhost:61616";
@Option(name = "--source-url", description = "URL towards the broker. (default: Read from current broker.xml or tcp://localhost:61616 if the default cannot be parsed)")
@Option(name = "--source-url", description = "URL towards the broker. (default: Build URL from acceptors defined in the broker.xml or tcp://localhost:61616 if the default cannot be parsed)")
protected String sourceURL = DEFAULT_BROKER_URL;
@Option(name = "--source-acceptor", description = "Acceptor used to build URL towards the broker")
protected String sourceAcceptor;
@Option(name = "--source-user", description = "User used to connect")
protected String sourceUser;
@ -117,6 +118,15 @@ public class Transfer extends InputAbstract {
return this;
}
public String getSourceAcceptor() {
return sourceAcceptor;
}
public Transfer setSourceAcceptor(String sourceAcceptor) {
this.sourceAcceptor = sourceAcceptor;
return this;
}
public String getSourceUser() {
return sourceUser;
}
@ -319,7 +329,7 @@ public class Transfer extends InputAbstract {
// and still honor the one passed by parameter.
// SupressWarnings was added to this method to supress the false positive here from error-prone.
if (sourceURL == DEFAULT_BROKER_URL) {
String brokerURLInstance = getBrokerURLInstance();
String brokerURLInstance = getBrokerURLInstance(sourceAcceptor);
if (brokerURLInstance != null) {
sourceURL = brokerURLInstance;

View File

@ -0,0 +1,67 @@
/**
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.artemis.cli.commands.messages;
import org.apache.activemq.cli.test.TestActionContext;
import org.junit.Assert;
import org.junit.Test;
import java.io.File;
public class ConnectionAbstractTest {
@Test
public void testDefaultAcceptor() throws Exception {
ConnectionAbstract connectionAbstract = new ConnectionAbstract();
File brokerInstanceEtc = new File(this.getClass().getClassLoader()
.getResource("broker.xml").getFile()).getParentFile();
System.setProperty("artemis.instance.etc", brokerInstanceEtc.getAbsolutePath());
try {
connectionAbstract.setHomeValues(null, brokerInstanceEtc.getParentFile());
connectionAbstract.execute(new TestActionContext());
Assert.assertEquals(ConnectionAbstract.DEFAULT_BROKER_URL, connectionAbstract.getBrokerURL());
} finally {
System.clearProperty("artemis.instance.etc");
}
}
@Test
public void testAMQPAcceptor() throws Exception {
ConnectionAbstract connectionAbstract = new ConnectionAbstract();
File brokerInstanceEtc = new File(this.getClass().getClassLoader()
.getResource("broker.xml").getFile()).getParentFile();
System.setProperty("artemis.instance.etc", brokerInstanceEtc.getAbsolutePath());
try {
connectionAbstract.setHomeValues(null, brokerInstanceEtc.getParentFile());
connectionAbstract.setAcceptor("amqp");
connectionAbstract.execute(new TestActionContext());
Assert.assertEquals("tcp://localhost:5672", connectionAbstract.getBrokerURL());
} finally {
System.clearProperty("artemis.instance.etc");
}
}
}

View File

@ -0,0 +1,75 @@
/**
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.artemis.cli.commands.messages;
import org.apache.activemq.cli.test.TestActionContext;
import org.junit.Assert;
import org.junit.Test;
import javax.jms.JMSException;
import java.io.File;
public class TransferTest {
@Test
public void testDefaultSourceAcceptor() {
Transfer transfer = new Transfer();
File brokerInstanceEtc = new File(this.getClass().getClassLoader()
.getResource("broker.xml").getFile()).getParentFile();
System.setProperty("artemis.instance.etc", brokerInstanceEtc.getAbsolutePath());
try {
transfer.setHomeValues(null, brokerInstanceEtc.getParentFile());
try {
transfer.execute(new TestActionContext());
} catch (Exception e) {
Assert.assertEquals(JMSException.class, e.getClass());
}
Assert.assertEquals(ConnectionAbstract.DEFAULT_BROKER_URL, transfer.getSourceURL());
} finally {
System.clearProperty("artemis.instance.etc");
}
}
@Test
public void testAMQPSourceAcceptor() {
Transfer transfer = new Transfer();
File brokerInstanceEtc = new File(this.getClass().getClassLoader()
.getResource("broker.xml").getFile()).getParentFile();
System.setProperty("artemis.instance.etc", brokerInstanceEtc.getAbsolutePath());
try {
transfer.setHomeValues(null, brokerInstanceEtc.getParentFile());
transfer.setSourceAcceptor("amqp");
try {
transfer.execute(new TestActionContext());
} catch (Exception e) {
Assert.assertEquals(JMSException.class, e.getClass());
}
Assert.assertEquals("tcp://localhost:5672", transfer.getSourceURL());
} finally {
System.clearProperty("artemis.instance.etc");
}
}
}

View File

@ -36,12 +36,12 @@ under the License.
<connectors>
<!-- Default Connector. Returned to clients during broadcast and distributed around cluster. See broadcast and discovery-groups -->
<connector name="activemq">tcp://${activemq.remoting.default.host:localhost}:${activemq.remoting.default.port:61616}</connector>
<connector name="artemis">tcp://${activemq.remoting.default.host:localhost}:${activemq.remoting.default.port:61616}</connector>
</connectors>
<acceptors>
<!-- Default ActiveMQ Artemis Acceptor. Multi-protocol adapter. Currently supports Core, OpenWire, Stomp and AMQP. -->
<acceptor name="activemq">tcp://${activemq.remoting.default.host:localhost}:${activemq.remoting.default.port:61616}</acceptor>
<acceptor name="artemis">tcp://${activemq.remoting.default.host:localhost}:${activemq.remoting.default.port:61616}</acceptor>
<!-- AMQP Acceptor. Listens on default AMQP port for AMQP traffic.-->
<acceptor name="amqp">tcp://${activemq.remoting.amqp.host:localhost}:${activemq.remoting.amqp.port:5672}?protocols=AMQP</acceptor>