This commit is contained in:
Clebert Suconic 2018-08-03 14:19:48 -04:00
commit d13b42def7
3 changed files with 104 additions and 2 deletions

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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);