NO-JIRA Moving CLI Management to use ManagementHelper

This commit is contained in:
Clebert Suconic 2023-07-20 13:57:06 -04:00 committed by clebertsuconic
parent 9ffe062c41
commit 6b61e9eaaf
22 changed files with 138 additions and 311 deletions

View File

@ -1,48 +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.artemis.cli.commands;
import org.apache.activemq.artemis.api.core.client.ActiveMQClient;
import org.apache.activemq.artemis.api.core.client.ClientMessage;
import org.apache.activemq.artemis.api.core.client.ClientSession;
import org.apache.activemq.artemis.api.core.client.ClientSessionFactory;
import org.apache.activemq.artemis.api.core.client.ServerLocator;
import org.apache.activemq.artemis.api.core.management.ManagementHelper;
import org.apache.activemq.artemis.cli.commands.messages.ConnectionAbstract;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
public abstract class AbstractAction extends ConnectionAbstract {
// TODO: This call could be replaced by a direct call into ManagementHelper.doManagement and their lambdas
public void performCoreManagement(ManagementCallback<ClientMessage> cb) throws Exception {
try (ActiveMQConnectionFactory factory = createCoreConnectionFactory();
ServerLocator locator = factory.getServerLocator();
ClientSessionFactory sessionFactory = locator.createSessionFactory();
ClientSession session = sessionFactory.createSession(user, password, false, true, true, false, ActiveMQClient.DEFAULT_ACK_BATCH_SIZE)) {
ManagementHelper.doManagement(session, cb::setUpInvocation, cb::requestSuccessful, cb::requestFailed);
}
}
public interface ManagementCallback<T> {
void setUpInvocation(T message) throws Exception;
void requestSuccessful(T reply) throws Exception;
void requestFailed(T reply) throws Exception;
}
}

View File

