KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xsocket > stream > BlockingConnectionPool


1 // $Id: BlockingConnection.java 1134 2007-04-05 17:44:43Z grro $
2
/*
3  * Copyright (c) xsocket.org, 2006 - 2007. All rights reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
20  * The latest copy of this software may be found on http://www.xsocket.org/
21  */

22 package org.xsocket.stream;
23
24 import java.io.IOException JavaDoc;
25 import java.lang.annotation.Inherited JavaDoc;
26 import java.net.InetAddress JavaDoc;
27 import java.net.InetSocketAddress JavaDoc;
28 import java.util.concurrent.Executor JavaDoc;
29
30
31
32
33 /**
34  * A connection pool implementation. A connection pool will be used on the
35  * client-side, if connections for the same server (address) will be created
36  * in a serial manner in a sort period of time. By pooling such connections
37  * the overhead of establishing a connection will be avoided
38  * For pool management reasons, timeouts can be defined. The
39  * IdleTimeout defines the max idle time in the pool. After this time the
40  * free connection will be closed. In the same way, the max living time
41  * defines the timout of the connection. If a free connection exceeds
42  * this time, the connection will be closed. <br>
43  * Additional the max size of the active connections can be defined.
44  * If a connection is requested and the max limit of the active connection
45  * is reached, the request will be blocked until a connection becomes free
46  * or the maxWaitTime will be reached. <br>
47  *
48  * <pre>
49  * // create a unlimited connection pool with idle timeout 60 sec
50  * BlockingConnectionPool pool = new BlockingConnectionPool(60L * 1000L);
51  *
52  *
53  * IBlockingConnection con = null;
54  *
55  * try {
56  * // retrieve a connection (if no connection is in pool, a new one will be created)
57  * con = pool.getBlockingConnection(host, port);
58  * con.write("Hello");
59  * ...
60  *
61  * // always close the connection! (the connection will be returned into the connection pool)
62  * con.close();
63  *
64  * } catch (IOException) {
65  * if (con != null) {
66  * try {
67  * // if the connection is invalid -> destroy it (it will not return into the pool)
68  * pool.destroyConnection(con);
69  * } catch (Exception ignore) { }
70  * }
71  * }
72  * </pre>
73  *
74  * @author grro@xsocket.org
75  */

76 public final class BlockingConnectionPool extends AbstractConnectionPool {
77     
78     public static final long UNLIMITED_TIMEOUT = AbstractConnectionPool.MAX_TIMEOUT;
79
80     /**
81      * constructor
82      *
83      * @param timeToIdleMillis the max idle time in the pool. After this time the free connection will be closed
84      */

85     public BlockingConnectionPool(long timeToIdleMillis) {
86         this(timeToIdleMillis, Integer.MAX_VALUE, NULL);
87     }
88
89     /**
90      * constructor
91      *
92      * @param timeToIdleMillis the max idle time in the pool. After this time the free connection will be closed
93      * @param timeToLiveMillis the max living time of the connection. If a free connection exeeded this time, the connection will be closed (if it is in pool)
94      * @param maxWaitMillis the max wait time by acquiring a connection from the pool
95      */

96     public BlockingConnectionPool(long timeToIdleMillis, int maxActive, long maxWaitTimeMillis) {
97         super(timeToIdleMillis, MAX_TIMEOUT, maxActive, maxWaitTimeMillis, MAX_SIZE);
98     }
99
100
101     /**
102      * constructor
103      *
104      * @param timeToIdleMillis the max idle time in the pool. After this time the free connection will be closed
105      * @param timeToLiveMillis the max living time of the connection. If a free connection exeeded this time, the connection will be closed (if it is in pool)
106      * @param maxWaitMillis the max wait time by acquiring a connection from the pool
107      * @param maxIdle the max number of free connection in the pool
108      */

109     public BlockingConnectionPool(long timeToIdleMillis, int maxActive, long maxWaitTimeMillis, int maxIdle) {
110         super(timeToIdleMillis, MAX_TIMEOUT, maxActive, maxWaitTimeMillis, maxIdle);
111     }
112     
113
114     /**
115      * get a pool connection for the given address. If no free connection is in the pool,
116      * a new one will be created <br> <br>
117      *
118      * This method is thread safe
119      *
120      * @param host the server address
121      * @param port the sever port
122      * @return the connection
123      * @throws WaitTimeoutException if the wait timeout has been reached (this will only been thrown if wait time has been set)
124      * @throws IOException if an exception occurs
125      */

126     public IBlockingConnection getBlockingConnection(String JavaDoc host, int port) throws IOException JavaDoc, WaitTimeoutException {
127         return (IBlockingConnection) getConnection(new InetSocketAddress JavaDoc(host, port), null);
128     }
129
130     
131     /**
132      * get a pool connection for the given address. If no free connection is in the pool,
133      * a new one will be created <br> <br>
134      *
135      * This method is thread safe
136      *
137      * @param address the server address
138      * @param port the sever port
139      * @return the connection
140      * @throws WaitTimeoutException if the wait timeout has been reached (this will only been thrown if wait time has been set)
141      * @throws IOException if an exception occurs
142      */

143     public IBlockingConnection getBlockingConnection(InetAddress JavaDoc address, int port) throws IOException JavaDoc, WaitTimeoutException {
144         return (IBlockingConnection) getConnection(new InetSocketAddress JavaDoc(address, port), null);
145     }
146
147
148     /**
149      * {@link Inherited}
150      */

151     @Override JavaDoc
152     PoolableConnection createConnection(InetSocketAddress JavaDoc address, Executor JavaDoc workerPool) throws IOException JavaDoc {
153         return new PoolableBlockingConnection(address);
154     }
155
156     
157     
158     private final class PoolableBlockingConnection extends PoolableConnection implements IBlockingConnection {
159         
160         public PoolableBlockingConnection(InetSocketAddress JavaDoc address) throws IOException JavaDoc {
161             super(BlockingConnectionPool.this, new BlockingConnection(address.getAddress(), address.getPort()), address);
162         }
163         
164         public void setReceiveTimeoutMillis(long timeout) {
165             ((BlockingConnection) getDelegee()).setReceiveTimeoutMillis(timeout);
166         }
167         
168         public IBlockingConnection setOption(String JavaDoc name, Object JavaDoc value) throws IOException JavaDoc {
169             return ((BlockingConnection) getDelegee()).setOption(name, value);
170         }
171     }
172 }
173
Popular Tags