1 18 package org.apache.activemq.systest.impl; 19 20 import org.apache.activemq.systest.AgentStopper; 21 import org.apache.activemq.systest.AgentSupport; 22 import org.apache.activemq.systest.BrokerAgent; 23 import org.apache.activemq.systest.ClientAgent; 24 25 import javax.jms.Connection ; 26 import javax.jms.ConnectionFactory ; 27 import javax.jms.Destination ; 28 import javax.jms.JMSException ; 29 import javax.jms.Session ; 30 31 36 public class JmsClientSupport extends AgentSupport implements ClientAgent { 37 38 private ConnectionFactory connectionFactory; 39 private Connection connection; 40 private Session session; 41 private boolean transacted; 42 private int acknowledgementMode = Session.CLIENT_ACKNOWLEDGE; 43 private Destination destination; 44 45 public void start() throws Exception { 46 getConnection().start(); 47 } 48 49 public void connectTo(BrokerAgent broker) throws Exception { 50 stop(); 52 setConnectionFactory(broker.getConnectionFactory()); 53 start(); 54 } 55 56 public Destination getDestination() { 59 if (destination == null) { 60 throw new IllegalArgumentException ("You must set the 'destination' property"); 61 } 62 return destination; 63 } 64 65 public void setDestination(Destination destination) { 66 this.destination = destination; 67 } 68 69 public int getAcknowledgementMode() { 70 return acknowledgementMode; 71 } 72 73 public void setAcknowledgementMode(int acknowledgementMode) { 74 this.acknowledgementMode = acknowledgementMode; 75 } 76 77 public ConnectionFactory getConnectionFactory() { 78 if (connectionFactory == null) { 79 throw new IllegalArgumentException ("You must set the 'connectionFactory' property"); 80 } 81 return connectionFactory; 82 } 83 84 public void setConnectionFactory(ConnectionFactory connectionFactory) { 85 this.connectionFactory = connectionFactory; 86 } 87 88 public boolean isTransacted() { 89 return transacted; 90 } 91 92 public void setTransacted(boolean transacted) { 93 this.transacted = transacted; 94 } 95 96 public Connection getConnection() throws JMSException { 97 if (connection == null) { 98 connection = createConnection(); 99 } 100 return connection; 101 } 102 103 public Session getSession() throws JMSException { 104 if (session == null) { 105 session = createSession(); 106 } 107 return session; 108 } 109 110 public void stop(AgentStopper stopper) { 111 if (session != null) { 112 try { 113 session.close(); 114 } 115 catch (JMSException e) { 116 stopper.onException(this, e); 117 } 118 finally { 119 session = null; 120 } 121 } 122 if (connection != null) { 123 try { 124 connection.close(); 125 } 126 catch (JMSException e) { 127 stopper.onException(this, e); 128 } 129 finally { 130 connection = null; 131 } 132 } 133 } 134 135 protected Connection createConnection() throws JMSException { 138 return getConnectionFactory().createConnection(); 139 } 140 141 protected Session createSession() throws JMSException { 142 return getConnection().createSession(transacted, acknowledgementMode); 143 } 144 145 } 146 | Popular Tags |