mirror of https://github.com/apache/activemq.git
added working test case for the use of Broker, Queue, Topic MBeans
git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@384274 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
138d9cfffa
commit
6c0fae01de
|
@ -21,59 +21,67 @@ import org.apache.activemq.EmbeddedBrokerTestSupport;
|
|||
import org.apache.activemq.broker.BrokerService;
|
||||
|
||||
import javax.jms.Connection;
|
||||
import javax.jms.Destination;
|
||||
import javax.jms.Message;
|
||||
import javax.jms.MessageConsumer;
|
||||
import javax.jms.MessageProducer;
|
||||
import javax.jms.Session;
|
||||
import javax.management.MBeanServer;
|
||||
import javax.management.MBeanServerInvocationHandler;
|
||||
import javax.management.MalformedObjectNameException;
|
||||
import javax.management.ObjectName;
|
||||
import javax.management.openmbean.CompositeData;
|
||||
import javax.management.openmbean.TabularData;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import junit.textui.TestRunner;
|
||||
|
||||
/**
|
||||
* A test case of the various MBeans in ActiveMQ.
|
||||
* If you want to look at the various MBeans after the test has been run then
|
||||
* run this test case as a command line application.
|
||||
*
|
||||
* @version $Revision$
|
||||
*/
|
||||
public class MBeanTest extends EmbeddedBrokerTestSupport {
|
||||
|
||||
private static boolean waitForKeyPress;
|
||||
|
||||
protected MBeanServer mbeanServer;
|
||||
protected String domain = "org.apache.activemq";
|
||||
protected String clientID = "foo";
|
||||
|
||||
protected Connection connection;
|
||||
protected boolean transacted;
|
||||
protected int authMode = Session.AUTO_ACKNOWLEDGE;
|
||||
protected int messageCount = 10;
|
||||
|
||||
public void testDummy() throws Exception {
|
||||
/**
|
||||
* When you run this test case from the command line it will pause before terminating
|
||||
* so that you can look at the MBeans state for debugging purposes.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
waitForKeyPress = true;
|
||||
TestRunner.run(MBeanTest.class);
|
||||
}
|
||||
|
||||
public void XXXX_testMBeans() throws Exception {
|
||||
public void testMBeans() throws Exception {
|
||||
connection = connectionFactory.createConnection();
|
||||
useConnection(connection);
|
||||
|
||||
// test all the various MBeans now we have a producer, consumer and
|
||||
// messages on a queue
|
||||
assertQueueBrowseWorks();
|
||||
assertCreateAndDestroyDurableSubscriptions();
|
||||
}
|
||||
|
||||
protected void assertQueueBrowseWorks() throws Exception {
|
||||
|
||||
Integer mbeancnt = mbeanServer.getMBeanCount();
|
||||
echo("Mbean count :" + mbeancnt);
|
||||
|
||||
ObjectName queueViewMBeanName = new ObjectName(domain + ":Type=Queue,Destination=" + getDestinationString() + ",BrokerName=localhost");
|
||||
|
||||
if (mbeanServer.isRegistered(queueViewMBeanName)) {
|
||||
echo("Bean Registered: " + queueViewMBeanName);
|
||||
}
|
||||
else {
|
||||
fail("Could not find MBean!: " + queueViewMBeanName);
|
||||
}
|
||||
|
||||
echo("\nCreate QueueView MBean...");
|
||||
ObjectName queueViewMBeanName = assertRegisteredObjectName(domain + ":Type=Queue,Destination=" + getDestinationString() + ",BrokerName=localhost");
|
||||
|
||||
echo("Create QueueView MBean...");
|
||||
QueueViewMBean proxy = (QueueViewMBean) MBeanServerInvocationHandler.newProxyInstance(mbeanServer, queueViewMBeanName, QueueViewMBean.class, true);
|
||||
|
||||
long concount = proxy.getConsumerCount();
|
||||
|
@ -81,32 +89,75 @@ public class MBeanTest extends EmbeddedBrokerTestSupport {
|
|||
long messcount = proxy.getQueueSize();
|
||||
echo("current number of messages in the queue :" + messcount);
|
||||
|
||||
|
||||
|
||||
// lets browse
|
||||
CompositeData[] compdatalist = proxy.browse();
|
||||
if (compdatalist.length == 0) {
|
||||
fail("There is no message in the queue:");
|
||||
}
|
||||
else {
|
||||
for (int i = 0; i < compdatalist.length; i++) {
|
||||
CompositeData cdata = compdatalist[i];
|
||||
echo("message " + i + " : " + cdata.toString());
|
||||
String[] messageIDs = new String[compdatalist.length];
|
||||
|
||||
for (int i = 0; i < compdatalist.length; i++) {
|
||||
CompositeData cdata = compdatalist[i];
|
||||
|
||||
if (i == 0) {
|
||||
echo("Columns: " + cdata.getCompositeType().keySet());
|
||||
}
|
||||
messageIDs[i] = (String) cdata.get("JMSMessageID");
|
||||
echo("message " + i + " : " + cdata.values());
|
||||
}
|
||||
|
||||
TabularData table = proxy.browseAsTable();
|
||||
echo("Found tabular data: " + table);
|
||||
assertTrue("Table should not be empty!", table.size() > 0);
|
||||
|
||||
|
||||
/*
|
||||
String messageID = null;
|
||||
assertEquals("Queue size", 10, proxy.getQueueSize());
|
||||
|
||||
String messageID = messageIDs[0];
|
||||
String newDestinationName = "queue://dummy.test.cheese";
|
||||
echo("Attempting to copy: " + messageID + " to destination: " + newDestinationName);
|
||||
proxy.copyMessageTo(messageID, newDestinationName);
|
||||
proxy.removeMessage(messageID);
|
||||
*/
|
||||
|
||||
assertEquals("Queue size", 10, proxy.getQueueSize());
|
||||
|
||||
messageID = messageIDs[1];
|
||||
echo("Attempting to remove: " + messageID);
|
||||
proxy.removeMessage(messageID);
|
||||
|
||||
assertEquals("Queue size", 9, proxy.getQueueSize());
|
||||
|
||||
echo("Worked!");
|
||||
}
|
||||
|
||||
protected void assertCreateAndDestroyDurableSubscriptions() throws Exception {
|
||||
// lets create a new topic
|
||||
ObjectName brokerName = assertRegisteredObjectName(domain + ":Type=Broker,BrokerName=localhost");
|
||||
echo("Create QueueView MBean...");
|
||||
BrokerViewMBean broker = (BrokerViewMBean) MBeanServerInvocationHandler.newProxyInstance(mbeanServer, brokerName, BrokerViewMBean.class, true);
|
||||
|
||||
broker.addTopic(getDestinationString());
|
||||
|
||||
assertEquals("Durable subscriber count", 0, broker.getDurableTopicSubscribers().length);
|
||||
|
||||
ObjectName newTopicName = assertRegisteredObjectName(domain + ":Type=Topic,Destination=" + getDestinationString() + ",BrokerName=localhost");
|
||||
TopicViewMBean topic = (TopicViewMBean) MBeanServerInvocationHandler.newProxyInstance(mbeanServer, newTopicName, TopicViewMBean.class, true);
|
||||
topic.createDurableSubscriber(clientID, "subscriber1");
|
||||
topic.createDurableSubscriber(clientID, "subscriber2");
|
||||
assertEquals("Durable subscriber count", 2, broker.getDurableTopicSubscribers().length);
|
||||
|
||||
// now lets try destroy it
|
||||
topic.destroyDurableSubscriber(clientID, "subscriber1");
|
||||
assertEquals("Durable subscriber count", 1, broker.getDurableTopicSubscribers().length);
|
||||
}
|
||||
|
||||
protected ObjectName assertRegisteredObjectName(String name) throws MalformedObjectNameException, NullPointerException {
|
||||
ObjectName objectName = new ObjectName(name);
|
||||
if (mbeanServer.isRegistered(objectName)) {
|
||||
echo("Bean Registered: " + objectName);
|
||||
}
|
||||
else {
|
||||
fail("Could not find MBean!: " + objectName);
|
||||
}
|
||||
return objectName;
|
||||
}
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
|
@ -117,6 +168,16 @@ public class MBeanTest extends EmbeddedBrokerTestSupport {
|
|||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
if (waitForKeyPress) {
|
||||
// We are running from the command line so let folks browse the
|
||||
// mbeans...
|
||||
System.out.println();
|
||||
System.out.println("Press enter to terminate the program.");
|
||||
System.out.println("In the meantime you can use your JMX console to view the current MBeans");
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
|
||||
reader.readLine();
|
||||
}
|
||||
|
||||
if (connection != null) {
|
||||
connection.close();
|
||||
connection = null;
|
||||
|
@ -133,7 +194,7 @@ public class MBeanTest extends EmbeddedBrokerTestSupport {
|
|||
}
|
||||
|
||||
protected void useConnection(Connection connection) throws Exception {
|
||||
connection.setClientID("foo");
|
||||
connection.setClientID(clientID);
|
||||
connection.start();
|
||||
Session session = connection.createSession(transacted, authMode);
|
||||
destination = createDestination();
|
||||
|
|
Loading…
Reference in New Issue