1 18 package org.apache.activemq.pool; 19 20 import org.apache.activemq.ActiveMQConnection; 21 import org.apache.activemq.ActiveMQSession; 22 import org.apache.activemq.AlreadyClosedException; 23 24 import javax.jms.Connection ; 25 import javax.jms.ConnectionConsumer ; 26 import javax.jms.ConnectionMetaData ; 27 import javax.jms.Destination ; 28 import javax.jms.ExceptionListener ; 29 import javax.jms.JMSException ; 30 import javax.jms.Queue ; 31 import javax.jms.QueueConnection ; 32 import javax.jms.QueueSession ; 33 import javax.jms.ServerSessionPool ; 34 import javax.jms.Session ; 35 import javax.jms.Topic ; 36 import javax.jms.TopicConnection ; 37 import javax.jms.TopicSession ; 38 39 52 public class PooledConnection implements TopicConnection , QueueConnection { 53 54 private ConnectionPool pool; 55 private boolean stopped; 56 57 public PooledConnection(ConnectionPool pool) { 58 this.pool = pool; 59 this.pool.incrementReferenceCount(); 60 } 61 62 65 public PooledConnection newInstance() { 66 return new PooledConnection(pool); 67 } 68 69 public void close() throws JMSException { 70 if( this.pool!=null ) { 71 this.pool.decrementReferenceCount(); 72 this.pool = null; 73 } 74 } 75 76 public void start() throws JMSException { 77 assertNotClosed(); 78 pool.start(); 79 } 80 81 public void stop() throws JMSException { 82 stopped = true; 83 } 84 85 public ConnectionConsumer createConnectionConsumer(Destination destination, String selector, ServerSessionPool serverSessionPool, int maxMessages) 86 throws JMSException { 87 return getConnection().createConnectionConsumer(destination, selector, serverSessionPool, maxMessages); 88 } 89 90 public ConnectionConsumer createConnectionConsumer(Topic topic, String s, ServerSessionPool serverSessionPool, int maxMessages) throws JMSException { 91 return getConnection().createConnectionConsumer(topic, s, serverSessionPool, maxMessages); 92 } 93 94 public ConnectionConsumer createDurableConnectionConsumer(Topic topic, String selector, String s1, ServerSessionPool serverSessionPool, int i) 95 throws JMSException { 96 return getConnection().createDurableConnectionConsumer(topic, selector, s1, serverSessionPool, i); 97 } 98 99 public String getClientID() throws JMSException { 100 return getConnection().getClientID(); 101 } 102 103 public ExceptionListener getExceptionListener() throws JMSException { 104 return getConnection().getExceptionListener(); 105 } 106 107 public ConnectionMetaData getMetaData() throws JMSException { 108 return getConnection().getMetaData(); 109 } 110 111 public void setExceptionListener(ExceptionListener exceptionListener) throws JMSException { 112 getConnection().setExceptionListener(exceptionListener); 113 } 114 115 public void setClientID(String clientID) throws JMSException { 116 getConnection().setClientID(clientID); 117 } 118 119 public ConnectionConsumer createConnectionConsumer(Queue queue, String selector, ServerSessionPool serverSessionPool, int maxMessages) throws JMSException { 120 return getConnection().createConnectionConsumer(queue, selector, serverSessionPool, maxMessages); 121 } 122 123 public QueueSession createQueueSession(boolean transacted, int ackMode) throws JMSException { 126 return (QueueSession ) createSession(transacted, ackMode); 127 } 128 129 public TopicSession createTopicSession(boolean transacted, int ackMode) throws JMSException { 130 return (TopicSession ) createSession(transacted, ackMode); 131 } 132 133 public Session createSession(boolean transacted, int ackMode) throws JMSException { 134 return pool.createSession(transacted, ackMode); 135 } 136 137 140 ActiveMQConnection getConnection() throws JMSException { 141 assertNotClosed(); 142 return pool.getConnection(); 143 } 144 145 protected void assertNotClosed() throws AlreadyClosedException { 146 if (stopped || pool == null) { 147 throw new AlreadyClosedException(); 148 } 149 } 150 151 protected ActiveMQSession createSession(SessionKey key) throws JMSException { 152 return (ActiveMQSession) getConnection().createSession(key.isTransacted(), key.getAckMode()); 153 } 154 155 public String toString() { 156 return "PooledConnection { "+pool+" }"; 157 } 158 159 } 160 | Popular Tags |