Repository: activemq-artemis Updated Branches: refs/heads/master ed643e479 -> d13b42def
[ARTEMIS-2008]Add a CLI command to purge queue Project: http://git-wip-us.apache.org/repos/asf/activemq-artemis/repo Commit: http://git-wip-us.apache.org/repos/asf/activemq-artemis/commit/3c5b050d Tree: http://git-wip-us.apache.org/repos/asf/activemq-artemis/tree/3c5b050d Diff: http://git-wip-us.apache.org/repos/asf/activemq-artemis/diff/3c5b050d Branch: refs/heads/master Commit: 3c5b050dff611ca1bb315a3424de2707053c12c8 Parents: ed643e4 Author: Shailendra Kumar Singh <[email protected]> Authored: Thu Aug 2 09:10:46 2018 +0530 Committer: Clebert Suconic <[email protected]> Committed: Fri Aug 3 14:19:47 2018 -0400 ---------------------------------------------------------------------- .../apache/activemq/artemis/cli/Artemis.java | 5 +- .../artemis/cli/commands/queue/PurgeQueue.java | 72 ++++++++++++++++++++ .../tests/integration/cli/QueueCommandTest.java | 29 ++++++++ 3 files changed, 104 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/3c5b050d/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/Artemis.java ---------------------------------------------------------------------- diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/Artemis.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/Artemis.java index 556bcd8..657055e 100644 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/Artemis.java +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/Artemis.java @@ -46,6 +46,7 @@ import org.apache.activemq.artemis.cli.commands.migration1x.Migrate1X; 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.HelpQueue; +import org.apache.activemq.artemis.cli.commands.queue.PurgeQueue; import org.apache.activemq.artemis.cli.commands.queue.UpdateQueue; import org.apache.activemq.artemis.cli.commands.tools.HelpData; import org.apache.activemq.artemis.cli.commands.tools.PrintData; @@ -153,8 +154,8 @@ public class Artemis { String instance = artemisInstance != null ? artemisInstance.getAbsolutePath() : System.getProperty("artemis.instance"); Cli.CliBuilder<Action> builder = Cli.<Action>builder("artemis").withDescription("ActiveMQ Artemis Command Line").withCommand(HelpAction.class).withCommand(Producer.class).withCommand(Consumer.class).withCommand(Browse.class).withCommand(Mask.class).withDefaultCommand(HelpAction.class); - builder.withGroup("queue").withDescription("Queue tools group (create|delete|update|stat) (example ./artemis queue create)"). - withDefaultCommand(HelpQueue.class).withCommands(CreateQueue.class, DeleteQueue.class, UpdateQueue.class, StatQueue.class); + builder.withGroup("queue").withDescription("Queue tools group (create|delete|update|stat|purge) (example ./artemis queue create)"). + withDefaultCommand(HelpQueue.class).withCommands(CreateQueue.class, DeleteQueue.class, UpdateQueue.class, StatQueue.class, PurgeQueue.class); builder.withGroup("address").withDescription("Address tools group (create|delete|update|show) (example ./artemis address create)"). withDefaultCommand(HelpAddress.class).withCommands(CreateAddress.class, DeleteAddress.class, UpdateAddress.class, ShowAddress.class); http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/3c5b050d/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/queue/PurgeQueue.java ---------------------------------------------------------------------- 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 new file mode 100644 index 0000000..f5d3d7e --- /dev/null +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/queue/PurgeQueue.java @@ -0,0 +1,72 @@ +/* + * 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.queue; + +import io.airlift.airline.Command; +import io.airlift.airline.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; + +@Command(name = "purge", description = "purge a queue") +public class PurgeQueue extends AbstractAction { + + @Option(name = "--name", description = "queue name") + String name; + + @Override + public Object execute(ActionContext context) throws Exception { + super.execute(context); + purgeQueue(context); + return null; + } + + private void purgeQueue(final ActionContext context) throws Exception { + performCoreManagement(new ManagementCallback<ClientMessage>() { + @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); + } + }); + } + + public void setName(String name) { + this.name = name; + } + + public String getName() { + if (name == null) { + name = input("--name", "Please provide the destination name:", ""); + } + + return name; + } +} http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/3c5b050d/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/cli/QueueCommandTest.java ---------------------------------------------------------------------- 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 b4e8fb6..c581e69 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 @@ -27,6 +27,7 @@ import org.apache.activemq.artemis.cli.commands.AbstractAction; import org.apache.activemq.artemis.cli.commands.ActionContext; 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; import org.apache.activemq.artemis.cli.commands.queue.UpdateQueue; import org.apache.activemq.artemis.core.server.Queue; import org.apache.activemq.artemis.core.server.QueueQueryResult; @@ -339,6 +340,34 @@ public class QueueCommandTest extends JMSTestBase { assertFalse(server.queueQuery(queueName).isExists()); } + @Test + public void testPurgeQueue() throws Exception { + SimpleString queueName = new SimpleString("purgeQueue"); + + CreateQueue command = new CreateQueue(); + command.setName(queueName.toString()); + command.setAutoCreateAddress(true); + command.setAnycast(true); + command.execute(new ActionContext()); + + PurgeQueue purge = new PurgeQueue(); + purge.setName(queueName.toString()); + purge.execute(new ActionContext(System.in, new PrintStream(output), new PrintStream(error))); + checkExecutionPassed(purge); + } + + @Test + public void testPurgeQueueDoesNotExist() throws Exception { + SimpleString queueName = new SimpleString("purgeQueue"); + + PurgeQueue purge = new PurgeQueue(); + purge.setName(queueName.toString()); + purge.execute(new ActionContext(System.in, new PrintStream(output), new PrintStream(error))); + checkExecutionFailure(purge, "AMQ119067: Cannot find resource with name queue." + queueName); + + assertFalse(server.queueQuery(queueName).isExists()); + } + private void checkExecutionPassed(AbstractAction command) throws Exception { String fullMessage = output.toString(); System.out.println("output: " + fullMessage);
