KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jms > listener > serversession > CommonsPoolServerSessionFactory


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jms.listener.serversession;
18
19 import java.util.Collections JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.jms.JMSException JavaDoc;
25 import javax.jms.ServerSession JavaDoc;
26
27 import org.apache.commons.pool.ObjectPool;
28 import org.apache.commons.pool.PoolableObjectFactory;
29 import org.apache.commons.pool.impl.GenericObjectPool;
30
31 import org.springframework.jms.listener.serversession.ListenerSessionManager;
32
33 /**
34  * ServerSessionFactory implementation that holds ServerSessions
35  * in a configurable Jakarta Commons Pool.
36  *
37  * <p>By default, an instance of <code>GenericObjectPool</code> is created.
38  * Subclasses may change the type of <code>ObjectPool</code> used by
39  * overriding the <code>createObjectPool</code> method.
40  *
41  * <p>Provides many configuration properties mirroring those of the Commons Pool
42  * <code>GenericObjectPool</code> class; these properties are passed to the
43  * <code>GenericObjectPool</code> during construction. If creating a subclass of this
44  * class to change the <code>ObjectPool</code> implementation type, pass in the values
45  * of configuration properties that are relevant to your chosen implementation.
46  *
47  * @author Juergen Hoeller
48  * @since 2.0
49  * @see GenericObjectPool
50  * @see #createObjectPool
51  * @see #setMaxSize
52  * @see #setMaxIdle
53  * @see #setMinIdle
54  * @see #setMaxWait
55  */

56 public class CommonsPoolServerSessionFactory extends AbstractPoolingServerSessionFactory {
57
58     private int maxIdle = GenericObjectPool.DEFAULT_MAX_IDLE;
59
60     private int minIdle = GenericObjectPool.DEFAULT_MIN_IDLE;
61
62     private long maxWait = GenericObjectPool.DEFAULT_MAX_WAIT;
63
64     private long timeBetweenEvictionRunsMillis = GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS;
65
66     private long minEvictableIdleTimeMillis = GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS;
67
68     private final Map JavaDoc serverSessionPools = Collections.synchronizedMap(new HashMap JavaDoc(1));
69
70
71     /**
72      * Create a CommonsPoolServerSessionFactory with default settings.
73      * Default maximum size of the pool is 8.
74      * @see #setMaxSize
75      * @see GenericObjectPool#setMaxActive
76      */

77     public CommonsPoolServerSessionFactory() {
78         setMaxSize(GenericObjectPool.DEFAULT_MAX_ACTIVE);
79     }
80
81     /**
82      * Set the maximum number of idle ServerSessions in the pool.
83      * Default is 8.
84      * @see GenericObjectPool#setMaxIdle
85      */

86     public void setMaxIdle(int maxIdle) {
87         this.maxIdle = maxIdle;
88     }
89
90     /**
91      * Return the maximum number of idle ServerSessions in the pool.
92      */

93     public int getMaxIdle() {
94         return maxIdle;
95     }
96
97     /**
98      * Set the minimum number of idle ServerSessions in the pool.
99      * Default is 0.
100      * @see GenericObjectPool#setMinIdle
101      */

102     public void setMinIdle(int minIdle) {
103         this.minIdle = minIdle;
104     }
105
106     /**
107      * Return the minimum number of idle ServerSessions in the pool.
108      */

109     public int getMinIdle() {
110         return minIdle;
111     }
112
113     /**
114      * Set the maximum waiting time for fetching an ServerSession from the pool.
115      * Default is -1, waiting forever.
116      * @see GenericObjectPool#setMaxWait
117      */

118     public void setMaxWait(long maxWait) {
119         this.maxWait = maxWait;
120     }
121
122     /**
123      * Return the maximum waiting time for fetching a ServerSession from the pool.
124      */

125     public long getMaxWait() {
126         return maxWait;
127     }
128
129     /**
130      * Set the time between eviction runs that check idle ServerSessions
131      * whether they have been idle for too long or have become invalid.
132      * Default is -1, not performing any eviction.
133      * @see GenericObjectPool#setTimeBetweenEvictionRunsMillis
134      */

135     public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) {
136         this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
137     }
138
139     /**
140      * Return the time between eviction runs that check idle ServerSessions.
141      */

142     public long getTimeBetweenEvictionRunsMillis() {
143         return timeBetweenEvictionRunsMillis;
144     }
145
146     /**
147      * Set the minimum time that an idle ServerSession can sit in the pool
148      * before it becomes subject to eviction. Default is 1800000 (30 minutes).
149      * <p>Note that eviction runs need to be performed to take this
150      * setting into effect.
151      * @see #setTimeBetweenEvictionRunsMillis
152      * @see GenericObjectPool#setMinEvictableIdleTimeMillis
153      */

