1 18 package org.apache.activemq.test; 19 20 import javax.jms.Connection ; 21 import javax.jms.ConnectionConsumer ; 22 import javax.jms.ConnectionFactory ; 23 import javax.jms.Destination ; 24 import javax.jms.JMSException ; 25 import javax.jms.MessageConsumer ; 26 import javax.jms.MessageProducer ; 27 import javax.jms.ServerSessionPool ; 28 import javax.jms.Session ; 29 import javax.jms.DeliveryMode ; 30 import javax.jms.Topic ; 31 32 import org.apache.activemq.ActiveMQConnectionFactory; 33 34 37 public class JmsResourceProvider { 38 39 private String serverUri = "vm://localhost?broker.persistent=false"; 40 private boolean transacted = false; 41 private int ackMode = Session.AUTO_ACKNOWLEDGE; 42 private boolean isTopic; 43 private int deliveryMode=DeliveryMode.PERSISTENT; 44 private String durableName = "DummyName"; 45 private String clientID = getClass().getName(); 46 47 52 public ConnectionFactory createConnectionFactory() throws Exception { 53 return new ActiveMQConnectionFactory(serverUri); 54 } 55 56 61 public Connection createConnection(ConnectionFactory cf) throws JMSException { 62 Connection connection = cf.createConnection(); 63 if (getClientID()!=null) { 64 connection.setClientID(getClientID()); 65 } 66 return connection; 67 } 68 69 72 public Session createSession(Connection conn) throws JMSException { 73 return conn.createSession(transacted, ackMode); 74 } 75 76 80 public MessageConsumer createConsumer(Session session, 81 Destination destination) throws JMSException { 82 if (isDurableSubscriber()) { 83 return session.createDurableSubscriber((Topic ) destination, durableName); 84 } 85 return session.createConsumer(destination); 86 } 87 88 94 public ConnectionConsumer createConnectionConsumer(Connection connection, Destination destination, ServerSessionPool ssp) throws JMSException { 95 return connection.createConnectionConsumer(destination,null,ssp,1); 96 } 97 98 104 public MessageProducer createProducer(Session session, 105 Destination destination) throws JMSException { 106 MessageProducer producer = session.createProducer(destination); 107 producer.setDeliveryMode(deliveryMode); 108 return producer; 109 } 110 111 117 public Destination createDestination(Session session, String name) 118 throws JMSException { 119 if( isTopic ) 120 return session.createTopic("TOPIC."+name); 121 else 122 return session.createQueue("QUEUE."+name); 123 } 124 125 130 public boolean isDurableSubscriber() { 131 return isTopic && durableName!=null; 132 } 133 134 139 public int getAckMode() { 140 return ackMode; 141 } 142 143 148 public void setAckMode(int ackMode) { 149 this.ackMode = ackMode; 150 } 151 152 157 public boolean isTopic() { 158 return isTopic; 159 } 160 161 164 public void setTopic(boolean isTopic) { 165 this.isTopic = isTopic; 166 } 167 168 173 public String getServerUri() { 174 return serverUri; 175 } 176 177 182 public void setServerUri(String serverUri) { 183 this.serverUri = serverUri; 184 } 185 186 191 public boolean isTransacted() { 192 return transacted; 193 } 194 195 200 public void setTransacted(boolean transacted) { 201 this.transacted = transacted; 202 } 203 204 209 public int getDeliveryMode() { 210 return deliveryMode; 211 } 212 213 218 public void setDeliveryMode(int deliveryMode) { 219 this.deliveryMode = deliveryMode; 220 } 221 222 227 public String getClientID() { 228 return clientID; 229 } 230 231 236 public void setClientID(String clientID) { 237 this.clientID = clientID; 238 } 239 240 245 public String getDurableName() { 246 return durableName; 247 } 248 249 254 public void setDurableName(String durableName) { 255 this.durableName = durableName; 256 } 257 } 258 | Popular Tags |