KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > usecases > ConsumeTopicPrefetchTest


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.usecases;
19
20 import javax.jms.Connection JavaDoc;
21 import javax.jms.JMSException JavaDoc;
22 import javax.jms.Message JavaDoc;
23 import javax.jms.TextMessage JavaDoc;
24
25 import org.apache.activemq.ActiveMQConnection;
26
27 /**
28  * @version $Revision: 1.1.1.1 $
29  */

30 public class ConsumeTopicPrefetchTest extends ProducerConsumerTestSupport {
31
32     protected int prefetchSize = 100;
33     protected String JavaDoc[] messageTexts;
34     protected long consumerTimeout = 10000L;
35
36     public void testSendPrefetchSize() throws JMSException JavaDoc {
37         testWithMessageCount(prefetchSize);
38     }
39
40     public void testSendDoublePrefetchSize() throws JMSException JavaDoc {
41         testWithMessageCount(prefetchSize * 2);
42     }
43
44     public void testSendPrefetchSizePlusOne() throws JMSException JavaDoc {
45         testWithMessageCount(prefetchSize + 1);
46     }
47
48     protected void testWithMessageCount(int messageCount) throws JMSException JavaDoc {
49         makeMessages(messageCount);
50
51         log.info("About to send and receive: " + messageCount + " on destination: " + destination
52                 + " of type: " + destination.getClass().getName());
53
54         for (int i = 0; i < messageCount; i++) {
55             Message JavaDoc message = session.createTextMessage(messageTexts[i]);
56             producer.send(message);
57         }
58
59         // lets consume them in two fetch batches
60
for (int i = 0; i < messageCount; i++) {
61             consumeMessge(i);
62         }
63     }
64
65     protected Connection JavaDoc createConnection() throws Exception JavaDoc {
66         ActiveMQConnection connection = (ActiveMQConnection) super.createConnection();
67         connection.getPrefetchPolicy().setQueuePrefetch(prefetchSize);
68         connection.getPrefetchPolicy().setTopicPrefetch(prefetchSize);
69         return connection;
70     }
71
72     protected void consumeMessge(int i) throws JMSException JavaDoc {
73         Message JavaDoc message = consumer.receive(consumerTimeout);
74         assertTrue("Should have received a message by now for message: " + i, message != null);
75         assertTrue("Should be a TextMessage: " + message, message instanceof TextMessage JavaDoc);
76         TextMessage JavaDoc textMessage = (TextMessage JavaDoc) message;
77         assertEquals("Message content", messageTexts[i], textMessage.getText());
78     }
79
80
81     protected void makeMessages(int messageCount) {
82         messageTexts = new String JavaDoc[messageCount];
83         for (int i = 0; i < messageCount; i++) {
84             messageTexts[i] = "Message for test: + " + getName() + " = " + i;
85         }
86     }
87
88 }
89
Popular Tags