@ -17,9 +17,9 @@
package org.apache.activemq.artemis.cli.commands.address; package org.apache.activemq.artemis.cli.commands.address;
import com.github.rvesse.airline.annotations.Option; import com.github.rvesse.airline.annotations.Option;
import org.apache.activemq.artemis.cli.commands.AbstractAction; import org.apache.activemq.artemis.cli.commands.messages.ConnectionAbstract;
public abstract class AddressAbstract extends AbstractAction { public abstract class AddressAbstract extends ConnectionAbstract {
@Option(name = "--name", description = "The address's name.") @Option(name = "--name", description = "The address's name.")
private String name; private String name;
@ -37,7 +37,7 @@ public abstract class AddressAbstract extends AbstractAction {
private Boolean noMulticast; private Boolean noMulticast;
public AbstractAction setName(String name) { public AddressAbstract setName(String name) {
this.name = name; this.name = name;
return this; return this;
} }

View File

@ -18,7 +18,6 @@
package org.apache.activemq.artemis.cli.commands.address; package org.apache.activemq.artemis.cli.commands.address;
import com.github.rvesse.airline.annotations.Command; import com.github.rvesse.airline.annotations.Command;
import org.apache.activemq.artemis.api.core.client.ClientMessage;
import org.apache.activemq.artemis.api.core.management.ManagementHelper; import org.apache.activemq.artemis.api.core.management.ManagementHelper;
import org.apache.activemq.artemis.cli.commands.ActionContext; import org.apache.activemq.artemis.cli.commands.ActionContext;
@ -33,23 +32,14 @@ public class CreateAddress extends AddressAbstract {
} }
private void createAddress(final ActionContext context) throws Exception { private void createAddress(final ActionContext context) throws Exception {
performCoreManagement(new ManagementCallback<ClientMessage>() { performCoreManagement(message -> {
@Override ManagementHelper.putOperationInvocation(message, "broker", "createAddress", getName(true), getRoutingTypes(true));
public void setUpInvocation(ClientMessage message) throws Exception { }, reply -> {
ManagementHelper.putOperationInvocation(message, "broker", "createAddress", getName(true), getRoutingTypes(true)); final String result = ManagementHelper.getResult(reply, String.class) + " created successfully.";
} context.out.println(result);
}, reply -> {
@Override String errMsg = (String) ManagementHelper.getResult(reply, String.class);
public void requestSuccessful(ClientMessage reply) throws Exception { context.err.println("Failed to create address " + getName(true) + ". Reason: " + errMsg);
final String result = ManagementHelper.getResult(reply, String.class) + " created successfully.";
context.out.println(result);
}
@Override
public void requestFailed(ClientMessage reply) throws Exception {
String errMsg = (String) ManagementHelper.getResult(reply, String.class);
context.err.println("Failed to create address " + getName(true) + ". Reason: " + errMsg);
}
}); });
} }

View File

@ -19,7 +19,6 @@ package org.apache.activemq.artemis.cli.commands.address;
import com.github.rvesse.airline.annotations.Command; import com.github.rvesse.airline.annotations.Command;
import com.github.rvesse.airline.annotations.Option; import com.github.rvesse.airline.annotations.Option;
import org.apache.activemq.artemis.api.core.client.ClientMessage;
import org.apache.activemq.artemis.api.core.management.ManagementHelper; import org.apache.activemq.artemis.api.core.management.ManagementHelper;
import org.apache.activemq.artemis.cli.commands.ActionContext; import org.apache.activemq.artemis.cli.commands.ActionContext;
@ -37,22 +36,13 @@ public class DeleteAddress extends AddressAbstract {
} }
private void deleteAddress(final ActionContext context) throws Exception { private void deleteAddress(final ActionContext context) throws Exception {
performCoreManagement(new ManagementCallback<ClientMessage>() { performCoreManagement(message -> {
@Override ManagementHelper.putOperationInvocation(message, "broker", "deleteAddress", getName(true), force);
public void setUpInvocation(ClientMessage message) throws Exception { }, reply -> {
ManagementHelper.putOperationInvocation(message, "broker", "deleteAddress", getName(true), force); context.out.println("Address " + getName(true) + " deleted successfully.");
} }, reply -> {
String errMsg = (String) ManagementHelper.getResult(reply, String.class);
@Override context.err.println("Failed to delete address " + getName(true) + ". Reason: " + errMsg);
public void requestSuccessful(ClientMessage reply) throws Exception {
context.out.println("Address " + getName(true) + " deleted successfully.");
}
@Override
public void requestFailed(ClientMessage reply) throws Exception {
String errMsg = (String) ManagementHelper.getResult(reply, String.class);
context.err.println("Failed to delete address " + getName(true) + ". Reason: " + errMsg);
}
}); });
} }

View File

@ -19,14 +19,12 @@ package org.apache.activemq.artemis.cli.commands.address;
import com.github.rvesse.airline.annotations.Command; import com.github.rvesse.airline.annotations.Command;
import com.github.rvesse.airline.annotations.Option; import com.github.rvesse.airline.annotations.Option;
import org.apache.activemq.artemis.api.core.client.ClientMessage;
import org.apache.activemq.artemis.api.core.management.ManagementHelper; import org.apache.activemq.artemis.api.core.management.ManagementHelper;
import org.apache.activemq.artemis.cli.commands.ActionContext; import org.apache.activemq.artemis.cli.commands.ActionContext;
@Command(name = "show", description = "Show the selected address.") @Command(name = "show", description = "Show the selected address.")
public class ShowAddress extends AddressAbstract { public class ShowAddress extends AddressAbstract {
@Option(name = "--bindings", description = "Show the bindings for this address.") @Option(name = "--bindings", description = "Show the bindings for this address.")
boolean bindings; boolean bindings;
@ -38,30 +36,20 @@ public class ShowAddress extends AddressAbstract {
} }
private void showAddress(final ActionContext context) throws Exception { private void showAddress(final ActionContext context) throws Exception {
performCoreManagement(new ManagementCallback<ClientMessage>() { performCoreManagement(message -> {
@Override if (getName(false) == null) {
public void setUpInvocation(ClientMessage message) throws Exception { ManagementHelper.putOperationInvocation(message, "broker", "listAddresses", "\n");
} else if (bindings) {
if (getName(false) == null) { ManagementHelper.putOperationInvocation(message, "broker", "listBindingsForAddress", getName(false));
ManagementHelper.putOperationInvocation(message, "broker", "listAddresses", "\n"); } else {
} else if (bindings) { ManagementHelper.putOperationInvocation(message, "broker", "getAddressInfo", getName(false));
ManagementHelper.putOperationInvocation(message, "broker", "listBindingsForAddress", getName(false));
} else {
ManagementHelper.putOperationInvocation(message, "broker", "getAddressInfo", getName(false));
}
}
@Override
public void requestSuccessful(ClientMessage reply) throws Exception {
final String result = (String) ManagementHelper.getResult(reply, String.class);
context.out.println(result);
}
@Override
public void requestFailed(ClientMessage reply) throws Exception {
String errMsg = (String) ManagementHelper.getResult(reply, String.class);
context.err.println("Failed to show address " + getName(false) + ". Reason: " + errMsg);
} }
}, reply -> {
final String result = (String) ManagementHelper.getResult(reply, String.class);
context.out.println(result);
}, reply -> {
String errMsg = (String) ManagementHelper.getResult(reply, String.class);
context.err.println("Failed to show address " + getName(false) + ". Reason: " + errMsg);
}); });
} }

View File

@ -17,9 +17,7 @@
package org.apache.activemq.artemis.cli.commands.address; package org.apache.activemq.artemis.cli.commands.address;
import com.github.rvesse.airline.annotations.Command; import com.github.rvesse.airline.annotations.Command;
import org.apache.activemq.artemis.api.core.client.ClientMessage;
import org.apache.activemq.artemis.api.core.management.ManagementHelper; import org.apache.activemq.artemis.api.core.management.ManagementHelper;
import org.apache.activemq.artemis.cli.commands.AbstractAction;
import org.apache.activemq.artemis.cli.commands.ActionContext; import org.apache.activemq.artemis.cli.commands.ActionContext;
@Command(name = "update", description = "Update an address.") @Command(name = "update", description = "Update an address.")
@ -33,23 +31,14 @@ public class UpdateAddress extends AddressAbstract {
} }
private void updateAddress(final ActionContext context) throws Exception { private void updateAddress(final ActionContext context) throws Exception {
performCoreManagement(new AbstractAction.ManagementCallback<ClientMessage>() { performCoreManagement(message -> {
@Override ManagementHelper.putOperationInvocation(message, "broker", "updateAddress", getName(true), getRoutingTypes(false));
public void setUpInvocation(ClientMessage message) throws Exception { }, reply -> {
ManagementHelper.putOperationInvocation(message, "broker", "updateAddress", getName(true), getRoutingTypes(false)); final String result = ManagementHelper.getResult(reply, String.class) + " updated successfully.";
} context.out.println(result);
}, reply -> {
@Override String errMsg = (String) ManagementHelper.getResult(reply, String.class);
public void requestSuccessful(ClientMessage reply) throws Exception { context.err.println("Failed to update address " + getName(true) + ". Reason: " + errMsg);
final String result = ManagementHelper.getResult(reply, String.class) + " updated successfully.";
context.out.println(result);
}
@Override
public void requestFailed(ClientMessage reply) throws Exception {
String errMsg = (String) ManagementHelper.getResult(reply, String.class);
context.err.println("Failed to update address " + getName(true) + ". Reason: " + errMsg);
}
}); });
} }
} }

