KickJava   Java API By Example, From Geeks To Geeks.

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


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.Session JavaDoc;
26 import javax.jms.TextMessage JavaDoc;
27 import javax.jms.Topic JavaDoc;
28
29 import org.apache.activemq.ActiveMQConnectionFactory;
30
31 /**
32  * @version $Revision: 1.3 $
33  */

34 public class JmsConnectionStartStopTest extends TestSupport {
35
36     private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
37             .getLog(JmsConnectionStartStopTest.class);
38     
39     private Connection JavaDoc startedConnection;
40     private Connection JavaDoc stoppedConnection;
41
42     /**
43      * @see junit.framework.TestCase#setUp()
44      */

45     protected void setUp() throws Exception JavaDoc {
46             
47         log.info(getClass().getClassLoader().getResource("log4j.properties"));
48         
49          ActiveMQConnectionFactory factory = createConnectionFactory();
50          startedConnection = factory.createConnection();
51          startedConnection.start();
52          stoppedConnection = factory.createConnection();
53     }
54
55     /**
56      * @see junit.framework.TestCase#tearDown()
57      */

58     protected void tearDown() throws Exception JavaDoc {
59          stoppedConnection.close();
60          startedConnection.close();
61     }
62
63     /**
64      * Tests if the consumer receives the messages that were sent before the connection was started.
65      *
66      * @throws JMSException
67      */

68     public void testStoppedConsumerHoldsMessagesTillStarted() throws JMSException JavaDoc {
69          Session JavaDoc startedSession = startedConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
70          Session JavaDoc stoppedSession = stoppedConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
71
72          // Setup the consumers.
73
Topic JavaDoc topic = startedSession.createTopic("test");
74          MessageConsumer JavaDoc startedConsumer = startedSession.createConsumer(topic);
75          MessageConsumer JavaDoc stoppedConsumer = stoppedSession.createConsumer(topic);
76
77          // Send the message.
78
MessageProducer JavaDoc producer = startedSession.createProducer(topic);
79          TextMessage JavaDoc message = startedSession.createTextMessage("Hello");
80          producer.send(message);
81          
82          // Test the assertions.
83
Message m = startedConsumer.receive(1000);
84          assertNotNull(m);
85          
86          m = stoppedConsumer.receive(1000);
87          assertNull(m);
88          
89          stoppedConnection.start();
90          m = stoppedConsumer.receive(5000);
91          assertNotNull(m);
92          
93          startedSession.close();
94          stoppedSession.close();
95     }
96
97     /**
98      * Tests if the consumer is able to receive messages eveb when the connecction restarts multiple times.
99      *
100      * @throws Exception
101      */

102     public void testMultipleConnectionStops() throws Exception JavaDoc {
103          testStoppedConsumerHoldsMessagesTillStarted();
104          stoppedConnection.stop();
105          testStoppedConsumerHoldsMessagesTillStarted();
106          stoppedConnection.stop();
107          testStoppedConsumerHoldsMessagesTillStarted();
108     }
109 }
110
Popular Tags