KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > usecases > TwoBrokerTopicSendReceiveTest


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.usecases;
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import javax.jms.Connection JavaDoc;
24 import javax.jms.JMSException JavaDoc;
25
26 import org.apache.activemq.ActiveMQConnectionFactory;
27 import org.apache.activemq.broker.BrokerService;
28 import org.apache.activemq.test.JmsTopicSendReceiveWithTwoConnectionsTest;
29 import org.apache.activemq.util.ServiceSupport;
30 import org.apache.activemq.xbean.BrokerFactoryBean;
31 import org.springframework.core.io.ClassPathResource;
32
33 /**
34  * @version $Revision: 1.1.1.1 $
35  */

36 public class TwoBrokerTopicSendReceiveTest extends JmsTopicSendReceiveWithTwoConnectionsTest {
37
38     protected ActiveMQConnectionFactory sendFactory;
39     protected ActiveMQConnectionFactory receiveFactory;
40     protected HashMap JavaDoc brokers = new HashMap JavaDoc();
41
42     protected void setUp() throws Exception JavaDoc {
43         sendFactory = createSenderConnectionFactory();
44         receiveFactory = createReceiverConnectionFactory();
45
46         // Give server enough time to setup,
47
// so we don't lose messages when connection fails
48
log.info("Waiting for brokers Initialize.");
49         Thread.sleep(5000);
50         log.info("Brokers should be initialized by now.. starting test.");
51         
52         super.setUp();
53     }
54
55     protected ActiveMQConnectionFactory createReceiverConnectionFactory() throws JMSException JavaDoc {
56         return createConnectionFactory("org/apache/activemq/usecases/receiver.xml", "receiver", "vm://receiver");
57     }
58
59     protected ActiveMQConnectionFactory createSenderConnectionFactory() throws JMSException JavaDoc {
60         return createConnectionFactory("org/apache/activemq/usecases/sender.xml", "sender", "vm://sender");
61     }
62
63     protected void tearDown() throws Exception JavaDoc {
64         super.tearDown();
65         for (Iterator JavaDoc iter = brokers.values().iterator(); iter.hasNext();) {
66             BrokerService broker = (BrokerService) iter.next();
67             ServiceSupport.dispose(broker);
68             iter.remove();
69         }
70     }
71
72     protected Connection JavaDoc createReceiveConnection() throws JMSException JavaDoc {
73         return receiveFactory.createConnection();
74     }
75
76     protected Connection JavaDoc createSendConnection() throws JMSException JavaDoc {
77         return sendFactory.createConnection();
78     }
79
80     protected ActiveMQConnectionFactory createConnectionFactory(String JavaDoc config, String JavaDoc brokerName, String JavaDoc connectUrl) throws JMSException JavaDoc {
81         try {
82             BrokerFactoryBean brokerFactory = new BrokerFactoryBean(new ClassPathResource(config));
83             brokerFactory.afterPropertiesSet();
84             BrokerService broker = brokerFactory.getBroker();
85             brokers.put(brokerName, broker);
86             
87             return new ActiveMQConnectionFactory(connectUrl);
88
89         } catch (Exception JavaDoc e) {
90             e.printStackTrace();
91         }
92         return null;
93     }
94 }
95
Popular Tags