View File

@ -26,12 +26,12 @@ import java.util.concurrent.TimeoutException;
import com.github.rvesse.airline.annotations.Option; import com.github.rvesse.airline.annotations.Option;
import org.apache.activemq.artemis.api.core.management.ActiveMQManagementProxy; import org.apache.activemq.artemis.api.core.management.ActiveMQManagementProxy;
import org.apache.activemq.artemis.cli.CLIException; import org.apache.activemq.artemis.cli.CLIException;
import org.apache.activemq.artemis.cli.commands.AbstractAction;
import org.apache.activemq.artemis.cli.commands.ActionContext; import org.apache.activemq.artemis.cli.commands.ActionContext;
import org.apache.activemq.artemis.cli.commands.messages.ConnectionAbstract;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.apache.commons.lang3.time.StopWatch; import org.apache.commons.lang3.time.StopWatch;
public abstract class CheckAbstract extends AbstractAction { public abstract class CheckAbstract extends ConnectionAbstract {
@Option(name = "--name", description = "Name of the target to check.") @Option(name = "--name", description = "Name of the target to check.")
protected String name; protected String name;

View File

@ -22,6 +22,7 @@ import javax.jms.JMSException;
import javax.jms.JMSSecurityException; import javax.jms.JMSSecurityException;
import com.github.rvesse.airline.annotations.Option; import com.github.rvesse.airline.annotations.Option;
import org.apache.activemq.artemis.api.core.management.ManagementHelper;
import org.apache.activemq.artemis.cli.commands.ActionContext; import org.apache.activemq.artemis.cli.commands.ActionContext;
import org.apache.activemq.artemis.cli.commands.InputAbstract; import org.apache.activemq.artemis.cli.commands.InputAbstract;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
@ -237,4 +238,11 @@ public class ConnectionAbstract extends InputAbstract {
} }
return password; return password;
} }
protected void performCoreManagement(ManagementHelper.MessageAcceptor setup, ManagementHelper.MessageAcceptor ok, ManagementHelper.MessageAcceptor failed) throws Exception {
try (ActiveMQConnectionFactory factory = createCoreConnectionFactory()) {
ManagementHelper.doManagement(factory.getServerLocator(), user, password, setup, ok, failed);
}
}
} }

View File

@ -18,7 +18,6 @@
package org.apache.activemq.artemis.cli.commands.queue; package org.apache.activemq.artemis.cli.commands.queue;
import com.github.rvesse.airline.annotations.Command; import com.github.rvesse.airline.annotations.Command;
import org.apache.activemq.artemis.api.core.client.ClientMessage;
import org.apache.activemq.artemis.api.core.management.ManagementHelper; import org.apache.activemq.artemis.api.core.management.ManagementHelper;
import org.apache.activemq.artemis.cli.commands.ActionContext; import org.apache.activemq.artemis.cli.commands.ActionContext;
@ -33,23 +32,14 @@ public class CreateQueue extends QueueAbstract {
} }
private void createQueue(final ActionContext context) throws Exception { private void createQueue(final ActionContext context) throws Exception {
performCoreManagement(new ManagementCallback<ClientMessage>() { performCoreManagement(message -> {
@Override ManagementHelper.putOperationInvocation(message, "broker", "createQueue", getAddress(true), getRoutingType(), getName(), getFilter(), isDurable(), getMaxConsumers(-1), isPurgeOnNoConsumers(true), isAutoCreateAddress());
public void setUpInvocation(ClientMessage message) throws Exception { }, reply -> {
ManagementHelper.putOperationInvocation(message, "broker", "createQueue", getAddress(true), getRoutingType(), getName(), getFilter(), isDurable(), getMaxConsumers(-1), isPurgeOnNoConsumers(true), isAutoCreateAddress()); final String result = ManagementHelper.getResult(reply, String.class) + " created successfully.";
} context.out.println(result);
}, reply -> {
@Override String errMsg = (String) ManagementHelper.getResult(reply, String.class);
public void requestSuccessful(ClientMessage reply) throws Exception { context.err.println("Failed to create queue " + getName() + ". Reason: " + errMsg);
final String result = ManagementHelper.getResult(reply, String.class) + " created successfully.";
context.out.println(result);
}
@Override
public void requestFailed(ClientMessage reply) throws Exception {
String errMsg = (String) ManagementHelper.getResult(reply, String.class);
context.err.println("Failed to create queue " + getName() + ". Reason: " + errMsg);
}
}); });
} }
} }

View File

