1 18 package org.apache.activemq.broker.jmx; 19 20 import org.apache.activemq.EmbeddedBrokerTestSupport; 21 import org.apache.activemq.broker.BrokerService; 22 23 import javax.jms.Connection ; 24 import javax.jms.Message ; 25 import javax.jms.MessageProducer ; 26 import javax.jms.Session ; 27 import javax.management.MBeanServer ; 28 import javax.management.MBeanServerInvocationHandler ; 29 import javax.management.MalformedObjectNameException ; 30 import javax.management.ObjectName ; 31 32 import junit.textui.TestRunner; 33 34 39 public class PurgeTest extends EmbeddedBrokerTestSupport { 40 41 protected MBeanServer mbeanServer; 42 protected String domain = "org.apache.activemq"; 43 protected String clientID = "foo"; 44 45 protected Connection connection; 46 protected boolean transacted; 47 protected int authMode = Session.AUTO_ACKNOWLEDGE; 48 protected int messageCount = 10; 49 50 public static void main(String [] args) { 51 TestRunner.run(PurgeTest.class); 52 } 53 54 public void testPurge() throws Exception { 55 connection = connectionFactory.createConnection(); 57 connection.setClientID(clientID); 58 connection.start(); 59 Session session = connection.createSession(transacted, authMode); 60 destination = createDestination(); 61 MessageProducer producer = session.createProducer(destination); 62 for (int i = 0; i < messageCount; i++) { 63 Message message = session.createTextMessage("Message: " + i); 64 producer.send(message); 65 } 66 67 ObjectName queueViewMBeanName = assertRegisteredObjectName(domain + ":Type=Queue,Destination=" + getDestinationString() + ",BrokerName=localhost"); 69 QueueViewMBean proxy = (QueueViewMBean) MBeanServerInvocationHandler.newProxyInstance(mbeanServer, queueViewMBeanName, QueueViewMBean.class, true); 70 71 long count = proxy.getQueueSize(); 72 assertEquals("Queue size", count, messageCount); 73 74 proxy.purge(); 75 count = proxy.getQueueSize(); 76 assertEquals("Queue size", count, 0); 77 78 messageCount += 1000; 81 for (int i = 0; i < messageCount; i++) { 82 Message message = session.createTextMessage("Message: " + i); 83 producer.send(message); 84 } 85 86 count = proxy.getQueueSize(); 87 assertEquals("Queue size", count, messageCount); 88 89 proxy.purge(); 90 count = proxy.getQueueSize(); 91 assertEquals("Queue size", count, 0); 92 } 93 94 protected ObjectName assertRegisteredObjectName(String name) throws MalformedObjectNameException , NullPointerException { 95 ObjectName objectName = new ObjectName (name); 96 if (mbeanServer.isRegistered(objectName)) { 97 echo("Bean Registered: " + objectName); 98 } 99 else { 100 fail("Could not find MBean!: " + objectName); 101 } 102 return objectName; 103 } 104 105 protected void setUp() throws Exception { 106 bindAddress = "tcp://localhost:61616"; 107 useTopic = false; 108 super.setUp(); 109 mbeanServer = broker.getManagementContext().getMBeanServer(); 110 } 111 112 protected void tearDown() throws Exception { 113 if (connection != null) { 114 connection.close(); 115 connection = null; 116 } 117 super.tearDown(); 118 } 119 120 protected BrokerService createBroker() throws Exception { 121 BrokerService answer = new BrokerService(); 122 answer.setUseJmx(true); 123 answer.setEnableStatistics(true); 124 answer.setPersistent(false); 125 answer.addConnector(bindAddress); 126 return answer; 127 } 128 129 protected void echo(String text) { 130 log.info(text); 131 } 132 } 133 | Popular Tags |