Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DefaultServerSessionPool |
|
| 1.5;1.5 | ||||
DefaultServerSessionPool$1 |
|
| 1.5;1.5 |
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 | 0 | public class DefaultServerSessionPool implements ServerSessionPool { |
29 | ||
30 | /** the list of ServerSession objects */ | |
31 | 0 | 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 | 0 | private long timeout = 5000; |
38 | ||
39 | 0 | public DefaultServerSessionPool() { |
40 | 0 | } |
41 | ||
42 | 0 | public DefaultServerSessionPool(SessionFactory sessionFactory, MessageListener listener, int numberOfServerSessions) throws JMSException { |
43 | 0 | this.sessionFactory = sessionFactory; |
44 | 0 | this.listener = listener; |
45 | ||
46 | 0 | for ( int i = 0; i < numberOfServerSessions; i++ ) { |
47 | 0 | list.add( createServerSession() ); |
48 | } | |
49 | 0 | } |
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 | 0 | while (list.isEmpty() ) { |
61 | try { | |
62 | 0 | wait( timeout ); |
63 | } | |
64 | 0 | catch(InterruptedException ex) { |
65 | // ignore | |
66 | 0 | } |
67 | } | |
68 | 0 | return (ServerSession) list.removeFirst(); |
69 | } | |
70 | ||
71 | public synchronized void putServerSession(ServerSession serverSession) { | |
72 | 0 | list.addLast(serverSession); |
73 | 0 | notify(); |
74 | 0 | } |
75 | ||
76 | // Implementation methods | |
77 | //------------------------------------------------------------------------- | |
78 | protected ServerSession createServerSession() throws JMSException { | |
79 | 0 | Session session = sessionFactory.createSession(); |
80 | 0 | final DefaultServerSession serverSession = new DefaultServerSession(session); |
81 | 0 | session.setMessageListener( |
82 | 0 | new MessageListener() { |
83 | public void onMessage(Message message) { | |
84 | try { | |
85 | 0 | listener.onMessage(message); |
86 | } | |
87 | finally { | |
88 | // now give this session back to the pool | |
89 | 0 | putServerSession( serverSession ); |
90 | 0 | } |
91 | 0 | } |
92 | } | |
93 | ); | |
94 | 0 | return serverSession; |
95 | } | |
96 | } | |
97 |