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  package org.apache.http.impl.nio.conn;
28  
29  import java.util.concurrent.TimeUnit;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.apache.http.annotation.Contract;
34  import org.apache.http.annotation.ThreadingBehavior;
35  import org.apache.http.conn.routing.HttpRoute;
36  import org.apache.http.nio.NHttpClientConnection;
37  import org.apache.http.nio.conn.ManagedNHttpClientConnection;
38  import org.apache.http.nio.pool.AbstractNIOConnPool;
39  import org.apache.http.nio.pool.NIOConnFactory;
40  import org.apache.http.nio.pool.SocketAddressResolver;
41  import org.apache.http.nio.reactor.ConnectingIOReactor;
42  
43  @Contract(threading = ThreadingBehavior.SAFE)
44  class CPool extends AbstractNIOConnPool<HttpRoute, ManagedNHttpClientConnection, CPoolEntry> {
45  
46      private final Log log = LogFactory.getLog(CPool.class);
47  
48      private final long timeToLive;
49      private final TimeUnit tunit;
50  
51      public CPool(
52              final ConnectingIOReactor ioreactor,
53              final NIOConnFactory<HttpRoute, ManagedNHttpClientConnection> connFactory,
54              final SocketAddressResolver<HttpRoute> addressResolver,
55              final int defaultMaxPerRoute, final int maxTotal,
56              final long timeToLive, final TimeUnit tunit) {
57          super(ioreactor, connFactory, addressResolver, defaultMaxPerRoute, maxTotal);
58          this.timeToLive = timeToLive;
59          this.tunit = tunit;
60      }
61  
62      @Override
63      protected CPoolEntry createEntry(final HttpRoute route, final ManagedNHttpClientConnection conn) {
64          final CPoolEntry entry =  new CPoolEntry(this.log, conn.getId(), route, conn, this.timeToLive, this.tunit);
65          entry.setSocketTimeout(conn.getSocketTimeout());
66          return entry;
67      }
68  
69      @Override
70      protected void onLease(final CPoolEntry entry) {
71          final NHttpClientConnection conn = entry.getConnection();
72          conn.setSocketTimeout(entry.getSocketTimeout());
73      }
74  
75      @Override
76      protected void onRelease(final CPoolEntry entry) {
77          final NHttpClientConnection conn = entry.getConnection();
78          conn.setSocketTimeout(0);
79      }
80  
81  }