KickJava   Java API By Example, From Geeks To Geeks.

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


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

17 package org.apache.activemq;
18
19 import org.apache.activemq.command.ActiveMQQueue;
20 import org.apache.activemq.spring.SpringConsumer;
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import javax.jms.Connection JavaDoc;
25 import javax.jms.JMSException JavaDoc;
26 import javax.jms.Message JavaDoc;
27 import javax.jms.MessageConsumer JavaDoc;
28 import javax.jms.MessageListener JavaDoc;
29 import javax.jms.MessageProducer JavaDoc;
30 import javax.jms.Queue JavaDoc;
31 import javax.jms.Session JavaDoc;
32 import javax.jms.TextMessage JavaDoc;
33
34 /**
35  *
36  * @version $Revision: 511598 $
37  */

38 public class ZeroPrefetchConsumerTest extends EmbeddedBrokerTestSupport {
39
40     private static final Log log = LogFactory.getLog(ZeroPrefetchConsumerTest.class);
41
42     protected Connection JavaDoc connection;
43     protected Queue queue;
44
45     public void testCannotUseMessageListener() throws Exception JavaDoc {
46         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
47         MessageConsumer JavaDoc consumer = session.createConsumer(queue);
48
49         MessageListener JavaDoc listener = new SpringConsumer();
50         try {
51             consumer.setMessageListener(listener);
52             fail("Should have thrown JMSException as we cannot use MessageListener with zero prefetch");
53         }
54         catch (JMSException JavaDoc e) {
55             log.info("Received expected exception : " + e);
56         }
57     }
58
59     public void testPullConsumerWorks() throws Exception JavaDoc {
60         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
61
62         MessageProducer JavaDoc producer = session.createProducer(queue);
63         producer.send(session.createTextMessage("Hello World!"));
64
65         // now lets receive it
66
MessageConsumer JavaDoc consumer = session.createConsumer(queue);
67         Message answer = consumer.receive(5000);
68         assertNotNull("Should have received a message!", answer);
69         // check if method will return at all and will return a null
70
answer = consumer.receive(1);
71         assertNull("Should have not received a message!", answer);
72         answer = consumer.receiveNoWait();
73         assertNull("Should have not received a message!", answer);
74     }
75
76     public void testIdleConsumer() throws Exception JavaDoc {
77         doTestIdleConsumer(false);
78     }
79     
80     public void testIdleConsumerTranscated() throws Exception JavaDoc {
81         doTestIdleConsumer(true);
82     }
83     
84     private void doTestIdleConsumer(boolean transacted) throws Exception JavaDoc {
85         Session JavaDoc session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
86
87         MessageProducer JavaDoc producer = session.createProducer(queue);
88         producer.send(session.createTextMessage("Msg1"));
89         producer.send(session.createTextMessage("Msg2"));
90         if(transacted) {
91             session.commit();
92         }
93         // now lets receive it
94
MessageConsumer JavaDoc consumer = session.createConsumer(queue);
95         //noinspection UNUSED_SYMBOL
96
MessageConsumer JavaDoc idleConsumer = session.createConsumer(queue);
97         TextMessage JavaDoc answer = (TextMessage JavaDoc) consumer.receive(5000);
98         assertEquals("Should have received a message!", answer.getText(), "Msg1");
99         if(transacted) {
100             session.commit();
101         }
102         // this call would return null if prefetchSize > 0
103
answer = (TextMessage JavaDoc) consumer.receive(5000);
104         assertEquals("Should have received a message!", answer.getText(), "Msg2");
105         if(transacted) {
106             session.commit();
107         }
108         answer = (TextMessage JavaDoc) consumer.receiveNoWait();
109         assertNull("Should have not received a message!", answer);
110     }
111
112     public void testRecvRecvCommit() throws Exception JavaDoc {
113         doTestRecvRecvCommit(false);
114     }
115     
116     public void testRecvRecvCommitTranscated() throws Exception JavaDoc {
117         doTestRecvRecvCommit(true);
118     }
119     
120     private void doTestRecvRecvCommit(boolean transacted) throws Exception JavaDoc {
121         Session JavaDoc session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
122
123         MessageProducer JavaDoc producer = session.createProducer(queue);
124         producer.send(session.createTextMessage("Msg1"));
125         producer.send(session.createTextMessage("Msg2"));
126         if(transacted) {
127             session.commit();
128         }
129         // now lets receive it
130
MessageConsumer JavaDoc consumer = session.createConsumer(queue);
131         TextMessage JavaDoc answer = (TextMessage JavaDoc) consumer.receiveNoWait();
132         assertEquals("Should have received a message!", answer.getText(), "Msg1");
133         answer = (TextMessage JavaDoc) consumer.receiveNoWait();
134         assertEquals("Should have received a message!", answer.getText(), "Msg2");
135         if(transacted) {
136             session.commit();
137         }
138         answer = (TextMessage JavaDoc) consumer.receiveNoWait();
139         assertNull("Should have not received a message!", answer);
140     }
141     
142     protected void setUp() throws Exception JavaDoc {
143         bindAddress = "tcp://localhost:61616";
144         super.setUp();
145
146         connection = createConnection();
147         connection.start();
148         queue = createQueue();
149     }
150
151     protected void tearDown() throws Exception JavaDoc {
152         connection.close();
153         super.tearDown();
154     }
155
156     protected Queue createQueue() {
157         return new ActiveMQQueue(getDestinationString() + "?consumer.prefetchSize=0");
158     }
159
160 }
161
Popular Tags