KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > web > SessionPool


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

17 package org.apache.activemq.web;
18
19 import org.apache.activemq.ActiveMQConnectionFactory;
20
21 import javax.jms.Connection JavaDoc;
22 import javax.jms.ConnectionFactory JavaDoc;
23 import javax.jms.JMSException JavaDoc;
24 import javax.jms.Session JavaDoc;
25 import java.util.LinkedList JavaDoc;
26
27 /**
28  * A simple pool of JMS Session objects intended for use by Queue browsers.
29  *
30  * @version $Revision: 504235 $
31  */

32 public class SessionPool {
33
34     private ConnectionFactory connectionFactory;
35     private Connection JavaDoc connection;
36     private LinkedList JavaDoc sessions = new LinkedList JavaDoc();
37
38     public Connection JavaDoc getConnection() throws JMSException JavaDoc {
39         if (connection == null) {
40             connection = getConnectionFactory().createConnection();
41             connection.start();
42         }
43         return connection;
44     }
45
46     public void setConnection(Connection JavaDoc connection) {
47         this.connection = connection;
48     }
49
50     public ConnectionFactory getConnectionFactory() {
51         if (connectionFactory == null) {
52             // TODO support remote brokers too
53
connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
54         }
55         return connectionFactory;
56     }
57
58     public void setConnectionFactory(ConnectionFactory connectionFactory) {
59         this.connectionFactory = connectionFactory;
60     }
61
62
63     public Session JavaDoc borrowSession() throws JMSException JavaDoc {
64         Session JavaDoc answer = null;
65         synchronized (sessions) {
66             if (sessions.isEmpty()) {
67                 answer = createSession();
68             }
69             else {
70                 answer = (Session JavaDoc) sessions.removeLast();
71             }
72         }
73         return answer;
74     }
75
76     protected void returnSession(Session JavaDoc session) {
77         if (session != null) {
78             synchronized (sessions) {
79                 sessions.add(session);
80             }
81         }
82     }
83
84     protected Session JavaDoc createSession() throws JMSException JavaDoc {
85         return getConnection().createSession(false, Session.AUTO_ACKNOWLEDGE);
86     }
87
88 }
89
Popular Tags