KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.jms.DeliveryMode JavaDoc;
21 import javax.jms.Message JavaDoc;
22 import javax.jms.MessageConsumer JavaDoc;
23 import javax.jms.MessageProducer JavaDoc;
24 import javax.jms.Session JavaDoc;
25
26 import junit.framework.Test;
27
28 import org.apache.activemq.command.ActiveMQQueue;
29
30 /**
31  * Test cases used to test the JMS message exclusive consumers.
32  *
33  * @version $Revision$
34  */

35 public class JMSExclusiveConsumerTest extends JmsTestSupport {
36
37     public static Test suite() {
38         return suite(JMSExclusiveConsumerTest.class);
39     }
40
41     public static void main(String JavaDoc[] args) {
42         junit.textui.TestRunner.run(suite());
43     }
44
45     public int deliveryMode;
46     
47     public void initCombosForTestRoundRobinDispatchOnNonExclusive() {
48         addCombinationValues("deliveryMode", new Object JavaDoc[] { new Integer JavaDoc(DeliveryMode.NON_PERSISTENT),
49                 new Integer JavaDoc(DeliveryMode.PERSISTENT) });
50     }
51
52     /**
53      * Shows that by default messages are round robined across a set of consumers.
54      *
55      * @throws Exception
56      */

57     public void testRoundRobinDispatchOnNonExclusive() throws Exception JavaDoc {
58
59         // Receive a message with the JMS API
60
connection.start();
61         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
62         ActiveMQQueue destination = new ActiveMQQueue("TEST");
63         MessageProducer JavaDoc producer = session.createProducer(destination);
64         producer.setDeliveryMode(deliveryMode);
65         
66         MessageConsumer JavaDoc consumer1 = session.createConsumer(destination);
67         MessageConsumer JavaDoc consumer2 = session.createConsumer(destination);
68
69         // Send the messages
70
producer.send(session.createTextMessage("1st"));
71         producer.send(session.createTextMessage("2nd"));
72         
73         Message m;
74         m = consumer2.receive(1000);
75         assertNotNull(m);
76         
77         m = consumer1.receive(1000);
78         assertNotNull(m);
79
80         assertNull(consumer1.receiveNoWait());
81         assertNull(consumer2.receiveNoWait());
82     }
83
84     public void initCombosForTestDispatchExclusive() {
85         addCombinationValues("deliveryMode", new Object JavaDoc[] { new Integer JavaDoc(DeliveryMode.NON_PERSISTENT),
86                 new Integer JavaDoc(DeliveryMode.PERSISTENT) });
87     }
88
89     /**
90      * Shows that if the "?consumer.exclusive=true" option is added to destination,
91      * then all messages are routed to 1 consumer.
92      *
93      * @throws Exception
94      */

95     public void testDispatchExclusive() throws Exception JavaDoc {
96
97         // Receive a message with the JMS API
98
connection.start();
99         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
100         ActiveMQQueue destination = new ActiveMQQueue("TEST?consumer.exclusive=true");
101         MessageProducer JavaDoc producer = session.createProducer(destination);
102         producer.setDeliveryMode(deliveryMode);
103         
104         MessageConsumer JavaDoc consumer1 = session.createConsumer(destination);
105         MessageConsumer JavaDoc consumer2 = session.createConsumer(destination);
106
107         // Send the messages
108
producer.send(session.createTextMessage("1st"));
109         producer.send(session.createTextMessage("2nd"));
110         producer.send(session.createTextMessage("3nd"));
111         
112         Message m;
113         m = consumer2.receive(1000);
114         if( m!=null ) {
115             // Consumer 2 should get all the messages.
116
for (int i = 0; i < 2; i++) {
117                 m = consumer2.receive(1000);
118                 assertNotNull(m);
119             }
120         } else {
121             // Consumer 1 should get all the messages.
122
for (int i = 0; i < 3; i++) {
123                 m = consumer1.receive(1000);
124                 assertNotNull(m);
125             }
126         }
127
128         assertNull(consumer1.receiveNoWait());
129         assertNull(consumer2.receiveNoWait());
130     }
131
132     public void testMixExclusiveWithNonExclusive() throws Exception JavaDoc {
133         ActiveMQQueue exclusiveQueue = new ActiveMQQueue("TEST.FOO?consumer.exclusive=true");
134         ActiveMQQueue nonExclusiveQueue = new ActiveMQQueue("TEST.FOO?consumer.exclusive=false");
135
136         connection.start();
137         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
138
139         MessageConsumer JavaDoc nonExCon = session.createConsumer(nonExclusiveQueue);
140         MessageConsumer JavaDoc exCon = session.createConsumer(exclusiveQueue);
141
142
143         MessageProducer JavaDoc prod = session.createProducer(exclusiveQueue);
144         prod.send(session.createMessage());
145         prod.send(session.createMessage());
146         prod.send(session.createMessage());
147
148         Message m;
149         for (int i=0; i<3; i++) {
150             m = exCon.receive(1000);
151             assertNotNull(m);
152             m = nonExCon.receive(1000);
153             assertNull(m);
154         }
155     }
156 }
157
Popular Tags