KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > LoadTestBurnIn


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;
19
20 import java.io.IOException JavaDoc;
21 import java.net.URI JavaDoc;
22 import java.net.URISyntaxException JavaDoc;
23
24 import javax.jms.BytesMessage JavaDoc;
25 import javax.jms.Connection JavaDoc;
26 import javax.jms.ConnectionFactory JavaDoc;
27 import javax.jms.DeliveryMode JavaDoc;
28 import javax.jms.JMSException JavaDoc;
29 import javax.jms.Message JavaDoc;
30 import javax.jms.MessageConsumer JavaDoc;
31 import javax.jms.MessageProducer JavaDoc;
32 import javax.jms.Session JavaDoc;
33 import javax.jms.Topic JavaDoc;
34
35 import junit.framework.Test;
36
37 import org.apache.activemq.ActiveMQConnectionFactory;
38 import org.apache.activemq.broker.BrokerFactory;
39 import org.apache.activemq.broker.BrokerService;
40 import org.apache.activemq.broker.TransportConnector;
41 import org.apache.activemq.command.ActiveMQDestination;
42
43 import java.util.concurrent.CountDownLatch JavaDoc;
44 import java.util.concurrent.TimeUnit JavaDoc;
45
46 /**
47  * Small burn test moves sends a moderate amount of messages through the broker, to
48  * checking to make sure that the broker does not lock up after a while of sustained
49  * messaging.
50  *
51  * @version $Revision$
52  */

53 public class LoadTestBurnIn extends JmsTestSupport {
54     
55     public static Test suite() {
56         return suite(LoadTestBurnIn.class);
57     }
58
59     protected void setUp() throws Exception JavaDoc {
60         System.out.println("Start: "+getName());
61         super.setUp();
62     }
63
64     protected void tearDown() throws Exception JavaDoc {
65         try {
66             super.tearDown();
67         } catch (Throwable JavaDoc e) {
68             e.printStackTrace(System.out);
69         } finally {
70             System.out.println("End: "+getName());
71         }
72     }
73     
74     public static void main(String JavaDoc[] args) {
75         junit.textui.TestRunner.run(suite());
76     }
77     
78     protected BrokerService createBroker() throws Exception JavaDoc {
79         return BrokerFactory.createBroker(new URI JavaDoc("broker://(tcp://localhost:0)?useJmx=true"));
80 // return BrokerFactory.createBroker(new URI("xbean:org/apache/activemq/broker/store/loadtester.xml"));
81
}
82
83     protected ConnectionFactory JavaDoc createConnectionFactory() throws URISyntaxException JavaDoc, IOException JavaDoc {
84         return new ActiveMQConnectionFactory(((TransportConnector) broker.getTransportConnectors().get(0)).getServer().getConnectURI());
85     }
86
87     public ActiveMQDestination destination;
88     public int deliveryMode;
89     public byte destinationType;
90     public boolean durableConsumer;
91     
92     public int messageCount = 50000;
93     public int messageSize = 1024;
94     
95     public void initCombosForTestSendReceive() {
96         addCombinationValues("deliveryMode", new Object JavaDoc[] {
97                 new Integer JavaDoc(DeliveryMode.NON_PERSISTENT),
98                 new Integer JavaDoc(DeliveryMode.PERSISTENT) });
99         addCombinationValues("destinationType", new Object JavaDoc[] {
100                 new Byte JavaDoc(ActiveMQDestination.TOPIC_TYPE),
101 // new Byte(ActiveMQDestination.QUEUE_TYPE),
102
});
103         addCombinationValues("durableConsumer", new Object JavaDoc[] {
104                 Boolean.TRUE,
105 // Boolean.FALSE,
106
});
107         addCombinationValues("messageSize", new Object JavaDoc[] {
108                 new Integer JavaDoc(101),
109                 new Integer JavaDoc(102),
110                 new Integer JavaDoc(103),
111                 new Integer JavaDoc(104),
112                 new Integer JavaDoc(105),
113                 new Integer JavaDoc(106),
114                 new Integer JavaDoc(107),
115                 new Integer JavaDoc(108),
116         });
117     }
118
119     public void testSendReceive() throws Exception JavaDoc {
120
121         // Durable consumer combination is only valid with topics
122
if( durableConsumer && destinationType!=ActiveMQDestination.TOPIC_TYPE)
123             return;
124         
125         connection.setClientID(getName());
126         connection.getPrefetchPolicy().setAll(1000);
127         connection.start();
128
129         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
130         destination = createDestination(session, destinationType);
131         MessageConsumer JavaDoc consumer;
132         if( durableConsumer ) {
133             consumer = session.createDurableSubscriber((Topic JavaDoc) destination, "sub1:"+System.currentTimeMillis());
134         } else {
135             consumer = session.createConsumer(destination);
136         }
137         profilerPause("Ready: ");
138         
139         final CountDownLatch JavaDoc producerDoneLatch = new CountDownLatch JavaDoc(1);
140         
141         // Send the messages, async
142
new Thread JavaDoc() {
143             public void run() {
144                 Connection JavaDoc connection2=null;
145                 try {
146                     connection2 = factory.createConnection();
147                     Session JavaDoc session = connection2.createSession(false, Session.AUTO_ACKNOWLEDGE);
148                     MessageProducer JavaDoc producer = session.createProducer(destination);
149                     producer.setDeliveryMode(deliveryMode);
150                     for (int i = 0; i < messageCount; i++) {
151                          BytesMessage JavaDoc m = session.createBytesMessage();
152                          m.writeBytes(new byte[messageSize]);
153                          producer.send(m);
154                     }
155                     producer.close();
156                 } catch (JMSException JavaDoc e) {
157                     e.printStackTrace();
158                 } finally {
159                     safeClose(connection2);
160                     producerDoneLatch.countDown();
161                 }
162                 
163             }
164         }.start();
165
166         // Make sure all the messages were delivered.
167
Message message = null;
168         for (int i = 0; i < messageCount; i++) {
169             message = consumer.receive(5000);
170             assertNotNull("Did not get message: "+i, message);
171         }
172
173         profilerPause("Done: ");
174         
175         assertNull(consumer.receiveNoWait());
176         message.acknowledge();
177
178         // Make sure the producer thread finishes.
179
assertTrue(producerDoneLatch.await(5, TimeUnit.SECONDS));
180     }
181
182 }
183
Popular Tags