001    /*
002     * Copyright (C) The Apache Software Foundation. All rights reserved.
003     *
004     * This software is published under the terms of the Apache Software License
005     * version 1.1, a copy of which has been included with this distribution in
006     * the LICENSE file.
007     * 
008     * $Id: DefaultServerSessionPool.java 155459 2005-02-26 13:24:44Z dirkv $
009     */
010    package org.apache.commons.messenger;
011    
012    import java.util.LinkedList;
013    
014    import javax.jms.JMSException;
015    import javax.jms.Message;
016    import javax.jms.MessageListener;
017    import javax.jms.ServerSession;
018    import javax.jms.ServerSessionPool;
019    import javax.jms.Session;
020    
021    
022    /** <p><code>DefaultServerSessionPool</code> is a default implementation of
023      * the JMS ServerSessionPool interface.</p>
024      *
025      * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
026      * @version $Revision: 155459 $
027      */
028    public class DefaultServerSessionPool implements ServerSessionPool {
029    
030        /** the list of ServerSession objects */
031        private LinkedList list = new LinkedList();
032        /** the factory used to create new Sessions */
033        private SessionFactory sessionFactory;
034        /** The MessageListener which are fired by new JMS Sessions */
035        private MessageListener listener;    
036        /** maximum blocking time, just in case a notify goes walkies */
037        private long timeout = 5000;
038        
039        public DefaultServerSessionPool() {
040        }
041        
042        public DefaultServerSessionPool(SessionFactory sessionFactory, MessageListener listener, int numberOfServerSessions) throws JMSException {
043            this.sessionFactory = sessionFactory;
044            this.listener = listener;
045            
046            for ( int i = 0; i < numberOfServerSessions; i++ ) {
047                list.add( createServerSession() );
048            }
049        }
050        
051    
052        /** Return a server session from the pool.
053          *
054          * @return a server session from the pool.
055          *
056          * @exception JMSException if a JMS error occurs.
057          */
058        public synchronized ServerSession getServerSession() throws JMSException {
059            // we may want to grow if there are no sessions available
060            while (list.isEmpty() ) {
061                try {
062                    wait( timeout );
063                }
064                catch(InterruptedException ex) {
065                    // ignore
066                }
067            }
068            return (ServerSession) list.removeFirst();
069        }
070    
071        public synchronized void putServerSession(ServerSession serverSession) {
072            list.addLast(serverSession);
073            notify();
074        }
075    
076        // Implementation methods
077        //-------------------------------------------------------------------------        
078        protected ServerSession createServerSession() throws JMSException {
079            Session session = sessionFactory.createSession();
080            final DefaultServerSession serverSession = new DefaultServerSession(session);
081            session.setMessageListener( 
082                new MessageListener() {
083                    public void onMessage(Message message) {
084                        try {
085                            listener.onMessage(message);
086                        }
087                        finally {
088                            // now give this session back to the pool
089                            putServerSession( serverSession );
090                        }
091                    }
092                }
093            );        
094            return serverSession;
095        }
096    }
097