KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > pool > PooledConnection


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

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 JavaDoc;
25 import javax.jms.ConnectionConsumer JavaDoc;
26 import javax.jms.ConnectionMetaData JavaDoc;
27 import javax.jms.Destination JavaDoc;
28 import javax.jms.ExceptionListener JavaDoc;
29 import javax.jms.JMSException JavaDoc;
30 import javax.jms.Queue JavaDoc;
31 import javax.jms.QueueConnection JavaDoc;
32 import javax.jms.QueueSession JavaDoc;
33 import javax.jms.ServerSessionPool JavaDoc;
34 import javax.jms.Session JavaDoc;
35 import javax.jms.Topic JavaDoc;
36 import javax.jms.TopicConnection JavaDoc;
37 import javax.jms.TopicSession JavaDoc;
38
39 /**
40  * Represents a proxy {@link Connection} which is-a {@link TopicConnection} and
41  * {@link QueueConnection} which is pooled and on {@link #close()} will return
42  * itself to the sessionPool.
43  *
44  * <b>NOTE</b> this implementation is only intended for use when sending
45  * messages.
46  * It does not deal with pooling of consumers; for that look at a library like
47  * <a HREF="http://jencks.org/">Jencks</a> such as in
48  * <a HREF="http://jencks.org/Message+Driven+POJOs">this example</a>
49  *
50  * @version $Revision: 1.1.1.1 $
51  */

52 public class PooledConnection implements TopicConnection JavaDoc, QueueConnection JavaDoc {
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     /**
63      * Factory method to create a new instance.
64      */

65     public PooledConnection newInstance() {
66         return new PooledConnection(pool);
67     }
68
69     public void close() throws JMSException JavaDoc {
70         if( this.pool!=null ) {
71             this.pool.decrementReferenceCount();
72             this.pool = null;
73         }
74     }
75
76     public void start() throws JMSException JavaDoc {
77         assertNotClosed();
78         pool.start();
79     }
80
81     public void stop() throws JMSException JavaDoc {
82         stopped = true;
83     }
84
85     public ConnectionConsumer JavaDoc createConnectionConsumer(Destination JavaDoc destination, String JavaDoc selector, ServerSessionPool JavaDoc serverSessionPool, int maxMessages)
86             throws JMSException JavaDoc {
87         return getConnection().createConnectionConsumer(destination, selector, serverSessionPool, maxMessages);
88     }
89
90     public ConnectionConsumer JavaDoc createConnectionConsumer(Topic JavaDoc topic, String JavaDoc s, ServerSessionPool JavaDoc serverSessionPool, int maxMessages) throws JMSException JavaDoc {
91         return getConnection().createConnectionConsumer(topic, s, serverSessionPool, maxMessages);
92     }
93
94     public ConnectionConsumer JavaDoc createDurableConnectionConsumer(Topic JavaDoc topic, String JavaDoc selector, String JavaDoc s1, ServerSessionPool JavaDoc serverSessionPool, int i)
95             throws JMSException JavaDoc {
96         return getConnection().createDurableConnectionConsumer(topic, selector, s1, serverSessionPool, i);
97     }
98
99     public String JavaDoc getClientID() throws JMSException JavaDoc {
100         return getConnection().getClientID();
101     }
102
103     public ExceptionListener JavaDoc getExceptionListener() throws JMSException JavaDoc {
104         return getConnection().getExceptionListener();
105     }
106
107     public ConnectionMetaData JavaDoc getMetaData() throws JMSException JavaDoc {
108         return getConnection().getMetaData();
109     }
110
111     public void setExceptionListener(ExceptionListener JavaDoc exceptionListener) throws JMSException JavaDoc {
112         getConnection().setExceptionListener(exceptionListener);
113     }
114
115     public void setClientID(String JavaDoc clientID) throws JMSException JavaDoc {
116         getConnection().setClientID(clientID);
117     }
118
119     public ConnectionConsumer JavaDoc createConnectionConsumer(Queue JavaDoc queue, String JavaDoc selector, ServerSessionPool JavaDoc serverSessionPool, int maxMessages) throws JMSException JavaDoc {
120         return getConnection().createConnectionConsumer(queue, selector, serverSessionPool, maxMessages);
121     }
122
123     // Session factory methods
124
// -------------------------------------------------------------------------
125
public QueueSession JavaDoc createQueueSession(boolean transacted, int ackMode) throws JMSException JavaDoc {
126         return (QueueSession JavaDoc) createSession(transacted, ackMode);
127     }
128
129     public TopicSession JavaDoc createTopicSession(boolean transacted, int ackMode) throws JMSException JavaDoc {
130         return (TopicSession JavaDoc) createSession(transacted, ackMode);
131     }
132
133     public Session createSession(boolean transacted, int ackMode) throws JMSException JavaDoc {
134         return pool.createSession(transacted, ackMode);
135     }
136
137     // Implementation methods
138
// -------------------------------------------------------------------------
139

140     ActiveMQConnection getConnection() throws JMSException JavaDoc {
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 JavaDoc {
152         return (ActiveMQSession) getConnection().createSession(key.isTransacted(), key.getAckMode());
153     }
154     
155     public String JavaDoc toString() {
156         return "PooledConnection { "+pool+" }";
157     }
158
159 }
160
Popular Tags