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.pool;
28  
29  import java.net.InetSocketAddress;
30  import java.net.SocketAddress;
31  import java.util.concurrent.Future;
32  import java.util.concurrent.TimeUnit;
33  import java.util.concurrent.atomic.AtomicLong;
34  
35  import org.apache.http.HttpHost;
36  import org.apache.http.annotation.ThreadSafe;
37  import org.apache.http.concurrent.FutureCallback;
38  import org.apache.http.nio.NHttpClientConnection;
39  import org.apache.http.nio.pool.AbstractNIOConnPool;
40  import org.apache.http.nio.pool.NIOConnFactory;
41  import org.apache.http.nio.reactor.ConnectingIOReactor;
42  import org.apache.http.params.HttpConnectionParams;
43  import org.apache.http.params.HttpParams;
44  import org.apache.http.pool.ConnPool;
45  
46  /**
47   * A very basic {@link ConnPool} implementation that represents a pool
48   * of non-blocking {@link NHttpClientConnection} connections identified by
49   * an {@link HttpHost} instance. Please note this pool implementation
50   * does not support complex routes via a proxy cannot differentiate between
51   * direct and proxied connections.
52   * <p>
53   * The following parameters can be used to customize the behavior of this
54   * class:
55   * <ul>
56   *  <li>{@link org.apache.http.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li>
57   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SO_TIMEOUT}</li>
58   *  <li>{@link org.apache.http.params.CoreConnectionPNames#CONNECTION_TIMEOUT}</li>
59   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SOCKET_BUFFER_SIZE}</li>
60   *  <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_HEADER_COUNT}</li>
61   *  <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li>
62   * </ul>
63   *
64   * @see HttpHost
65   * @since 4.2
66   */
67  @ThreadSafe
68  public class BasicNIOConnPool extends AbstractNIOConnPool<HttpHost, NHttpClientConnection, BasicNIOPoolEntry> {
69  
70      private static AtomicLong COUNTER = new AtomicLong();
71  
72      private final HttpParams params;
73  
74      public BasicNIOConnPool(
75              final ConnectingIOReactor ioreactor,
76              final NIOConnFactory<HttpHost, NHttpClientConnection> connFactory,
77              final HttpParams params) {
78          super(ioreactor, connFactory, 2, 20);
79          if (params == null) {
80              throw new IllegalArgumentException("HTTP parameters may not be null");
81          }
82          this.params = params;
83      }
84  
85      public BasicNIOConnPool(
86              final ConnectingIOReactor ioreactor, final HttpParams params) {
87          this(ioreactor, new BasicNIOConnFactory(params), params);
88      }
89  
90      @Override
91      protected SocketAddress resolveRemoteAddress(final HttpHost host) {
92          return new InetSocketAddress(host.getHostName(), host.getPort());
93      }
94  
95      @Override
96      protected SocketAddress resolveLocalAddress(final HttpHost host) {
97          return null;
98      }
99  
100     @Override
101     protected BasicNIOPoolEntry createEntry(final HttpHost host, final NHttpClientConnection conn) {
102         return new BasicNIOPoolEntry(Long.toString(COUNTER.getAndIncrement()), host, conn);
103     }
104 
105     @Override
106     public Future<BasicNIOPoolEntry> lease(
107             final HttpHost route,
108             final Object state,
109             final FutureCallback<BasicNIOPoolEntry> callback) {
110         int connectTimeout = HttpConnectionParams.getConnectionTimeout(this.params);
111         return super.lease(route, state, connectTimeout, TimeUnit.MILLISECONDS, callback);
112     }
113 
114     @Override
115     public Future<BasicNIOPoolEntry> lease(
116             final HttpHost route,
117             final Object state) {
118         int connectTimeout = HttpConnectionParams.getConnectionTimeout(this.params);
119         return super.lease(route, state, connectTimeout, TimeUnit.MILLISECONDS, null);
120     }
121 
122 }