1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.apache.http.impl.conn.tsccm;
28
29 import java.io.IOException;
30 import java.lang.ref.Reference;
31 import java.lang.ref.ReferenceQueue;
32 import java.util.HashSet;
33 import java.util.Iterator;
34 import java.util.Set;
35 import java.util.concurrent.TimeUnit;
36 import java.util.concurrent.locks.Lock;
37 import java.util.concurrent.locks.ReentrantLock;
38
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41 import org.apache.http.conn.ConnectionPoolTimeoutException;
42 import org.apache.http.conn.OperatedClientConnection;
43 import org.apache.http.conn.routing.HttpRoute;
44 import org.apache.http.impl.conn.IdleConnectionHandler;
45 import org.apache.http.util.Args;
46
47
48
49
50
51
52
53
54
55
56
57
58 @Deprecated
59 public abstract class AbstractConnPool {
60
61 private final Log log;
62
63
64
65
66 protected final Lock poolLock;
67
68 protected Set<BasicPoolEntry> leasedConnections;
69
70 protected int numConnections;
71
72
73 protected volatile boolean isShutDown;
74
75 protected Set<BasicPoolEntryRef> issuedConnections;
76
77 protected ReferenceQueue<Object> refQueue;
78
79 protected IdleConnectionHandler idleConnHandler;
80
81
82
83
84 protected AbstractConnPool() {
85 super();
86 this.log = LogFactory.getLog(getClass());
87 this.leasedConnections = new HashSet<BasicPoolEntry>();
88 this.idleConnHandler = new IdleConnectionHandler();
89 this.poolLock = new ReentrantLock();
90 }
91
92 public void enableConnectionGC()
93 throws IllegalStateException {
94 }
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112 public final
113 BasicPoolEntry getEntry(
114 final HttpRoute route,
115 final Object state,
116 final long timeout,
117 final TimeUnit timeUnit)
118 throws ConnectionPoolTimeoutException, InterruptedException {
119 return requestPoolEntry(route, state).getPoolEntry(timeout, timeUnit);
120 }
121
122
123
124
125
126
127
128
129 public abstract PoolEntryRequest requestPoolEntry(HttpRoute route, Object state);
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144 public abstract void freeEntry(BasicPoolEntry entry, boolean reusable, long validDuration, TimeUnit timeUnit)
145 ;
146
147 public void handleReference(final Reference<?> ref) {
148 }
149
150 protected abstract void handleLostEntry(HttpRoute route);
151
152
153
154
155
156
157
158
159 public void closeIdleConnections(final long idletime, final TimeUnit timeUnit) {
160
161
162 Args.notNull(timeUnit, "Time unit");
163
164 poolLock.lock();
165 try {
166 idleConnHandler.closeIdleConnections(timeUnit.toMillis(idletime));
167 } finally {
168 poolLock.unlock();
169 }
170 }
171
172 public void closeExpiredConnections() {
173 poolLock.lock();
174 try {
175 idleConnHandler.closeExpiredConnections();
176 } finally {
177 poolLock.unlock();
178 }
179 }
180
181
182
183
184
185 public abstract void deleteClosedConnections();
186
187
188
189
190
191 public void shutdown() {
192
193 poolLock.lock();
194 try {
195
196 if (isShutDown) {
197 return;
198 }
199
200
201 final Iterator<BasicPoolEntry> iter = leasedConnections.iterator();
202 while (iter.hasNext()) {
203 final BasicPoolEntry entry = iter.next();
204 iter.remove();
205 closeConnection(entry.getConnection());
206 }
207 idleConnHandler.removeAll();
208
209 isShutDown = true;
210
211 } finally {
212 poolLock.unlock();
213 }
214 }
215
216
217
218
219
220
221
222 protected void closeConnection(final OperatedClientConnection conn) {
223 if (conn != null) {
224 try {
225 conn.close();
226 } catch (final IOException ex) {
227 log.debug("I/O error closing connection", ex);
228 }
229 }
230 }
231
232 }
233