KickJava   Java API By Example, From Geeks To Geeks.

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


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 import javax.jms.Topic JavaDoc;
26
27 /**
28  * @version $Revision: 1.2 $
29  */

30 public class JmsTopicSendReceiveTest extends JmsSendReceiveTestSupport {
31     private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
32             .getLog(JmsTopicSendReceiveTest.class);
33     
34     protected Connection JavaDoc connection;
35
36     protected void setUp() throws Exception JavaDoc {
37         super.setUp();
38
39         connectionFactory = createConnectionFactory();
40         connection = createConnection();
41         if (durable) {
42             connection.setClientID(getClass().getName());
43         }
44
45         log.info("Created connection: " + connection);
46
47         session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
48         consumeSession = createConsumerSession();
49
50         log.info("Created session: " + session);
51         log.info("Created consumeSession: " + consumeSession);
52         producer = session.createProducer(null);
53         producer.setDeliveryMode(deliveryMode);
54
55         log.info("Created producer: " + producer + " delivery mode = " +
56                 (deliveryMode == DeliveryMode.PERSISTENT ? "PERSISTENT" : "NON_PERSISTENT"));
57
58         if (topic) {
59             consumerDestination = session.createTopic(getConsumerSubject());
60             producerDestination = session.createTopic(getProducerSubject());
61         }
62         else {
63             consumerDestination = session.createQueue(getConsumerSubject());
64             producerDestination = session.createQueue(getProducerSubject());
65         }
66
67         log.info("Created consumer destination: " + consumerDestination + " of type: " + consumerDestination.getClass());
68         log.info("Created producer destination: " + producerDestination + " of type: " + producerDestination.getClass());
69         consumer = createConsumer();
70         consumer.setMessageListener(this);
71         connection.start();
72
73         log.info("Created connection: " + connection);
74     }
75
76     protected void tearDown() throws Exception JavaDoc {
77         log.info("Dumping stats...");
78         //TODO
79
//connectionFactory.getFactoryStats().dump(new IndentPrinter());
80

81         log.info("Closing down connection");
82
83         /** TODO we should be able to shut down properly */
84         session.close();
85         connection.close();
86     }
87     
88     /**
89      * Creates a session.
90      *
91      * @return session
92      * @throws JMSException
93      */

94     protected Session JavaDoc createConsumerSession() throws JMSException JavaDoc {
95         if (useSeparateSession) {
96             return connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
97         }
98         else {
99             return session;
100         }
101     }
102
103     /**
104      * Creates a durable suscriber or a consumer.
105      *
106      * @return MessageConsumer - durable suscriber or consumer.
107      * @throws JMSException
108      */

109     protected MessageConsumer JavaDoc createConsumer() throws JMSException JavaDoc {
110         if (durable) {
111             log.info("Creating durable consumer");
112             return consumeSession.createDurableSubscriber((Topic JavaDoc) consumerDestination, getName());
113         }
114         return consumeSession.createConsumer(consumerDestination);
115     }
116 }
117
Popular Tags