KickJava   Java API By Example, From Geeks To Geeks.

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


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.Connection JavaDoc;
21 import javax.jms.JMSException JavaDoc;
22 import javax.jms.Message JavaDoc;
23 import javax.jms.MessageConsumer JavaDoc;
24 import javax.jms.MessageProducer JavaDoc;
25 import javax.jms.Queue JavaDoc;
26 import javax.jms.Session JavaDoc;
27
28
29 /**
30  * @version $Revision: 1.1.1.1 $
31  */

32 public class ConsumerReceiveWithTimeoutTest extends TestSupport {
33
34     private Connection JavaDoc connection;
35
36     protected void setUp() throws Exception JavaDoc {
37         super.setUp();
38         connection = createConnection();
39     }
40
41     /**
42      * @see junit.framework.TestCase#tearDown()
43      */

44     protected void tearDown() throws Exception JavaDoc {
45         if (connection != null) {
46             connection.close();
47             connection = null;
48         }
49         super.tearDown();
50     }
51
52     /**
53      * Test to check if consumer thread wakes up inside a receive(timeout) after a message is dispatched to the consumer
54      *
55      * @throws javax.jms.JMSException
56      */

57     public void testConsumerReceiveBeforeMessageDispatched() throws JMSException JavaDoc {
58
59         connection.start();
60
61         final Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
62         final Queue JavaDoc queue = session.createQueue("test");
63
64         Thread JavaDoc t = new Thread JavaDoc() {
65                public void run(){
66                   try {
67                      //wait for 10 seconds to allow consumer.receive to be run first
68
Thread.sleep(10000);
69                      MessageProducer JavaDoc producer = session.createProducer(queue);
70                      producer.send(session.createTextMessage("Hello"));
71                   }catch(Exception JavaDoc e){e.printStackTrace();}
72                }
73            };
74
75         t.start();
76
77         // Consume the message...
78
MessageConsumer JavaDoc consumer = session.createConsumer(queue);
79         Message msg = consumer.receive(60000);
80         assertNotNull(msg);
81         session.close();
82
83     }
84
85
86
87
88
89 }
90
Popular Tags