1 18 package org.apache.activemq.usecases; 19 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 23 import javax.jms.Connection ; 24 import javax.jms.JMSException ; 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 36 public class TwoBrokerTopicSendReceiveTest extends JmsTopicSendReceiveWithTwoConnectionsTest { 37 38 protected ActiveMQConnectionFactory sendFactory; 39 protected ActiveMQConnectionFactory receiveFactory; 40 protected HashMap brokers = new HashMap (); 41 42 protected void setUp() throws Exception { 43 sendFactory = createSenderConnectionFactory(); 44 receiveFactory = createReceiverConnectionFactory(); 45 46 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 { 56 return createConnectionFactory("org/apache/activemq/usecases/receiver.xml", "receiver", "vm://receiver"); 57 } 58 59 protected ActiveMQConnectionFactory createSenderConnectionFactory() throws JMSException { 60 return createConnectionFactory("org/apache/activemq/usecases/sender.xml", "sender", "vm://sender"); 61 } 62 63 protected void tearDown() throws Exception { 64 super.tearDown(); 65 for (Iterator 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 createReceiveConnection() throws JMSException { 73 return receiveFactory.createConnection(); 74 } 75 76 protected Connection createSendConnection() throws JMSException { 77 return sendFactory.createConnection(); 78 } 79 80 protected ActiveMQConnectionFactory createConnectionFactory(String config, String brokerName, String connectUrl) throws JMSException { 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 e) { 90 e.printStackTrace(); 91 } 92 return null; 93 } 94 } 95 | Popular Tags |