View Javadoc

1   /*
2    * Copyright (C) The Apache Software Foundation. All rights reserved.
3    *
4    * This software is published under the terms of the Apache Software License
5    * version 1.1, a copy of which has been included with this distribution in
6    * the LICENSE file.
7    * 
8    * $Id: DefaultServerSessionPool.java 155459 2005-02-26 13:24:44Z dirkv $
9    */
10  package org.apache.commons.messenger;
11  
12  import java.util.LinkedList;
13  
14  import javax.jms.JMSException;
15  import javax.jms.Message;
16  import javax.jms.MessageListener;
17  import javax.jms.ServerSession;
18  import javax.jms.ServerSessionPool;
19  import javax.jms.Session;
20  
21  
22  /** <p><code>DefaultServerSessionPool</code> is a default implementation of
23    * the JMS ServerSessionPool interface.</p>
24    *
25    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
26    * @version $Revision: 155459 $
27    */
28  public class DefaultServerSessionPool implements ServerSessionPool {
29  
30      /** the list of ServerSession objects */
31      private LinkedList list = new LinkedList();
32      /** the factory used to create new Sessions */
33      private SessionFactory sessionFactory;
34      /** The MessageListener which are fired by new JMS Sessions */
35      private MessageListener listener;    
36      /** maximum blocking time, just in case a notify goes walkies */
37      private long timeout = 5000;
38      
39      public DefaultServerSessionPool() {
40      }
41      
42      public DefaultServerSessionPool(SessionFactory sessionFactory, MessageListener listener, int numberOfServerSessions) throws JMSException {
43          this.sessionFactory = sessionFactory;
44          this.listener = listener;
45          
46          for ( int i = 0; i < numberOfServerSessions; i++ ) {
47              list.add( createServerSession() );
48          }
49      }
50      
51  
52      /** Return a server session from the pool.
53        *
54        * @return a server session from the pool.
55        *
56        * @exception JMSException if a JMS error occurs.
57        */
58      public synchronized ServerSession getServerSession() throws JMSException {
59          // we may want to grow if there are no sessions available
60          while (list.isEmpty() ) {
61              try {
62                  wait( timeout );
63              }
64              catch(InterruptedException ex) {
65                  // ignore
66              }
67          }
68          return (ServerSession) list.removeFirst();
69      }
70  
71      public synchronized void putServerSession(ServerSession serverSession) {
72          list.addLast(serverSession);
73          notify();
74      }
75  
76      // Implementation methods
77      //-------------------------------------------------------------------------        
78      protected ServerSession createServerSession() throws JMSException {
79          Session session = sessionFactory.createSession();
80          final DefaultServerSession serverSession = new DefaultServerSession(session);
81          session.setMessageListener( 
82              new MessageListener() {
83                  public void onMessage(Message message) {
84                      try {
85                          listener.onMessage(message);
86                      }
87                      finally {
88                          // now give this session back to the pool
89                          putServerSession( serverSession );
90                      }
91                  }
92              }
93          );        
94          return serverSession;
95      }
96  }
97