mirror of https://github.com/apache/activemq.git
added reset option to clear destination statistics
This commit is contained in:
parent
d919db5e3d
commit
c8b604314a
|
@ -16,24 +16,16 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.activemq.console.command;
|
package org.apache.activemq.console.command;
|
||||||
|
|
||||||
import java.net.URI;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.StringTokenizer;
|
import java.util.StringTokenizer;
|
||||||
|
|
||||||
import javax.jms.Destination;
|
|
||||||
import javax.jms.Message;
|
|
||||||
import javax.management.MBeanServerConnection;
|
|
||||||
import javax.management.MBeanServerInvocationHandler;
|
import javax.management.MBeanServerInvocationHandler;
|
||||||
import javax.management.ObjectInstance;
|
import javax.management.ObjectInstance;
|
||||||
import javax.management.ObjectName;
|
import javax.management.ObjectName;
|
||||||
import javax.management.openmbean.CompositeData;
|
|
||||||
import javax.management.remote.JMXConnector;
|
|
||||||
|
|
||||||
import org.apache.activemq.broker.jmx.QueueViewMBean;
|
import org.apache.activemq.broker.jmx.QueueViewMBean;
|
||||||
import org.apache.activemq.command.ActiveMQQueue;
|
|
||||||
import org.apache.activemq.console.util.AmqMessagesUtil;
|
|
||||||
import org.apache.activemq.console.util.JmxMBeansUtil;
|
import org.apache.activemq.console.util.JmxMBeansUtil;
|
||||||
|
|
||||||
public class PurgeCommand extends AbstractJmxCommand {
|
public class PurgeCommand extends AbstractJmxCommand {
|
||||||
|
@ -45,6 +37,7 @@ public class PurgeCommand extends AbstractJmxCommand {
|
||||||
"Purge Options:",
|
"Purge Options:",
|
||||||
" --msgsel <msgsel1,msglsel2> Add to the search list messages matched by the query similar to",
|
" --msgsel <msgsel1,msglsel2> Add to the search list messages matched by the query similar to",
|
||||||
" the messages selector format.",
|
" the messages selector format.",
|
||||||
|
" --reset After the purge operation, reset the destination statistics.",
|
||||||
" --jmxurl <url> Set the JMX URL to connect to.",
|
" --jmxurl <url> Set the JMX URL to connect to.",
|
||||||
" --pid <pid> Set the pid to connect to (only on Sun JVM).",
|
" --pid <pid> Set the pid to connect to (only on Sun JVM).",
|
||||||
" --jmxuser <user> Set the JMX user used for authenticating.",
|
" --jmxuser <user> Set the JMX user used for authenticating.",
|
||||||
|
@ -69,6 +62,7 @@ public class PurgeCommand extends AbstractJmxCommand {
|
||||||
|
|
||||||
private final List<String> queryAddObjects = new ArrayList<String>(10);
|
private final List<String> queryAddObjects = new ArrayList<String>(10);
|
||||||
private final List<String> querySubObjects = new ArrayList<String>(10);
|
private final List<String> querySubObjects = new ArrayList<String>(10);
|
||||||
|
private boolean resetStatistics;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
|
@ -87,6 +81,7 @@ public class PurgeCommand extends AbstractJmxCommand {
|
||||||
* @param tokens - command arguments
|
* @param tokens - command arguments
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
protected void runTask(List<String> tokens) throws Exception {
|
protected void runTask(List<String> tokens) throws Exception {
|
||||||
try {
|
try {
|
||||||
// If there is no queue name specified, let's select all
|
// If there is no queue name specified, let's select all
|
||||||
|
@ -104,7 +99,7 @@ public class PurgeCommand extends AbstractJmxCommand {
|
||||||
purgeQueue(queueName);
|
purgeQueue(queueName);
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
QueueViewMBean proxy = (QueueViewMBean) MBeanServerInvocationHandler.
|
QueueViewMBean proxy = MBeanServerInvocationHandler.
|
||||||
newProxyInstance(createJmxConnection(),
|
newProxyInstance(createJmxConnection(),
|
||||||
queueName,
|
queueName,
|
||||||
QueueViewMBean.class,
|
QueueViewMBean.class,
|
||||||
|
@ -133,6 +128,10 @@ public class PurgeCommand extends AbstractJmxCommand {
|
||||||
removed = proxy.removeMatchingMessages(sqlQuery);
|
removed = proxy.removeMatchingMessages(sqlQuery);
|
||||||
context.printInfo("Removed: " + removed
|
context.printInfo("Removed: " + removed
|
||||||
+ " messages for message selector " + sqlQuery.toString());
|
+ " messages for message selector " + sqlQuery.toString());
|
||||||
|
|
||||||
|
if (resetStatistics) {
|
||||||
|
proxy.resetStatistics();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -152,6 +151,9 @@ public class PurgeCommand extends AbstractJmxCommand {
|
||||||
public void purgeQueue(ObjectName queue) throws Exception {
|
public void purgeQueue(ObjectName queue) throws Exception {
|
||||||
context.printInfo("Purging all messages in queue: " + queue.getKeyProperty("destinationName"));
|
context.printInfo("Purging all messages in queue: " + queue.getKeyProperty("destinationName"));
|
||||||
createJmxConnection().invoke(queue, "purge", new Object[] {}, new String[] {});
|
createJmxConnection().invoke(queue, "purge", new Object[] {}, new String[] {});
|
||||||
|
if (resetStatistics) {
|
||||||
|
createJmxConnection().invoke(queue, "resetStatistics", new Object[] {}, new String[] {});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -161,18 +163,19 @@ public class PurgeCommand extends AbstractJmxCommand {
|
||||||
* @param tokens - succeeding command arguments
|
* @param tokens - succeeding command arguments
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
protected void handleOption(String token, List<String> tokens) throws Exception {
|
protected void handleOption(String token, List<String> tokens) throws Exception {
|
||||||
// If token is an additive message selector option
|
// If token is an additive message selector option
|
||||||
if (token.startsWith("--msgsel")) {
|
if (token.startsWith("--msgsel")) {
|
||||||
|
|
||||||
// If no message selector is specified, or next token is a new
|
// If no message selector is specified, or next token is a new
|
||||||
// option
|
// option
|
||||||
if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) {
|
if (tokens.isEmpty() || tokens.get(0).startsWith("-")) {
|
||||||
context.printException(new IllegalArgumentException("Message selector not specified"));
|
context.printException(new IllegalArgumentException("Message selector not specified"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
StringTokenizer queryTokens = new StringTokenizer((String)tokens.remove(0), COMMAND_OPTION_DELIMETER);
|
StringTokenizer queryTokens = new StringTokenizer(tokens.remove(0), COMMAND_OPTION_DELIMETER);
|
||||||
while (queryTokens.hasMoreTokens()) {
|
while (queryTokens.hasMoreTokens()) {
|
||||||
queryAddObjects.add(queryTokens.nextToken());
|
queryAddObjects.add(queryTokens.nextToken());
|
||||||
}
|
}
|
||||||
|
@ -181,16 +184,17 @@ public class PurgeCommand extends AbstractJmxCommand {
|
||||||
|
|
||||||
// If no message selector is specified, or next token is a new
|
// If no message selector is specified, or next token is a new
|
||||||
// option
|
// option
|
||||||
if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) {
|
if (tokens.isEmpty() || tokens.get(0).startsWith("-")) {
|
||||||
context.printException(new IllegalArgumentException("Message selector not specified"));
|
context.printException(new IllegalArgumentException("Message selector not specified"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
StringTokenizer queryTokens = new StringTokenizer((String)tokens.remove(0), COMMAND_OPTION_DELIMETER);
|
StringTokenizer queryTokens = new StringTokenizer(tokens.remove(0), COMMAND_OPTION_DELIMETER);
|
||||||
while (queryTokens.hasMoreTokens()) {
|
while (queryTokens.hasMoreTokens()) {
|
||||||
querySubObjects.add(queryTokens.nextToken());
|
querySubObjects.add(queryTokens.nextToken());
|
||||||
}
|
}
|
||||||
|
} else if (token.startsWith("--reset")) {
|
||||||
|
resetStatistics = true;
|
||||||
} else {
|
} else {
|
||||||
// Let super class handle unknown option
|
// Let super class handle unknown option
|
||||||
super.handleOption(token, tokens);
|
super.handleOption(token, tokens);
|
||||||
|
@ -228,6 +232,7 @@ public class PurgeCommand extends AbstractJmxCommand {
|
||||||
/**
|
/**
|
||||||
* Print the help messages for the browse command
|
* Print the help messages for the browse command
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
protected void printHelp() {
|
protected void printHelp() {
|
||||||
context.printHelp(helpFile);
|
context.printHelp(helpFile);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,188 @@
|
||||||
|
/**
|
||||||
|
* 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.console;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
import javax.jms.Connection;
|
||||||
|
import javax.jms.ConnectionFactory;
|
||||||
|
import javax.jms.JMSException;
|
||||||
|
import javax.jms.MessageConsumer;
|
||||||
|
import javax.jms.MessageProducer;
|
||||||
|
import javax.jms.Queue;
|
||||||
|
import javax.jms.Session;
|
||||||
|
import javax.management.MalformedObjectNameException;
|
||||||
|
import javax.management.ObjectName;
|
||||||
|
|
||||||
|
import org.apache.activemq.ActiveMQConnectionFactory;
|
||||||
|
import org.apache.activemq.broker.BrokerService;
|
||||||
|
import org.apache.activemq.broker.TransportConnector;
|
||||||
|
import org.apache.activemq.broker.jmx.QueueViewMBean;
|
||||||
|
import org.apache.activemq.console.command.PurgeCommand;
|
||||||
|
import org.apache.activemq.console.formatter.CommandShellOutputFormatter;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Rule;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.TestName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for the purge command.
|
||||||
|
*/
|
||||||
|
public class PurgeCommandTest {
|
||||||
|
|
||||||
|
private BrokerService brokerService;
|
||||||
|
private ConnectionFactory factory;
|
||||||
|
|
||||||
|
@Rule public TestName name = new TestName();
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void createBroker() throws Exception {
|
||||||
|
brokerService = new BrokerService();
|
||||||
|
brokerService.getManagementContext().setCreateConnector(false);
|
||||||
|
brokerService.setPersistent(false);
|
||||||
|
TransportConnector connector = brokerService.addConnector("tcp://0.0.0.0:0");
|
||||||
|
brokerService.start();
|
||||||
|
brokerService.waitUntilStarted();
|
||||||
|
|
||||||
|
factory = new ActiveMQConnectionFactory(connector.getPublishableConnectString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void stopBroker() throws Exception {
|
||||||
|
if (brokerService != null) {
|
||||||
|
brokerService.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(timeout = 30000)
|
||||||
|
public void testPurge() throws Exception {
|
||||||
|
produce(10);
|
||||||
|
|
||||||
|
QueueViewMBean queueView = getProxyToQueue(getDestinationName());
|
||||||
|
assertEquals(10, queueView.getQueueSize());
|
||||||
|
|
||||||
|
executePurge(getDestinationName());
|
||||||
|
|
||||||
|
assertEquals(0, queueView.getQueueSize());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(timeout = 30000)
|
||||||
|
public void testPurgeWithReset() throws Exception {
|
||||||
|
produce(20);
|
||||||
|
consume(10);
|
||||||
|
|
||||||
|
QueueViewMBean queueView = getProxyToQueue(getDestinationName());
|
||||||
|
assertEquals(10, queueView.getQueueSize());
|
||||||
|
assertEquals(20, queueView.getEnqueueCount());
|
||||||
|
assertEquals(10, queueView.getDequeueCount());
|
||||||
|
|
||||||
|
// Normal purge doesn't change stats.
|
||||||
|
executePurge(getDestinationName());
|
||||||
|
|
||||||
|
assertEquals(0, queueView.getQueueSize());
|
||||||
|
assertEquals(20, queueView.getEnqueueCount());
|
||||||
|
assertEquals(20, queueView.getDequeueCount());
|
||||||
|
|
||||||
|
// Purge on empty leaves stats alone.
|
||||||
|
executePurge(getDestinationName());
|
||||||
|
|
||||||
|
assertEquals(0, queueView.getQueueSize());
|
||||||
|
assertEquals(20, queueView.getEnqueueCount());
|
||||||
|
assertEquals(20, queueView.getDequeueCount());
|
||||||
|
|
||||||
|
executePurge("--reset " + getDestinationName());
|
||||||
|
|
||||||
|
// Purge on empty with reset clears stats.
|
||||||
|
assertEquals(0, queueView.getQueueSize());
|
||||||
|
assertEquals(0, queueView.getEnqueueCount());
|
||||||
|
assertEquals(0, queueView.getDequeueCount());
|
||||||
|
|
||||||
|
produce(20);
|
||||||
|
consume(10);
|
||||||
|
|
||||||
|
assertEquals(10, queueView.getQueueSize());
|
||||||
|
assertEquals(20, queueView.getEnqueueCount());
|
||||||
|
assertEquals(10, queueView.getDequeueCount());
|
||||||
|
|
||||||
|
executePurge("--reset " + getDestinationName());
|
||||||
|
|
||||||
|
// Purge on non-empty with reset clears stats.
|
||||||
|
assertEquals(0, queueView.getQueueSize());
|
||||||
|
assertEquals(0, queueView.getEnqueueCount());
|
||||||
|
assertEquals(0, queueView.getDequeueCount());
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getDestinationName() {
|
||||||
|
return name.getMethodName();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String executePurge(String options) throws Exception {
|
||||||
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
|
||||||
|
CommandContext context = new CommandContext();
|
||||||
|
context.setFormatter(new CommandShellOutputFormatter(byteArrayOutputStream));
|
||||||
|
|
||||||
|
PurgeCommand purgeCommand = new PurgeCommand();
|
||||||
|
purgeCommand.setJmxUseLocal(true);
|
||||||
|
purgeCommand.setCommandContext(context);
|
||||||
|
|
||||||
|
LinkedList<String> args = new LinkedList<>();
|
||||||
|
args.addAll(Arrays.asList(options.split(" ")));
|
||||||
|
purgeCommand.execute(args);
|
||||||
|
|
||||||
|
return byteArrayOutputStream.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private QueueViewMBean getProxyToQueue(String name) throws MalformedObjectNameException, JMSException {
|
||||||
|
ObjectName queueViewMBeanName = new ObjectName("org.apache.activemq:type=Broker,brokerName=localhost,destinationType=Queue,destinationName="+name);
|
||||||
|
QueueViewMBean proxy = (QueueViewMBean) brokerService.getManagementContext()
|
||||||
|
.newProxyInstance(queueViewMBeanName, QueueViewMBean.class, true);
|
||||||
|
return proxy;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void produce(int count) throws Exception {
|
||||||
|
Connection connection = factory.createConnection();
|
||||||
|
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
|
||||||
|
Queue queue = session.createQueue(getDestinationName());
|
||||||
|
MessageProducer producer = session.createProducer(queue);
|
||||||
|
|
||||||
|
for (int i = 0; i < count; ++i) {
|
||||||
|
producer.send(session.createMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
connection.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void consume(int count) throws Exception {
|
||||||
|
Connection connection = factory.createConnection();
|
||||||
|
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
|
||||||
|
Queue queue = session.createQueue(getDestinationName());
|
||||||
|
MessageConsumer consumer = session.createConsumer(queue);
|
||||||
|
connection.start();
|
||||||
|
|
||||||
|
for (int i = 0; i < count; ++i) {
|
||||||
|
assertNotNull(consumer.receive(1000));
|
||||||
|
}
|
||||||
|
|
||||||
|
connection.close();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue