KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > broker > jmx > PurgeTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

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 JavaDoc;
24 import javax.jms.Message JavaDoc;
25 import javax.jms.MessageProducer JavaDoc;
26 import javax.jms.Session JavaDoc;
27 import javax.management.MBeanServer JavaDoc;
28 import javax.management.MBeanServerInvocationHandler JavaDoc;
29 import javax.management.MalformedObjectNameException JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31
32 import junit.textui.TestRunner;
33
34 /**
35  * A specific test of Queue.purge() functionality
36  *
37  * @version $Revision: 512156 $
38  */

39 public class PurgeTest extends EmbeddedBrokerTestSupport {
40
41     protected MBeanServer JavaDoc mbeanServer;
42     protected String JavaDoc domain = "org.apache.activemq";
43     protected String JavaDoc clientID = "foo";
44
45     protected Connection JavaDoc connection;
46     protected boolean transacted;
47     protected int authMode = Session.AUTO_ACKNOWLEDGE;
48     protected int messageCount = 10;
49
50     public static void main(String JavaDoc[] args) {
51         TestRunner.run(PurgeTest.class);
52     }
53
54     public void testPurge() throws Exception JavaDoc {
55         // Send some messages
56
connection = connectionFactory.createConnection();
57         connection.setClientID(clientID);
58         connection.start();
59         Session JavaDoc session = connection.createSession(transacted, authMode);
60         destination = createDestination();
61         MessageProducer JavaDoc producer = session.createProducer(destination);
62         for (int i = 0; i < messageCount; i++) {
63             Message JavaDoc message = session.createTextMessage("Message: " + i);
64             producer.send(message);
65         }
66
67         // Now get the QueueViewMBean and purge
68
ObjectName JavaDoc 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         // Queues have a special case once there are more than a thousand
79
// dead messages, make sure we hit that.
80
messageCount += 1000;
81         for (int i = 0; i < messageCount; i++) {
82             Message JavaDoc 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 JavaDoc assertRegisteredObjectName(String JavaDoc name) throws MalformedObjectNameException JavaDoc, NullPointerException JavaDoc {
95         ObjectName JavaDoc objectName = new ObjectName JavaDoc(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 JavaDoc {
106         bindAddress = "tcp://localhost:61616";
107         useTopic = false;
108         super.setUp();
109         mbeanServer = broker.getManagementContext().getMBeanServer();
110     }
111
112     protected void tearDown() throws Exception JavaDoc {
113         if (connection != null) {
114             connection.close();
115             connection = null;
116         }
117         super.tearDown();
118     }
119
120     protected BrokerService createBroker() throws Exception JavaDoc {
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 JavaDoc text) {
130         log.info(text);
131     }
132 }
133
Popular Tags