@ -19,13 +19,12 @@ package org.apache.activemq.artemis.cli.commands.queue;
import com.github.rvesse.airline.annotations.Command; import com.github.rvesse.airline.annotations.Command;
import com.github.rvesse.airline.annotations.Option; import com.github.rvesse.airline.annotations.Option;
import org.apache.activemq.artemis.api.core.client.ClientMessage;
import org.apache.activemq.artemis.api.core.management.ManagementHelper; import org.apache.activemq.artemis.api.core.management.ManagementHelper;
import org.apache.activemq.artemis.cli.commands.ActionContext; import org.apache.activemq.artemis.cli.commands.ActionContext;
import org.apache.activemq.artemis.cli.commands.AbstractAction; import org.apache.activemq.artemis.cli.commands.messages.ConnectionAbstract;
@Command(name = "delete", description = "Delete a queue.") @Command(name = "delete", description = "Delete a queue.")
public class DeleteQueue extends AbstractAction { public class DeleteQueue extends ConnectionAbstract {
@Option(name = "--name", description = "The queue's name") @Option(name = "--name", description = "The queue's name")
String name; String name;
@ -44,22 +43,13 @@ public class DeleteQueue extends AbstractAction {
} }
private void deleteQueue(final ActionContext context) throws Exception { private void deleteQueue(final ActionContext context) throws Exception {
performCoreManagement(new ManagementCallback<ClientMessage>() { performCoreManagement(message -> {
@Override ManagementHelper.putOperationInvocation(message, "broker", "destroyQueue", getName(), removeConsumers, autoDeleteAddress);
public void setUpInvocation(ClientMessage message) throws Exception { }, reply -> {
ManagementHelper.putOperationInvocation(message, "broker", "destroyQueue", getName(), removeConsumers, autoDeleteAddress); context.out.println("Queue " + getName() + " deleted successfully.");
} }, reply -> {
String errMsg = (String) ManagementHelper.getResult(reply, String.class);
@Override context.err.println("Failed to delete queue " + getName() + ". Reason: " + errMsg);
public void requestSuccessful(ClientMessage reply) throws Exception {
context.out.println("Queue " + getName() + " deleted successfully.");
}
@Override
public void requestFailed(ClientMessage reply) throws Exception {
String errMsg = (String) ManagementHelper.getResult(reply, String.class);
context.err.println("Failed to delete queue " + getName() + ". Reason: " + errMsg);
}
}); });
} }

View File

@ -19,14 +19,13 @@ package org.apache.activemq.artemis.cli.commands.queue;
import com.github.rvesse.airline.annotations.Command; import com.github.rvesse.airline.annotations.Command;
import com.github.rvesse.airline.annotations.Option; import com.github.rvesse.airline.annotations.Option;
import org.apache.activemq.artemis.api.core.client.ClientMessage;
import org.apache.activemq.artemis.api.core.management.ManagementHelper; import org.apache.activemq.artemis.api.core.management.ManagementHelper;
import org.apache.activemq.artemis.api.core.management.ResourceNames; import org.apache.activemq.artemis.api.core.management.ResourceNames;
import org.apache.activemq.artemis.cli.commands.ActionContext; import org.apache.activemq.artemis.cli.commands.ActionContext;
import org.apache.activemq.artemis.cli.commands.AbstractAction; import org.apache.activemq.artemis.cli.commands.messages.ConnectionAbstract;
@Command(name = "purge", description = "Delete all messages in a queue.") @Command(name = "purge", description = "Delete all messages in a queue.")
public class PurgeQueue extends AbstractAction { public class PurgeQueue extends ConnectionAbstract {
@Option(name = "--name", description = "The queue's name.") @Option(name = "--name", description = "The queue's name.")
String name; String name;
@ -39,22 +38,13 @@ public class PurgeQueue extends AbstractAction {
} }
private void purgeQueue(final ActionContext context) throws Exception { private void purgeQueue(final ActionContext context) throws Exception {
performCoreManagement(new ManagementCallback<ClientMessage>() { performCoreManagement(message -> {
@Override ManagementHelper.putOperationInvocation(message, ResourceNames.QUEUE + getName(), "removeAllMessages");
public void setUpInvocation(ClientMessage message) throws Exception { }, reply -> {
ManagementHelper.putOperationInvocation(message, ResourceNames.QUEUE + getName(), "removeAllMessages"); context.out.println("Queue " + getName() + " purged successfully.");
} }, reply -> {
String errMsg = (String) ManagementHelper.getResult(reply, String.class);
@Override context.err.println("Failed to purge queue " + getName() + ". Reason: " + errMsg);
public void requestSuccessful(ClientMessage reply) throws Exception {
context.out.println("Queue " + getName() + " purged successfully.");
}
@Override
public void requestFailed(ClientMessage reply) throws Exception {
String errMsg = (String) ManagementHelper.getResult(reply, String.class);
context.err.println("Failed to purge queue " + getName() + ". Reason: " + errMsg);
}
}); });
} }

View File

@ -17,9 +17,9 @@
package org.apache.activemq.artemis.cli.commands.queue; package org.apache.activemq.artemis.cli.commands.queue;
import com.github.rvesse.airline.annotations.Option; import com.github.rvesse.airline.annotations.Option;
import org.apache.activemq.artemis.cli.commands.AbstractAction; import org.apache.activemq.artemis.cli.commands.messages.ConnectionAbstract;
public class QueueAbstract extends AbstractAction { public class QueueAbstract extends ConnectionAbstract {
@Option(name = "--name", description = "The queue's name.") @Option(name = "--name", description = "The queue's name.")
private String name; private String name;

View File

@ -24,15 +24,14 @@ import java.util.TreeMap;
import com.github.rvesse.airline.annotations.Command; import com.github.rvesse.airline.annotations.Command;
import com.github.rvesse.airline.annotations.Option; import com.github.rvesse.airline.annotations.Option;
import org.apache.activemq.artemis.api.core.JsonUtil; import org.apache.activemq.artemis.api.core.JsonUtil;
import org.apache.activemq.artemis.api.core.client.ClientMessage;
import org.apache.activemq.artemis.api.core.management.ManagementHelper; import org.apache.activemq.artemis.api.core.management.ManagementHelper;
import org.apache.activemq.artemis.cli.commands.AbstractAction;
import org.apache.activemq.artemis.cli.commands.ActionContext; import org.apache.activemq.artemis.cli.commands.ActionContext;
import org.apache.activemq.artemis.cli.commands.messages.ConnectionAbstract;
import org.apache.activemq.artemis.json.JsonArray; import org.apache.activemq.artemis.json.JsonArray;
import org.apache.activemq.artemis.json.JsonObject; import org.apache.activemq.artemis.json.JsonObject;
@Command(name = "stat", description = "Print basic stats of a queue. Output includes CONSUMER_COUNT (number of consumers), MESSAGE_COUNT (current message count on the queue, including scheduled, paged and in-delivery messages), MESSAGES_ADDED (messages added to the queue), DELIVERING_COUNT (messages broker is currently delivering to consumer(s)), MESSAGES_ACKED (messages acknowledged from the consumer(s))." + " Queues can be filtered using EITHER '--queueName X' where X is contained in the queue name OR using a full filter '--field NAME --operation EQUALS --value X'.") @Command(name = "stat", description = "Print basic stats of a queue. Output includes CONSUMER_COUNT (number of consumers), MESSAGE_COUNT (current message count on the queue, including scheduled, paged and in-delivery messages), MESSAGES_ADDED (messages added to the queue), DELIVERING_COUNT (messages broker is currently delivering to consumer(s)), MESSAGES_ACKED (messages acknowledged from the consumer(s))." + " Queues can be filtered using EITHER '--queueName X' where X is contained in the queue name OR using a full filter '--field NAME --operation EQUALS --value X'.")
public class StatQueue extends AbstractAction { public class StatQueue extends ConnectionAbstract {
public enum FIELD { public enum FIELD {
NAME("name"), ADDRESS("address"), CONSUMER_COUNT("consumerCount"), MESSAGE_COUNT("messageCount"), MESSAGES_ADDED("messagesAdded"), DELIVERING_COUNT("deliveringCount"), MESSAGES_ACKED("messagesAcked"), SCHEDULED_COUNT("scheduledCount"), ROUTING_TYPE("routingType"); NAME("name"), ADDRESS("address"), CONSUMER_COUNT("consumerCount"), MESSAGE_COUNT("messageCount"), MESSAGES_ADDED("messagesAdded"), DELIVERING_COUNT("deliveringCount"), MESSAGES_ACKED("messagesAcked"), SCHEDULED_COUNT("scheduledCount"), ROUTING_TYPE("routingType");
@ -152,23 +151,14 @@ public class StatQueue extends AbstractAction {
} }
private void printStats(final ActionContext context, final String filter) throws Exception { private void printStats(final ActionContext context, final String filter) throws Exception {
performCoreManagement(new ManagementCallback<ClientMessage>() { performCoreManagement(message -> {
@Override ManagementHelper.putOperationInvocation(message, "broker", "listQueues", filter, 1, maxRows);
public void setUpInvocation(ClientMessage message) throws Exception { }, reply -> {
ManagementHelper.putOperationInvocation(message, "broker", "listQueues", filter, 1, maxRows); final String result = (String) ManagementHelper.getResult(reply, String.class);
} printStats(result);
}, reply -> {
@Override String errMsg = (String) ManagementHelper.getResult(reply, String.class);
public void requestSuccessful(ClientMessage reply) throws Exception { getActionContext().err.println("Failed to get Stats for Queues. Reason: " + errMsg);
final String result = (String) ManagementHelper.getResult(reply, String.class);
printStats(result);
}
@Override
public void requestFailed(ClientMessage reply) throws Exception {
String errMsg = (String) ManagementHelper.getResult(reply, String.class);
getActionContext().err.println("Failed to get Stats for Queues. Reason: " + errMsg);
}
}); });
} }

View File

@ -17,7 +17,6 @@
package org.apache.activemq.artemis.cli.commands.queue; package org.apache.activemq.artemis.cli.commands.queue;
import com.github.rvesse.airline.annotations.Command; import com.github.rvesse.airline.annotations.Command;
import org.apache.activemq.artemis.api.core.client.ClientMessage;
import org.apache.activemq.artemis.api.core.management.ManagementHelper; import org.apache.activemq.artemis.api.core.management.ManagementHelper;
import org.apache.activemq.artemis.cli.commands.ActionContext; import org.apache.activemq.artemis.cli.commands.ActionContext;
@ -32,23 +31,14 @@ public class UpdateQueue extends QueueAbstract {
} }
private void updateQueue(final ActionContext context) throws Exception { private void updateQueue(final ActionContext context) throws Exception {
performCoreManagement(new ManagementCallback<ClientMessage>() { performCoreManagement(message -> {
@Override ManagementHelper.putOperationInvocation(message, "broker", "updateQueue", getName(), getRoutingType(), getMaxConsumers(null), isPurgeOnNoConsumers());
public void setUpInvocation(ClientMessage message) throws Exception { }, reply -> {
ManagementHelper.putOperationInvocation(message, "broker", "updateQueue", getName(), getRoutingType(), getMaxConsumers(null), isPurgeOnNoConsumers()); final String result = ManagementHelper.getResult(reply, String.class) + " updated successfully.";
} context.out.println(result);
}, reply -> {
@Override String errMsg = (String) ManagementHelper.getResult(reply, String.class);
public void requestSuccessful(ClientMessage reply) throws Exception { context.err.println("Failed to update " + getName() + ". Reason: " + errMsg);
final String result = ManagementHelper.getResult(reply, String.class) + " updated successfully.";
context.out.println(result);
}
@Override
public void requestFailed(ClientMessage reply) throws Exception {
String errMsg = (String) ManagementHelper.getResult(reply, String.class);
context.err.println("Failed to update " + getName() + ". Reason: " + errMsg);
}
}); });
} }
} }

View File

@ -18,9 +18,7 @@ package org.apache.activemq.artemis.cli.commands.user;
import com.github.rvesse.airline.annotations.Command; import com.github.rvesse.airline.annotations.Command;
import com.github.rvesse.airline.annotations.Option; import com.github.rvesse.airline.annotations.Option;
import org.apache.activemq.artemis.api.core.client.ClientMessage;
import org.apache.activemq.artemis.api.core.management.ManagementHelper; import org.apache.activemq.artemis.api.core.management.ManagementHelper;
import org.apache.activemq.artemis.cli.commands.AbstractAction;
import org.apache.activemq.artemis.cli.commands.ActionContext; import org.apache.activemq.artemis.cli.commands.ActionContext;
/** /**
@ -49,22 +47,13 @@ public class AddUser extends PasswordAction {
* @throws Exception if communication with the broker fails * @throws Exception if communication with the broker fails
*/ */
private void add() throws Exception { private void add() throws Exception {
performCoreManagement(new AbstractAction.ManagementCallback<ClientMessage>() { performCoreManagement(message -> {
@Override ManagementHelper.putOperationInvocation(message, "broker", "addUser", userCommandUser, userCommandPassword, role, plaintext);
public void setUpInvocation(ClientMessage message) throws Exception { }, reply -> {
ManagementHelper.putOperationInvocation(message, "broker", "addUser", userCommandUser, userCommandPassword, role, plaintext); getActionContext().out.println(userCommandUser + " added successfully.");
} }, reply -> {
String errMsg = (String) ManagementHelper.getResult(reply, String.class);
@Override getActionContext().err.println("Failed to add user " + userCommandUser + ". Reason: " + errMsg);
public void requestSuccessful(ClientMessage reply) throws Exception {
getActionContext().out.println(userCommandUser + " added successfully.");
}
@Override
public void requestFailed(ClientMessage reply) throws Exception {
String errMsg = (String) ManagementHelper.getResult(reply, String.class);
getActionContext().err.println("Failed to add user " + userCommandUser + ". Reason: " + errMsg);
}
}); });
} }
} }

View File

@ -21,9 +21,7 @@ import org.apache.activemq.artemis.json.JsonObject;
import com.github.rvesse.airline.annotations.Command; import com.github.rvesse.airline.annotations.Command;
import org.apache.activemq.artemis.api.core.JsonUtil; import org.apache.activemq.artemis.api.core.JsonUtil;
import org.apache.activemq.artemis.api.core.client.ClientMessage;
import org.apache.activemq.artemis.api.core.management.ManagementHelper; import org.apache.activemq.artemis.api.core.management.ManagementHelper;
import org.apache.activemq.artemis.cli.commands.AbstractAction;
import org.apache.activemq.artemis.cli.commands.ActionContext; import org.apache.activemq.artemis.cli.commands.ActionContext;
/** /**
@ -52,22 +50,13 @@ public class ListUser extends UserAction {
int userCount = 0; int userCount = 0;
final String[] result = new String[1]; final String[] result = new String[1];
performCoreManagement(new AbstractAction.ManagementCallback<ClientMessage>() { performCoreManagement(message -> {
@Override ManagementHelper.putOperationInvocation(message, "broker", "listUser", userCommandUser);
public void setUpInvocation(ClientMessage message) throws Exception { }, reply -> {
ManagementHelper.putOperationInvocation(message, "broker", "listUser", userCommandUser); result[0] = (String) ManagementHelper.getResult(reply, String.class);
} }, reply -> {
String errMsg = (String) ManagementHelper.getResult(reply, String.class);
@Override getActionContext().err.println("Failed to list user " + userCommandUser + ". Reason: " + errMsg);
public void requestSuccessful(ClientMessage reply) throws Exception {
result[0] = (String) ManagementHelper.getResult(reply, String.class);
}
@Override
public void requestFailed(ClientMessage reply) throws Exception {
String errMsg = (String) ManagementHelper.getResult(reply, String.class);
getActionContext().err.println("Failed to list user " + userCommandUser + ". Reason: " + errMsg);
}
}); });
// process the JSON results from the broker // process the JSON results from the broker

View File

@ -17,9 +17,7 @@
package org.apache.activemq.artemis.cli.commands.user; package org.apache.activemq.artemis.cli.commands.user;
import com.github.rvesse.airline.annotations.Command; import com.github.rvesse.airline.annotations.Command;
import org.apache.activemq.artemis.api.core.client.ClientMessage;
import org.apache.activemq.artemis.api.core.management.ManagementHelper; import org.apache.activemq.artemis.api.core.management.ManagementHelper;
import org.apache.activemq.artemis.cli.commands.AbstractAction;
import org.apache.activemq.artemis.cli.commands.ActionContext; import org.apache.activemq.artemis.cli.commands.ActionContext;
/** /**
@ -38,22 +36,13 @@ public class RemoveUser extends UserAction {
} }
private void remove() throws Exception { private void remove() throws Exception {
performCoreManagement(new AbstractAction.ManagementCallback<ClientMessage>() { performCoreManagement(message -> {
@Override ManagementHelper.putOperationInvocation(message, "broker", "removeUser", userCommandUser);
public void setUpInvocation(ClientMessage message) throws Exception { }, reply -> {
ManagementHelper.putOperationInvocation(message, "broker", "removeUser", userCommandUser); getActionContext().out.println(userCommandUser + " removed successfully.");
} }, reply -> {
String errMsg = (String) ManagementHelper.getResult(reply, String.class);
@Override getActionContext().err.println("Failed to remove user " + userCommandUser + ". Reason: " + errMsg);
public void requestSuccessful(ClientMessage reply) throws Exception {
getActionContext().out.println(userCommandUser + " removed successfully.");
}
@Override
public void requestFailed(ClientMessage reply) throws Exception {
String errMsg = (String) ManagementHelper.getResult(reply, String.class);
getActionContext().err.println("Failed to remove user " + userCommandUser + ". Reason: " + errMsg);
}
}); });
} }

View File

@ -18,9 +18,7 @@ package org.apache.activemq.artemis.cli.commands.user;
import com.github.rvesse.airline.annotations.Command; import com.github.rvesse.airline.annotations.Command;
import com.github.rvesse.airline.annotations.Option; import com.github.rvesse.airline.annotations.Option;
import org.apache.activemq.artemis.api.core.client.ClientMessage;
import org.apache.activemq.artemis.api.core.management.ManagementHelper; import org.apache.activemq.artemis.api.core.management.ManagementHelper;
import org.apache.activemq.artemis.cli.commands.AbstractAction;
import org.apache.activemq.artemis.cli.commands.ActionContext; import org.apache.activemq.artemis.cli.commands.ActionContext;
/** /**
@ -43,22 +41,13 @@ public class ResetUser extends PasswordAction {
} }
private void reset() throws Exception { private void reset() throws Exception {
performCoreManagement(new AbstractAction.ManagementCallback<ClientMessage>() { performCoreManagement(message -> {
@Override ManagementHelper.putOperationInvocation(message, "broker", "resetUser", userCommandUser, userCommandPassword, role, plaintext);
public void setUpInvocation(ClientMessage message) throws Exception { }, reply -> {
ManagementHelper.putOperationInvocation(message, "broker", "resetUser", userCommandUser, userCommandPassword, role, plaintext); getActionContext().out.println(userCommandUser + " reset successfully.");
} }, reply -> {
String errMsg = (String) ManagementHelper.getResult(reply, String.class);
@Override getActionContext().err.println("Failed to reset user " + userCommandUser + ". Reason: " + errMsg);
public void requestSuccessful(ClientMessage reply) throws Exception {
getActionContext().out.println(userCommandUser + " reset successfully.");
}
@Override
public void requestFailed(ClientMessage reply) throws Exception {
String errMsg = (String) ManagementHelper.getResult(reply, String.class);
getActionContext().err.println("Failed to reset user " + userCommandUser + ". Reason: " + errMsg);
}
}); });
} }
} }

View File

@ -17,9 +17,9 @@
package org.apache.activemq.artemis.cli.commands.user; package org.apache.activemq.artemis.cli.commands.user;
import com.github.rvesse.airline.annotations.Option; import com.github.rvesse.airline.annotations.Option;
import org.apache.activemq.artemis.cli.commands.AbstractAction; import org.apache.activemq.artemis.cli.commands.messages.ConnectionAbstract;
public abstract class UserAction extends AbstractAction { public abstract class UserAction extends ConnectionAbstract {
@Option(name = "--role", description = "The user's role(s). Separate multiple roles with comma.") @Option(name = "--role", description = "The user's role(s). Separate multiple roles with comma.")
String role; String role;

View File

@ -99,10 +99,14 @@ public final class ManagementHelper {
void accept(ClientMessage message) throws Exception; void accept(ClientMessage message) throws Exception;
} }
/** Utility function to connect to a server and perform a management operation via core. */
public static void doManagement(String uri, String user, String password, MessageAcceptor setup, MessageAcceptor ok, MessageAcceptor failed) throws Exception { public static void doManagement(String uri, String user, String password, MessageAcceptor setup, MessageAcceptor ok, MessageAcceptor failed) throws Exception {
try (ServerLocator locator = ServerLocatorImpl.newLocator(uri); try (ServerLocator locator = ServerLocatorImpl.newLocator(uri)) {
ClientSessionFactory sessionFactory = locator.createSessionFactory(); doManagement(locator, user, password, setup, ok, failed);
}
}
public static void doManagement(ServerLocator locator, String user, String password, MessageAcceptor setup, MessageAcceptor ok, MessageAcceptor failed) throws Exception {
try (ClientSessionFactory sessionFactory = locator.createSessionFactory();
ClientSession session = sessionFactory.createSession(user, password, false, true, true, false, ActiveMQClient.DEFAULT_ACK_BATCH_SIZE)) { ClientSession session = sessionFactory.createSession(user, password, false, true, true, false, ActiveMQClient.DEFAULT_ACK_BATCH_SIZE)) {
doManagement(session, setup, ok, failed); doManagement(session, setup, ok, failed);
} }

View File

@ -29,12 +29,12 @@ import org.apache.activemq.artemis.api.core.client.ClientProducer;
import org.apache.activemq.artemis.api.core.client.ClientSession; import org.apache.activemq.artemis.api.core.client.ClientSession;
import org.apache.activemq.artemis.api.core.client.ClientSessionFactory; import org.apache.activemq.artemis.api.core.client.ClientSessionFactory;
import org.apache.activemq.artemis.api.core.client.ServerLocator; import org.apache.activemq.artemis.api.core.client.ServerLocator;
import org.apache.activemq.artemis.cli.commands.AbstractAction;
import org.apache.activemq.artemis.cli.commands.ActionContext; import org.apache.activemq.artemis.cli.commands.ActionContext;
import org.apache.activemq.artemis.cli.commands.address.CreateAddress; import org.apache.activemq.artemis.cli.commands.address.CreateAddress;
import org.apache.activemq.artemis.cli.commands.address.DeleteAddress; import org.apache.activemq.artemis.cli.commands.address.DeleteAddress;
import org.apache.activemq.artemis.cli.commands.address.ShowAddress; import org.apache.activemq.artemis.cli.commands.address.ShowAddress;
import org.apache.activemq.artemis.cli.commands.address.UpdateAddress; import org.apache.activemq.artemis.cli.commands.address.UpdateAddress;
import org.apache.activemq.artemis.cli.commands.messages.ConnectionAbstract;
import org.apache.activemq.artemis.core.config.DivertConfiguration; import org.apache.activemq.artemis.core.config.DivertConfiguration;
import org.apache.activemq.artemis.api.core.RoutingType; import org.apache.activemq.artemis.api.core.RoutingType;
import org.apache.activemq.artemis.core.server.Queue; import org.apache.activemq.artemis.core.server.Queue;
@ -241,13 +241,13 @@ public class AddressCommandTest extends JMSTestBase {
checkExecutionFailure(updateAddress, expectedErrorMessage); checkExecutionFailure(updateAddress, expectedErrorMessage);
} }
private void checkExecutionPassed(AbstractAction command) throws Exception { private void checkExecutionPassed(ConnectionAbstract command) throws Exception {
String fullMessage = output.toString(); String fullMessage = output.toString();
logger.debug("output: {}", fullMessage); logger.debug("output: {}", fullMessage);
assertTrue(fullMessage, fullMessage.contains("successfully")); assertTrue(fullMessage, fullMessage.contains("successfully"));
} }
private void checkExecutionFailure(AbstractAction command, String message) throws Exception { private void checkExecutionFailure(ConnectionAbstract command, String message) throws Exception {
String fullMessage = error.toString(); String fullMessage = error.toString();
logger.debug("error: {}", fullMessage); logger.debug("error: {}", fullMessage);
assertTrue(fullMessage, fullMessage.contains(message)); assertTrue(fullMessage, fullMessage.contains(message));

View File

@ -25,8 +25,8 @@ import java.util.UUID;
import org.apache.activemq.artemis.api.core.QueueConfiguration; import org.apache.activemq.artemis.api.core.QueueConfiguration;
import org.apache.activemq.artemis.api.core.SimpleString; import org.apache.activemq.artemis.api.core.SimpleString;
import org.apache.activemq.artemis.cli.commands.AbstractAction;
import org.apache.activemq.artemis.cli.commands.ActionContext; import org.apache.activemq.artemis.cli.commands.ActionContext;
import org.apache.activemq.artemis.cli.commands.messages.ConnectionAbstract;
import org.apache.activemq.artemis.cli.commands.queue.CreateQueue; import org.apache.activemq.artemis.cli.commands.queue.CreateQueue;
import org.apache.activemq.artemis.cli.commands.queue.DeleteQueue; import org.apache.activemq.artemis.cli.commands.queue.DeleteQueue;
import org.apache.activemq.artemis.cli.commands.queue.PurgeQueue; import org.apache.activemq.artemis.cli.commands.queue.PurgeQueue;
@ -382,13 +382,13 @@ public class QueueCommandTest extends JMSTestBase {
assertFalse(server.queueQuery(queueName).isExists()); assertFalse(server.queueQuery(queueName).isExists());
} }
private void checkExecutionPassed(AbstractAction command) throws Exception { private void checkExecutionPassed(ConnectionAbstract command) throws Exception {
String fullMessage = output.toString(); String fullMessage = output.toString();
logger.debug("output: {}", fullMessage); logger.debug("output: {}", fullMessage);
assertTrue(fullMessage, fullMessage.contains("successfully")); assertTrue(fullMessage, fullMessage.contains("successfully"));
} }
private void checkExecutionFailure(AbstractAction command, String message) throws Exception { private void checkExecutionFailure(ConnectionAbstract command, String message) throws Exception {
String fullMessage = error.toString(); String fullMessage = error.toString();
logger.debug("error: {}", fullMessage); logger.debug("error: {}", fullMessage);
assertTrue(fullMessage, fullMessage.contains(message)); assertTrue(fullMessage, fullMessage.contains(message));