From 6b61e9eaaf4ea387346785d269bea47881186426 Mon Sep 17 00:00:00 2001 From: Clebert Suconic Date: Thu, 20 Jul 2023 13:57:06 -0400 Subject: [PATCH] NO-JIRA Moving CLI Management to use ManagementHelper --- .../artemis/cli/commands/AbstractAction.java | 48 ------------------- .../cli/commands/address/AddressAbstract.java | 6 +-- .../cli/commands/address/CreateAddress.java | 26 ++++------ .../cli/commands/address/DeleteAddress.java | 24 +++------- .../cli/commands/address/ShowAddress.java | 38 +++++---------- .../cli/commands/address/UpdateAddress.java | 27 ++++------- .../cli/commands/check/CheckAbstract.java | 4 +- .../commands/messages/ConnectionAbstract.java | 8 ++++ .../cli/commands/queue/CreateQueue.java | 26 ++++------ .../cli/commands/queue/DeleteQueue.java | 28 ++++------- .../cli/commands/queue/PurgeQueue.java | 28 ++++------- .../cli/commands/queue/QueueAbstract.java | 4 +- .../artemis/cli/commands/queue/StatQueue.java | 30 ++++-------- .../cli/commands/queue/UpdateQueue.java | 26 ++++------ .../artemis/cli/commands/user/AddUser.java | 25 +++------- .../artemis/cli/commands/user/ListUser.java | 25 +++------- .../artemis/cli/commands/user/RemoveUser.java | 25 +++------- .../artemis/cli/commands/user/ResetUser.java | 25 +++------- .../artemis/cli/commands/user/UserAction.java | 4 +- .../api/core/management/ManagementHelper.java | 10 ++-- .../integration/cli/AddressCommandTest.java | 6 +-- .../integration/cli/QueueCommandTest.java | 6 +-- 22 files changed, 138 insertions(+), 311 deletions(-) delete mode 100644 artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/AbstractAction.java diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/AbstractAction.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/AbstractAction.java deleted file mode 100644 index 5c4662d18a..0000000000 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/AbstractAction.java +++ /dev/null @@ -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 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 { - - void setUpInvocation(T message) throws Exception; - - void requestSuccessful(T reply) throws Exception; - - void requestFailed(T reply) throws Exception; - } -} diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/address/AddressAbstract.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/address/AddressAbstract.java index 1e7f082186..337b8c4b48 100644 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/address/AddressAbstract.java +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/address/AddressAbstract.java @@ -17,9 +17,9 @@ package org.apache.activemq.artemis.cli.commands.address; 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.") private String name; @@ -37,7 +37,7 @@ public abstract class AddressAbstract extends AbstractAction { private Boolean noMulticast; - public AbstractAction setName(String name) { + public AddressAbstract setName(String name) { this.name = name; return this; } diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/address/CreateAddress.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/address/CreateAddress.java index fcc97428d5..6d5a004d46 100644 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/address/CreateAddress.java +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/address/CreateAddress.java @@ -18,7 +18,6 @@ package org.apache.activemq.artemis.cli.commands.address; 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.cli.commands.ActionContext; @@ -33,23 +32,14 @@ public class CreateAddress extends AddressAbstract { } private void createAddress(final ActionContext context) throws Exception { - performCoreManagement(new ManagementCallback() { - @Override - public void setUpInvocation(ClientMessage message) throws Exception { - ManagementHelper.putOperationInvocation(message, "broker", "createAddress", getName(true), getRoutingTypes(true)); - } - - @Override - public void requestSuccessful(ClientMessage reply) throws Exception { - 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); - } + performCoreManagement(message -> { + ManagementHelper.putOperationInvocation(message, "broker", "createAddress", getName(true), getRoutingTypes(true)); + }, reply -> { + final String result = ManagementHelper.getResult(reply, String.class) + " created successfully."; + context.out.println(result); + }, reply -> { + String errMsg = (String) ManagementHelper.getResult(reply, String.class); + context.err.println("Failed to create address " + getName(true) + ". Reason: " + errMsg); }); } diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/address/DeleteAddress.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/address/DeleteAddress.java index 79bd003290..6f18b442f6 100644 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/address/DeleteAddress.java +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/address/DeleteAddress.java @@ -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.Option; -import org.apache.activemq.artemis.api.core.client.ClientMessage; import org.apache.activemq.artemis.api.core.management.ManagementHelper; 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 { - performCoreManagement(new ManagementCallback() { - @Override - public void setUpInvocation(ClientMessage message) throws Exception { - ManagementHelper.putOperationInvocation(message, "broker", "deleteAddress", getName(true), force); - } - - @Override - 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); - } + performCoreManagement(message -> { + ManagementHelper.putOperationInvocation(message, "broker", "deleteAddress", getName(true), force); + }, reply -> { + context.out.println("Address " + getName(true) + " deleted successfully."); + }, reply -> { + String errMsg = (String) ManagementHelper.getResult(reply, String.class); + context.err.println("Failed to delete address " + getName(true) + ". Reason: " + errMsg); }); } diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/address/ShowAddress.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/address/ShowAddress.java index 7be7d73ee0..f2b7b42c3a 100644 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/address/ShowAddress.java +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/address/ShowAddress.java @@ -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.Option; -import org.apache.activemq.artemis.api.core.client.ClientMessage; import org.apache.activemq.artemis.api.core.management.ManagementHelper; import org.apache.activemq.artemis.cli.commands.ActionContext; @Command(name = "show", description = "Show the selected address.") public class ShowAddress extends AddressAbstract { - @Option(name = "--bindings", description = "Show the bindings for this address.") boolean bindings; @@ -38,30 +36,20 @@ public class ShowAddress extends AddressAbstract { } private void showAddress(final ActionContext context) throws Exception { - performCoreManagement(new ManagementCallback() { - @Override - public void setUpInvocation(ClientMessage message) throws Exception { - - if (getName(false) == null) { - ManagementHelper.putOperationInvocation(message, "broker", "listAddresses", "\n"); - } else if (bindings) { - 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); + performCoreManagement(message -> { + if (getName(false) == null) { + ManagementHelper.putOperationInvocation(message, "broker", "listAddresses", "\n"); + } else if (bindings) { + ManagementHelper.putOperationInvocation(message, "broker", "listBindingsForAddress", getName(false)); + } else { + ManagementHelper.putOperationInvocation(message, "broker", "getAddressInfo", getName(false)); } + }, 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); }); } diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/address/UpdateAddress.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/address/UpdateAddress.java index 04e7ac8ef2..614e7b61c2 100644 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/address/UpdateAddress.java +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/address/UpdateAddress.java @@ -17,9 +17,7 @@ package org.apache.activemq.artemis.cli.commands.address; 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.cli.commands.AbstractAction; import org.apache.activemq.artemis.cli.commands.ActionContext; @Command(name = "update", description = "Update an address.") @@ -33,23 +31,14 @@ public class UpdateAddress extends AddressAbstract { } private void updateAddress(final ActionContext context) throws Exception { - performCoreManagement(new AbstractAction.ManagementCallback() { - @Override - public void setUpInvocation(ClientMessage message) throws Exception { - ManagementHelper.putOperationInvocation(message, "broker", "updateAddress", getName(true), getRoutingTypes(false)); - } - - @Override - public void requestSuccessful(ClientMessage reply) throws Exception { - 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); - } + performCoreManagement(message -> { + ManagementHelper.putOperationInvocation(message, "broker", "updateAddress", getName(true), getRoutingTypes(false)); + }, reply -> { + final String result = ManagementHelper.getResult(reply, String.class) + " updated successfully."; + context.out.println(result); + }, reply -> { + String errMsg = (String) ManagementHelper.getResult(reply, String.class); + context.err.println("Failed to update address " + getName(true) + ". Reason: " + errMsg); }); } } diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/check/CheckAbstract.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/check/CheckAbstract.java index 221593e9b2..dff6238f7e 100644 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/check/CheckAbstract.java +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/check/CheckAbstract.java @@ -26,12 +26,12 @@ import java.util.concurrent.TimeoutException; import com.github.rvesse.airline.annotations.Option; import org.apache.activemq.artemis.api.core.management.ActiveMQManagementProxy; 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.messages.ConnectionAbstract; import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; 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.") protected String name; 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 f4e8f08183..c9ee1a4fb8 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 @@ -22,6 +22,7 @@ import javax.jms.JMSException; import javax.jms.JMSSecurityException; 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.InputAbstract; import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; @@ -237,4 +238,11 @@ public class ConnectionAbstract extends InputAbstract { } 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); + } + } + } diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/queue/CreateQueue.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/queue/CreateQueue.java index 57d272e109..a015b1f317 100644 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/queue/CreateQueue.java +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/queue/CreateQueue.java @@ -18,7 +18,6 @@ package org.apache.activemq.artemis.cli.commands.queue; 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.cli.commands.ActionContext; @@ -33,23 +32,14 @@ public class CreateQueue extends QueueAbstract { } private void createQueue(final ActionContext context) throws Exception { - performCoreManagement(new ManagementCallback() { - @Override - public void setUpInvocation(ClientMessage message) throws Exception { - ManagementHelper.putOperationInvocation(message, "broker", "createQueue", getAddress(true), getRoutingType(), getName(), getFilter(), isDurable(), getMaxConsumers(-1), isPurgeOnNoConsumers(true), isAutoCreateAddress()); - } - - @Override - public void requestSuccessful(ClientMessage reply) throws Exception { - 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); - } + performCoreManagement(message -> { + ManagementHelper.putOperationInvocation(message, "broker", "createQueue", getAddress(true), getRoutingType(), getName(), getFilter(), isDurable(), getMaxConsumers(-1), isPurgeOnNoConsumers(true), isAutoCreateAddress()); + }, reply -> { + final String result = ManagementHelper.getResult(reply, String.class) + " created successfully."; + context.out.println(result); + }, reply -> { + String errMsg = (String) ManagementHelper.getResult(reply, String.class); + context.err.println("Failed to create queue " + getName() + ". Reason: " + errMsg); }); } } diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/queue/DeleteQueue.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/queue/DeleteQueue.java index 0da284d28b..38e994d7d1 100644 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/queue/DeleteQueue.java +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/queue/DeleteQueue.java @@ -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.Option; -import org.apache.activemq.artemis.api.core.client.ClientMessage; import org.apache.activemq.artemis.api.core.management.ManagementHelper; 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.") -public class DeleteQueue extends AbstractAction { +public class DeleteQueue extends ConnectionAbstract { @Option(name = "--name", description = "The queue's name") String name; @@ -44,22 +43,13 @@ public class DeleteQueue extends AbstractAction { } private void deleteQueue(final ActionContext context) throws Exception { - performCoreManagement(new ManagementCallback() { - @Override - public void setUpInvocation(ClientMessage message) throws Exception { - ManagementHelper.putOperationInvocation(message, "broker", "destroyQueue", getName(), removeConsumers, autoDeleteAddress); - } - - @Override - 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); - } + performCoreManagement(message -> { + ManagementHelper.putOperationInvocation(message, "broker", "destroyQueue", getName(), removeConsumers, autoDeleteAddress); + }, reply -> { + context.out.println("Queue " + getName() + " deleted successfully."); + }, reply -> { + String errMsg = (String) ManagementHelper.getResult(reply, String.class); + context.err.println("Failed to delete queue " + getName() + ". Reason: " + errMsg); }); } diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/queue/PurgeQueue.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/queue/PurgeQueue.java index cfe210476f..09d217da3d 100644 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/queue/PurgeQueue.java +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/queue/PurgeQueue.java @@ -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.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.ResourceNames; 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.") -public class PurgeQueue extends AbstractAction { +public class PurgeQueue extends ConnectionAbstract { @Option(name = "--name", description = "The queue's name.") String name; @@ -39,22 +38,13 @@ public class PurgeQueue extends AbstractAction { } private void purgeQueue(final ActionContext context) throws Exception { - performCoreManagement(new ManagementCallback() { - @Override - public void setUpInvocation(ClientMessage message) throws Exception { - ManagementHelper.putOperationInvocation(message, ResourceNames.QUEUE + getName(), "removeAllMessages"); - } - - @Override - 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); - } + performCoreManagement(message -> { + ManagementHelper.putOperationInvocation(message, ResourceNames.QUEUE + getName(), "removeAllMessages"); + }, reply -> { + context.out.println("Queue " + getName() + " purged successfully."); + }, reply -> { + String errMsg = (String) ManagementHelper.getResult(reply, String.class); + context.err.println("Failed to purge queue " + getName() + ". Reason: " + errMsg); }); } diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/queue/QueueAbstract.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/queue/QueueAbstract.java index dbc33ed077..69bb869b4a 100644 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/queue/QueueAbstract.java +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/queue/QueueAbstract.java @@ -17,9 +17,9 @@ package org.apache.activemq.artemis.cli.commands.queue; 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.") private String name; diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/queue/StatQueue.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/queue/StatQueue.java index b1c9605e2b..17b2ed70ec 100644 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/queue/StatQueue.java +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/queue/StatQueue.java @@ -24,15 +24,14 @@ import java.util.TreeMap; import com.github.rvesse.airline.annotations.Command; import com.github.rvesse.airline.annotations.Option; 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.cli.commands.AbstractAction; 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.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'.") -public class StatQueue extends AbstractAction { +public class StatQueue extends ConnectionAbstract { 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"); @@ -152,23 +151,14 @@ public class StatQueue extends AbstractAction { } private void printStats(final ActionContext context, final String filter) throws Exception { - performCoreManagement(new ManagementCallback() { - @Override - public void setUpInvocation(ClientMessage message) throws Exception { - ManagementHelper.putOperationInvocation(message, "broker", "listQueues", filter, 1, maxRows); - } - - @Override - public void requestSuccessful(ClientMessage reply) throws Exception { - 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); - } + performCoreManagement(message -> { + ManagementHelper.putOperationInvocation(message, "broker", "listQueues", filter, 1, maxRows); + }, reply -> { + final String result = (String) ManagementHelper.getResult(reply, String.class); + printStats(result); + }, reply -> { + String errMsg = (String) ManagementHelper.getResult(reply, String.class); + getActionContext().err.println("Failed to get Stats for Queues. Reason: " + errMsg); }); } diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/queue/UpdateQueue.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/queue/UpdateQueue.java index c6d4c1eae1..416fbc61cc 100644 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/queue/UpdateQueue.java +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/queue/UpdateQueue.java @@ -17,7 +17,6 @@ package org.apache.activemq.artemis.cli.commands.queue; 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.cli.commands.ActionContext; @@ -32,23 +31,14 @@ public class UpdateQueue extends QueueAbstract { } private void updateQueue(final ActionContext context) throws Exception { - performCoreManagement(new ManagementCallback() { - @Override - public void setUpInvocation(ClientMessage message) throws Exception { - ManagementHelper.putOperationInvocation(message, "broker", "updateQueue", getName(), getRoutingType(), getMaxConsumers(null), isPurgeOnNoConsumers()); - } - - @Override - public void requestSuccessful(ClientMessage reply) throws Exception { - 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); - } + performCoreManagement(message -> { + ManagementHelper.putOperationInvocation(message, "broker", "updateQueue", getName(), getRoutingType(), getMaxConsumers(null), isPurgeOnNoConsumers()); + }, reply -> { + final String result = ManagementHelper.getResult(reply, String.class) + " updated successfully."; + context.out.println(result); + }, reply -> { + String errMsg = (String) ManagementHelper.getResult(reply, String.class); + context.err.println("Failed to update " + getName() + ". Reason: " + errMsg); }); } } diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/AddUser.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/AddUser.java index 418fa504f5..73f7cf6340 100644 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/AddUser.java +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/AddUser.java @@ -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.Option; -import org.apache.activemq.artemis.api.core.client.ClientMessage; 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; /** @@ -49,22 +47,13 @@ public class AddUser extends PasswordAction { * @throws Exception if communication with the broker fails */ private void add() throws Exception { - performCoreManagement(new AbstractAction.ManagementCallback() { - @Override - public void setUpInvocation(ClientMessage message) throws Exception { - ManagementHelper.putOperationInvocation(message, "broker", "addUser", userCommandUser, userCommandPassword, role, plaintext); - } - - @Override - 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); - } + performCoreManagement(message -> { + ManagementHelper.putOperationInvocation(message, "broker", "addUser", userCommandUser, userCommandPassword, role, plaintext); + }, reply -> { + getActionContext().out.println(userCommandUser + " added successfully."); + }, reply -> { + String errMsg = (String) ManagementHelper.getResult(reply, String.class); + getActionContext().err.println("Failed to add user " + userCommandUser + ". Reason: " + errMsg); }); } } diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/ListUser.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/ListUser.java index 6bd60f116d..a01cbf00a0 100644 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/ListUser.java +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/ListUser.java @@ -21,9 +21,7 @@ import org.apache.activemq.artemis.json.JsonObject; import com.github.rvesse.airline.annotations.Command; 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.cli.commands.AbstractAction; import org.apache.activemq.artemis.cli.commands.ActionContext; /** @@ -52,22 +50,13 @@ public class ListUser extends UserAction { int userCount = 0; final String[] result = new String[1]; - performCoreManagement(new AbstractAction.ManagementCallback() { - @Override - public void setUpInvocation(ClientMessage message) throws Exception { - ManagementHelper.putOperationInvocation(message, "broker", "listUser", userCommandUser); - } - - @Override - 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); - } + performCoreManagement(message -> { + ManagementHelper.putOperationInvocation(message, "broker", "listUser", userCommandUser); + }, reply -> { + result[0] = (String) ManagementHelper.getResult(reply, String.class); + }, reply -> { + 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 diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/RemoveUser.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/RemoveUser.java index fa7c9d6411..9073dab1f8 100644 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/RemoveUser.java +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/RemoveUser.java @@ -17,9 +17,7 @@ package org.apache.activemq.artemis.cli.commands.user; 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.cli.commands.AbstractAction; import org.apache.activemq.artemis.cli.commands.ActionContext; /** @@ -38,22 +36,13 @@ public class RemoveUser extends UserAction { } private void remove() throws Exception { - performCoreManagement(new AbstractAction.ManagementCallback() { - @Override - public void setUpInvocation(ClientMessage message) throws Exception { - ManagementHelper.putOperationInvocation(message, "broker", "removeUser", userCommandUser); - } - - @Override - 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); - } + performCoreManagement(message -> { + ManagementHelper.putOperationInvocation(message, "broker", "removeUser", userCommandUser); + }, reply -> { + getActionContext().out.println(userCommandUser + " removed successfully."); + }, reply -> { + String errMsg = (String) ManagementHelper.getResult(reply, String.class); + getActionContext().err.println("Failed to remove user " + userCommandUser + ". Reason: " + errMsg); }); } diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/ResetUser.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/ResetUser.java index ae47b9ba0b..e221b8ea03 100644 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/ResetUser.java +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/ResetUser.java @@ -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.Option; -import org.apache.activemq.artemis.api.core.client.ClientMessage; 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; /** @@ -43,22 +41,13 @@ public class ResetUser extends PasswordAction { } private void reset() throws Exception { - performCoreManagement(new AbstractAction.ManagementCallback() { - @Override - public void setUpInvocation(ClientMessage message) throws Exception { - ManagementHelper.putOperationInvocation(message, "broker", "resetUser", userCommandUser, userCommandPassword, role, plaintext); - } - - @Override - 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); - } + performCoreManagement(message -> { + ManagementHelper.putOperationInvocation(message, "broker", "resetUser", userCommandUser, userCommandPassword, role, plaintext); + }, reply -> { + getActionContext().out.println(userCommandUser + " reset successfully."); + }, reply -> { + String errMsg = (String) ManagementHelper.getResult(reply, String.class); + getActionContext().err.println("Failed to reset user " + userCommandUser + ". Reason: " + errMsg); }); } } diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/UserAction.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/UserAction.java index 106ea74b36..50fcecd5e4 100644 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/UserAction.java +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/UserAction.java @@ -17,9 +17,9 @@ package org.apache.activemq.artemis.cli.commands.user; 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.") String role; diff --git a/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/management/ManagementHelper.java b/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/management/ManagementHelper.java index 69a16a8f4b..1e6eff0be4 100644 --- a/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/management/ManagementHelper.java +++ b/artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/management/ManagementHelper.java @@ -99,10 +99,14 @@ public final class ManagementHelper { 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 { - try (ServerLocator locator = ServerLocatorImpl.newLocator(uri); - ClientSessionFactory sessionFactory = locator.createSessionFactory(); + try (ServerLocator locator = ServerLocatorImpl.newLocator(uri)) { + 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)) { doManagement(session, setup, ok, failed); } diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/cli/AddressCommandTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/cli/AddressCommandTest.java index 82d32440c7..3c9d487ab7 100644 --- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/cli/AddressCommandTest.java +++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/cli/AddressCommandTest.java @@ -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.ClientSessionFactory; 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.address.CreateAddress; 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.UpdateAddress; +import org.apache.activemq.artemis.cli.commands.messages.ConnectionAbstract; import org.apache.activemq.artemis.core.config.DivertConfiguration; import org.apache.activemq.artemis.api.core.RoutingType; import org.apache.activemq.artemis.core.server.Queue; @@ -241,13 +241,13 @@ public class AddressCommandTest extends JMSTestBase { checkExecutionFailure(updateAddress, expectedErrorMessage); } - private void checkExecutionPassed(AbstractAction command) throws Exception { + private void checkExecutionPassed(ConnectionAbstract command) throws Exception { String fullMessage = output.toString(); logger.debug("output: {}", fullMessage); 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(); logger.debug("error: {}", fullMessage); assertTrue(fullMessage, fullMessage.contains(message)); diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/cli/QueueCommandTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/cli/QueueCommandTest.java index c14771b2e2..a933b16104 100644 --- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/cli/QueueCommandTest.java +++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/cli/QueueCommandTest.java @@ -25,8 +25,8 @@ import java.util.UUID; import org.apache.activemq.artemis.api.core.QueueConfiguration; 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.messages.ConnectionAbstract; 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.PurgeQueue; @@ -382,13 +382,13 @@ public class QueueCommandTest extends JMSTestBase { assertFalse(server.queueQuery(queueName).isExists()); } - private void checkExecutionPassed(AbstractAction command) throws Exception { + private void checkExecutionPassed(ConnectionAbstract command) throws Exception { String fullMessage = output.toString(); logger.debug("output: {}", fullMessage); 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(); logger.debug("error: {}", fullMessage); assertTrue(fullMessage, fullMessage.contains(message));