1 18 package org.apache.activemq; 19 20 import javax.jms.Connection ; 21 import javax.jms.ConnectionFactory ; 22 import javax.jms.Destination ; 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 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 = "vm://localhost"; 46 protected ConnectionFactory connectionFactory; 47 protected boolean useTopic = false; 48 protected Destination destination; 49 protected JmsTemplate template; 50 private boolean usePooledConnectionWithTemplate = true; 51 52 protected void setUp() throws Exception { 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 { 69 if (broker != null) { 70 broker.stop(); 71 } 72 } 73 74 79 protected JmsTemplate createJmsTemplate() { 80 if (usePooledConnectionWithTemplate) { 81 return new JmsTemplate(new PooledConnectionFactory(bindAddress)); 83 } 84 else { 85 return new JmsTemplate(connectionFactory); 86 } 87 } 88 89 94 protected Destination createDestination() { 95 return createDestination(getDestinationString()); 96 } 97 98 102 protected Destination createDestination(String subject) { 103 if (useTopic) { 104 return new ActiveMQTopic(subject); 105 } 106 else { 107 return new ActiveMQQueue(subject); 108 } 109 } 110 111 114 protected String getDestinationString() { 115 return getClass().getName() + "." + getName(); 116 } 117 118 123 protected ConnectionFactory createConnectionFactory() throws Exception { 124 return new ActiveMQConnectionFactory(bindAddress); 125 } 126 127 132 protected BrokerService createBroker() throws Exception { 133 BrokerService answer = new BrokerService(); 134 answer.setPersistent(isPersistent()); 135 answer.addConnector(bindAddress); 136 return answer; 137 } 138 139 protected void startBroker() throws Exception { 140 broker.start(); 141 } 142 143 146 protected boolean isPersistent() { 147 return false; 148 } 149 150 153 protected Connection createConnection() throws Exception { 154 return connectionFactory.createConnection(); 155 } 156 } 157 | Popular Tags |