77 * @param url The URL contains info about the host and port
78 * @param http The HttpClient to be cached
79 */
80 public synchronized void put(final URL url, Object obj, HttpClient http) {
81 boolean startThread = (keepAliveTimer == null);
82 if (!startThread) {
83 if (!keepAliveTimer.isAlive()) {
84 startThread = true;
85 }
86 }
87 if (startThread) {
88 clear();
89 /* Unfortunately, we can't always believe the keep-alive timeout we got
90 * back from the server. If I'm connected through a Netscape proxy
91 * to a server that sent me a keep-alive
92 * time of 15 sec, the proxy unilaterally terminates my connection
93 * The robustness to get around this is in HttpClient.parseHTTP()
94 */
95 final KeepAliveCache cache = this;
96 java.security.AccessController.doPrivileged(
97 new java.security.PrivilegedAction<Void>() {
98 public Void run() {
99 keepAliveTimer = new InnocuousThread(cache, "Keep-Alive-Timer");
100 keepAliveTimer.setDaemon(true);
101 keepAliveTimer.setPriority(Thread.MAX_PRIORITY - 2);
102 // Set the context class loader to null in order to avoid
103 // keeping a strong reference to an application classloader.
104 keepAliveTimer.setContextClassLoader(null);
105 keepAliveTimer.start();
106 return null;
107 }
108 });
109 }
110
111 KeepAliveKey key = new KeepAliveKey(url, obj);
112 ClientVector v = super.get(key);
113
114 if (v == null) {
115 int keepAliveTimeout = http.getKeepAliveTimeout();
116 v = new ClientVector(keepAliveTimeout > 0?
117 keepAliveTimeout*1000 : LIFETIME);
161 @Override
162 public void run() {
163 do {
164 try {
165 Thread.sleep(LIFETIME);
166 } catch (InterruptedException e) {}
167 synchronized (this) {
168 /* Remove all unused HttpClients. Starting from the
169 * bottom of the stack (the least-recently used first).
170 * REMIND: It'd be nice to not remove *all* connections
171 * that aren't presently in use. One could have been added
172 * a second ago that's still perfectly valid, and we're
173 * needlessly axing it. But it's not clear how to do this
174 * cleanly, and doing it right may be more trouble than it's
175 * worth.
176 */
177
178 long currentTime = System.currentTimeMillis();
179
180 ArrayList<KeepAliveKey> keysToRemove
181 = new ArrayList<KeepAliveKey>();
182
183 for (KeepAliveKey key : keySet()) {
184 ClientVector v = get(key);
185 synchronized (v) {
186 int i;
187
188 for (i = 0; i < v.size(); i++) {
189 KeepAliveEntry e = v.elementAt(i);
190 if ((currentTime - e.idleStartTime) > v.nap) {
191 HttpClient h = e.hc;
192 h.closeServer();
193 } else {
194 break;
195 }
196 }
197 v.subList(0, i).clear();
198
199 if (v.size() == 0) {
200 keysToRemove.add(key);
201 }
|
77 * @param url The URL contains info about the host and port
78 * @param http The HttpClient to be cached
79 */
80 public synchronized void put(final URL url, Object obj, HttpClient http) {
81 boolean startThread = (keepAliveTimer == null);
82 if (!startThread) {
83 if (!keepAliveTimer.isAlive()) {
84 startThread = true;
85 }
86 }
87 if (startThread) {
88 clear();
89 /* Unfortunately, we can't always believe the keep-alive timeout we got
90 * back from the server. If I'm connected through a Netscape proxy
91 * to a server that sent me a keep-alive
92 * time of 15 sec, the proxy unilaterally terminates my connection
93 * The robustness to get around this is in HttpClient.parseHTTP()
94 */
95 final KeepAliveCache cache = this;
96 java.security.AccessController.doPrivileged(
97 new java.security.PrivilegedAction<>() {
98 public Void run() {
99 keepAliveTimer = new InnocuousThread(cache, "Keep-Alive-Timer");
100 keepAliveTimer.setDaemon(true);
101 keepAliveTimer.setPriority(Thread.MAX_PRIORITY - 2);
102 // Set the context class loader to null in order to avoid
103 // keeping a strong reference to an application classloader.
104 keepAliveTimer.setContextClassLoader(null);
105 keepAliveTimer.start();
106 return null;
107 }
108 });
109 }
110
111 KeepAliveKey key = new KeepAliveKey(url, obj);
112 ClientVector v = super.get(key);
113
114 if (v == null) {
115 int keepAliveTimeout = http.getKeepAliveTimeout();
116 v = new ClientVector(keepAliveTimeout > 0?
117 keepAliveTimeout*1000 : LIFETIME);
161 @Override
162 public void run() {
163 do {
164 try {
165 Thread.sleep(LIFETIME);
166 } catch (InterruptedException e) {}
167 synchronized (this) {
168 /* Remove all unused HttpClients. Starting from the
169 * bottom of the stack (the least-recently used first).
170 * REMIND: It'd be nice to not remove *all* connections
171 * that aren't presently in use. One could have been added
172 * a second ago that's still perfectly valid, and we're
173 * needlessly axing it. But it's not clear how to do this
174 * cleanly, and doing it right may be more trouble than it's
175 * worth.
176 */
177
178 long currentTime = System.currentTimeMillis();
179
180 ArrayList<KeepAliveKey> keysToRemove
181 = new ArrayList<>();
182
183 for (KeepAliveKey key : keySet()) {
184 ClientVector v = get(key);
185 synchronized (v) {
186 int i;
187
188 for (i = 0; i < v.size(); i++) {
189 KeepAliveEntry e = v.elementAt(i);
190 if ((currentTime - e.idleStartTime) > v.nap) {
191 HttpClient h = e.hc;
192 h.closeServer();
193 } else {
194 break;
195 }
196 }
197 v.subList(0, i).clear();
198
199 if (v.size() == 0) {
200 keysToRemove.add(key);
201 }
|