KickJava   Java API By Example, From Geeks To Geeks.

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


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.ConnectionFactory JavaDoc;
22 import javax.jms.Destination JavaDoc;
23
24 import junit.framework.TestCase;
25
26 import org.apache.activemq.broker.BrokerService;
27 import org.apache.activemq.command.ActiveMQQueue;
28 import org.apache.activemq.command.ActiveMQTopic;
29 import org.apache.activemq.pool.PooledConnectionFactory;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.springframework.jms.core.JmsTemplate;
33
34 /**
35  * A useful base class which creates and closes an embedded broker
36  *
37  * @version $Revision: 1.1 $
38  */

39 public abstract class EmbeddedBrokerTestSupport extends TestCase {
40
41     protected static final Log log = LogFactory.getLog(EmbeddedBrokerTestSupport.class);
42     
43     protected BrokerService broker;
44     //protected String bindAddress = "tcp://localhost:61616";
45
protected String JavaDoc bindAddress = "vm://localhost";
46     protected ConnectionFactory JavaDoc connectionFactory;
47     protected boolean useTopic = false;
48     protected Destination JavaDoc destination;
49     protected JmsTemplate template;
50     private boolean usePooledConnectionWithTemplate = true;
51
52     protected void setUp() throws Exception JavaDoc {
53         if (broker == null) {
54             broker = createBroker();
55         }
56         startBroker();
57
58         connectionFactory = createConnectionFactory();
59
60         destination = createDestination();
61
62         template = createJmsTemplate();
63         template.setDefaultDestination(destination);
64         template.setPubSubDomain(useTopic);
65         template.afterPropertiesSet();
66     }
67
68     protected void tearDown() throws Exception JavaDoc {
69         if (broker != null) {
70             broker.stop();
71         }
72     }
73
74     /**
75      * Factory method to create a new {@link JmsTemplate}
76      *
77      * @return a newly created JmsTemplate
78      */

79     protected JmsTemplate createJmsTemplate() {
80         if (usePooledConnectionWithTemplate) {
81             // lets use a pool to avoid creating and closing producers
82
return new JmsTemplate(new PooledConnectionFactory(bindAddress));
83         }
84         else {
85             return new JmsTemplate(connectionFactory);
86         }
87     }
88
89     /**
90      * Factory method to create a new {@link Destination}
91      *
92      * @return newly created Destinaiton
93      */

94     protected Destination JavaDoc createDestination() {
95         return createDestination(getDestinationString());
96     }
97
98     /**
99      * Factory method to create the destination in either the queue or topic
100      * space based on the value of the {@link #useTopic} field
101      */

102     protected Destination JavaDoc createDestination(String JavaDoc subject) {
103         if (useTopic) {
104             return new ActiveMQTopic(subject);
105         }
106         else {
107             return new ActiveMQQueue(subject);
108         }
109     }
110
111     /**
112      * Returns the name of the destination used in this test case
113      */

114     protected String JavaDoc getDestinationString() {
115         return getClass().getName() + "." + getName();
116     }
117
118     /**
119      * Factory method to create a new {@link ConnectionFactory} instance
120      *
121      * @return a newly created connection factory
122      */

123     protected ConnectionFactory JavaDoc createConnectionFactory() throws Exception JavaDoc {
124         return new ActiveMQConnectionFactory(bindAddress);
125     }
126
127     /**
128      * Factory method to create a new broker
129      *
130      * @throws Exception
131      */

132     protected BrokerService createBroker() throws Exception JavaDoc {
133         BrokerService answer = new BrokerService();
134         answer.setPersistent(isPersistent());
135         answer.addConnector(bindAddress);
136         return answer;
137     }
138
139     protected void startBroker() throws Exception JavaDoc {
140         broker.start();
141     }
142
143     /**
144      * @return whether or not persistence should be used
145      */

146     protected boolean isPersistent() {
147         return false;
148     }
149
150     /**
151      * Factory method to create a new connection
152      */

153     protected Connection JavaDoc createConnection() throws Exception JavaDoc {
154         return connectionFactory.createConnection();
155     }
156 }
157
Popular Tags