View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.client.integration;
29  
30  import java.util.concurrent.TimeUnit;
31  
32  import org.apache.http.HttpHost;
33  import org.apache.http.HttpResponse;
34  import org.apache.http.client.ClientProtocolException;
35  import org.apache.http.client.HttpClient;
36  import org.apache.http.client.methods.HttpGet;
37  import org.apache.http.client.methods.HttpUriRequest;
38  import org.apache.http.conn.HttpClientConnectionManager;
39  import org.apache.http.localserver.LocalServerTestBase;
40  import org.apache.http.util.EntityUtils;
41  import org.junit.Test;
42  
43  public class TestIdleConnectionEviction extends LocalServerTestBase {
44  
45      @Test
46      public void testIdleConnectionEviction() throws Exception {
47          this.connManager.setDefaultMaxPerRoute(10);
48          this.connManager.setMaxTotal(50);
49  
50          final HttpHost target = start();
51  
52          final IdleConnectionMonitor idleConnectionMonitor = new IdleConnectionMonitor(this.connManager);
53          idleConnectionMonitor.start();
54  
55          final HttpGet httpget = new HttpGet("/random/1024");
56          final WorkerThread[] workers = new WorkerThread[5];
57          for (int i = 0; i < workers.length; i++) {
58              workers[i] = new WorkerThread(httpclient, target, httpget, 200);
59          }
60          for (final WorkerThread worker : workers) {
61              worker.start();
62          }
63          for (final WorkerThread worker : workers) {
64              worker.join();
65              final Exception ex = worker.getException();
66              if (ex != null) {
67                  throw ex;
68              }
69          }
70          idleConnectionMonitor.shutdown();
71      }
72  
73      static class WorkerThread extends Thread {
74  
75          private final HttpClient httpclient;
76          private final HttpHost target;
77          private final HttpUriRequest request;
78          private final int count;
79  
80          private volatile Exception ex;
81  
82          public WorkerThread(
83                  final HttpClient httpclient,
84                  final HttpHost target,
85                  final HttpUriRequest request,
86                  final int count) {
87              super();
88              this.httpclient = httpclient;
89              this.target = target;
90              this.request = request;
91              this.count = count;
92          }
93  
94          @Override
95          public void run() {
96              try {
97                  for (int i = 0; i < this.count; i++) {
98                      final HttpResponse response = this.httpclient.execute(this.target, this.request);
99                      final int status = response.getStatusLine().getStatusCode();
100                     if (status != 200) {
101                         this.request.abort();
102                         throw new ClientProtocolException("Unexpected status code: " + status);
103                     }
104                     EntityUtils.consume(response.getEntity());
105                     Thread.sleep(10);
106                 }
107             } catch (final Exception ex) {
108                 this.ex = ex;
109             }
110         }
111 
112         public Exception getException() {
113             return ex;
114         }
115 
116     }
117 
118     public static class IdleConnectionMonitor extends Thread {
119 
120         private final HttpClientConnectionManager cm;
121         private volatile boolean shutdown;
122 
123         public IdleConnectionMonitor(final HttpClientConnectionManager cm) {
124             super();
125             this.cm = cm;
126             setDaemon(true);
127         }
128 
129         @Override
130         public void run() {
131             try {
132                 while (!this.shutdown) {
133                     synchronized (this) {
134                         wait(250);
135                         this.cm.closeIdleConnections(1, TimeUnit.MILLISECONDS);
136                     }
137                 }
138             } catch (final InterruptedException ex) {
139                 // terminate
140             }
141         }
142 
143         public void shutdown() {
144             this.shutdown = true;
145             synchronized (this) {
146                 notifyAll();
147             }
148         }
149 
150     }
151 
152 }