KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > test > JmsTopicSendReceiveWithTwoConnectionsTest


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.test;
19
20 import javax.jms.Connection JavaDoc;
21 import javax.jms.DeliveryMode JavaDoc;
22 import javax.jms.JMSException JavaDoc;
23 import javax.jms.MessageConsumer JavaDoc;
24 import javax.jms.Session JavaDoc;
25
26 import org.apache.activemq.ActiveMQConnectionFactory;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 /**
31  * @version $Revision: 1.3 $
32  */

33 public class JmsTopicSendReceiveWithTwoConnectionsTest extends JmsSendReceiveTestSupport {
34     
35     protected static final Log log = LogFactory.getLog(JmsTopicSendReceiveWithTwoConnectionsTest.class);
36
37     protected Connection JavaDoc sendConnection;
38     protected Connection JavaDoc receiveConnection;
39     protected Session JavaDoc receiveSession;
40
41     /**
42      * Sets up a test where the producer and consumer have their own connection.
43      *
44      * @see junit.framework.TestCase#setUp()
45      */

46     protected void setUp() throws Exception JavaDoc {
47         super.setUp();
48
49         connectionFactory = createConnectionFactory();
50
51         log.info("Creating send connection");
52         sendConnection = createSendConnection();
53         log.info("Starting send connection");
54         sendConnection.start();
55
56         log.info("Creating receive connection");
57         receiveConnection = createReceiveConnection();
58         log.info("Starting receive connection");
59         receiveConnection.start();
60
61         log.info("Created sendConnection: " + sendConnection);
62         log.info("Created receiveConnection: " + receiveConnection);
63
64         session = sendConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
65         receiveSession = receiveConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
66
67         log.info("Created sendSession: " + session);
68         log.info("Created receiveSession: " + receiveSession);
69
70         producer = session.createProducer(null);
71         producer.setDeliveryMode(deliveryMode);
72
73         log.info("Created producer: " + producer + " delivery mode = " +
74                 (deliveryMode == DeliveryMode.PERSISTENT ? "PERSISTENT" : "NON_PERSISTENT"));
75
76         if (topic) {
77             consumerDestination = session.createTopic(getConsumerSubject());
78             producerDestination = session.createTopic(getProducerSubject());
79         }
80         else {
81             consumerDestination = session.createQueue(getConsumerSubject());
82             producerDestination = session.createQueue(getProducerSubject());
83         }
84
85         log.info("Created consumer destination: " + consumerDestination + " of type: " + consumerDestination.getClass());
86         log.info("Created producer destination: " + producerDestination + " of type: " + producerDestination.getClass());
87
88         consumer = createConsumer();
89         consumer.setMessageListener(this);
90
91         log.info("Started connections");
92     }
93
94     protected MessageConsumer JavaDoc createConsumer() throws JMSException JavaDoc {
95         return receiveSession.createConsumer(consumerDestination);
96     }
97     
98     /*
99      * @see junit.framework.TestCase#tearDown()
100      */

101     protected void tearDown() throws Exception JavaDoc {
102         session.close();
103         receiveSession.close();
104         sendConnection.close();
105         receiveConnection.close();
106     }
107
108     /**
109      * Creates a connection.
110      *
111      * @return Connection
112      * @throws Exception
113      */

114     protected Connection JavaDoc createReceiveConnection() throws Exception JavaDoc {
115         return createConnection();
116     }
117
118     /**
119      * Creates a connection.
120      *
121      * @return Connection
122      * @throws Exception
123      */

124     protected Connection JavaDoc createSendConnection() throws Exception JavaDoc {
125         return createConnection();
126     }
127
128     /**
129      * Creates an ActiveMQConnectionFactory.
130      *
131      * @see org.apache.activemq.test.TestSupport#createConnectionFactory()
132      */

133     protected ActiveMQConnectionFactory createConnectionFactory() throws Exception JavaDoc {
134         return new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
135     }
136 }
137
Popular Tags