154     public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {
155         this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
156     }
157
158     /**
159      * Return the minimum time that an idle ServerSession can sit in the pool.
160      */

161     public long getMinEvictableIdleTimeMillis() {
162         return minEvictableIdleTimeMillis;
163     }
164
165
166     /**
167      * Returns a ServerSession from the pool, creating a new pool for the given
168      * session manager if necessary.
169      * @see #createObjectPool
170      */

171     public ServerSession JavaDoc getServerSession(ListenerSessionManager sessionManager) throws JMSException JavaDoc {
172         ObjectPool pool = null;
173         synchronized (this.serverSessionPools) {
174             pool = (ObjectPool) this.serverSessionPools.get(sessionManager);
175             if (pool == null) {
176                 if (logger.isInfoEnabled()) {
177                     logger.info("Creating Commons ServerSession pool for: " + sessionManager);
178                 }
179                 pool = createObjectPool(sessionManager);
180                 this.serverSessionPools.put(sessionManager, pool);
181             }
182         }
183         try {
184             return (ServerSession JavaDoc) pool.borrowObject();
185         }
186         catch (Exception JavaDoc ex) {
187             JMSException JavaDoc jmsEx = new JMSException JavaDoc("Failed to borrow ServerSession from pool");
188             jmsEx.setLinkedException(ex);
189             throw jmsEx;
190         }
191     }
192
193     /**
194      * Subclasses can override this if they want to return a specific Commons pool.
195      * They should apply any configuration properties to the pool here.
196      * <p>Default is a GenericObjectPool instance with the given pool size.
197      * @param sessionManager the session manager to use for
198      * creating and executing new listener sessions
199      * @return an empty Commons <code>ObjectPool</code>.
200      * @see org.apache.commons.pool.impl.GenericObjectPool
201      * @see #setMaxSize
202      */

203     protected ObjectPool createObjectPool(ListenerSessionManager sessionManager) {
204         GenericObjectPool pool = new GenericObjectPool(createPoolableObjectFactory(sessionManager));
205         pool.setMaxActive(getMaxSize());
206         pool.setMaxIdle(getMaxIdle());
207         pool.setMinIdle(getMinIdle());
208         pool.setMaxWait(getMaxWait());
209         pool.setTimeBetweenEvictionRunsMillis(getTimeBetweenEvictionRunsMillis());
210         pool.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis());
211         return pool;
212     }
213
214     /**
215      * Create a Commons PoolableObjectFactory adapter for the given session manager.
216      * Calls <code>createServerSession</code> and <code>destroyServerSession</code>
217      * as defined by the AbstractPoolingServerSessionFactory class.
218      * @param sessionManager the session manager to use for
219      * creating and executing new listener sessions
220      * @return the Commons PoolableObjectFactory
221      * @see #createServerSession
222      * @see #destroyServerSession
223      */

224     protected PoolableObjectFactory createPoolableObjectFactory(final ListenerSessionManager sessionManager) {
225         return new PoolableObjectFactory() {
226             public Object JavaDoc makeObject() throws JMSException JavaDoc {
227                 return createServerSession(sessionManager);
228             }
229             public void destroyObject(Object JavaDoc obj) {
230                 destroyServerSession((ServerSession JavaDoc) obj);
231             }
232             public boolean validateObject(Object JavaDoc obj) {
233                 return true;
234             }
235             public void activateObject(Object JavaDoc obj) {
236             }
237             public void passivateObject(Object JavaDoc obj) {
238             }
239         };
240     }
241
242     /**
243      * Returns the given ServerSession, which just finished an execution
244      * of its listener, back to the pool.
245      */

246     protected void serverSessionFinished(ServerSession JavaDoc serverSession, ListenerSessionManager sessionManager) {
247         ObjectPool pool = (ObjectPool) this.serverSessionPools.get(sessionManager);
248         try {
249             pool.returnObject(serverSession);
250         }
251         catch (Exception JavaDoc ex) {
252             logger.error("Failed to return ServerSession to pool", ex);
253         }
254     }
255
256     /**
257      * Closes all pools held by this ServerSessionFactory.
258      */

259     public void close(ListenerSessionManager sessionManager) {
260         synchronized (this.serverSessionPools) {
261             for (Iterator JavaDoc it = this.serverSessionPools.values().iterator(); it.hasNext();) {
262                 ObjectPool pool = (ObjectPool) it.next();
263                 try {
264                     pool.close();
265                 }
266                 catch (Exception JavaDoc ex) {
267                     logger.error("Failed to close ServerSession pool", ex);
268                 }
269             }
270         }
271     }
272
273 }
274
Popular Tags