1 17 package org.apache.activemq.web; 18 19 import org.apache.activemq.ActiveMQConnectionFactory; 20 21 import javax.jms.Connection ; 22 import javax.jms.ConnectionFactory ; 23 import javax.jms.JMSException ; 24 import javax.jms.Session ; 25 import java.util.LinkedList ; 26 27 32 public class SessionPool { 33 34 private ConnectionFactory connectionFactory; 35 private Connection connection; 36 private LinkedList sessions = new LinkedList (); 37 38 public Connection getConnection() throws JMSException { 39 if (connection == null) { 40 connection = getConnectionFactory().createConnection(); 41 connection.start(); 42 } 43 return connection; 44 } 45 46 public void setConnection(Connection connection) { 47 this.connection = connection; 48 } 49 50 public ConnectionFactory getConnectionFactory() { 51 if (connectionFactory == null) { 52 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 borrowSession() throws JMSException { 64 Session answer = null; 65 synchronized (sessions) { 66 if (sessions.isEmpty()) { 67 answer = createSession(); 68 } 69 else { 70 answer = (Session ) sessions.removeLast(); 71 } 72 } 73 return answer; 74 } 75 76 protected void returnSession(Session session) { 77 if (session != null) { 78 synchronized (sessions) { 79 sessions.add(session); 80 } 81 } 82 } 83 84 protected Session createSession() throws JMSException { 85 return getConnection().createSession(false, Session.AUTO_ACKNOWLEDGE); 86 } 87 88 } 89 | Popular Tags |