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.pool;
28  
29  import java.util.concurrent.atomic.AtomicLong;
30  
31  import org.apache.http.HttpClientConnection;
32  import org.apache.http.HttpHost;
33  import org.apache.http.annotation.ThreadingBehavior;
34  import org.apache.http.annotation.Contract;
35  import org.apache.http.config.ConnectionConfig;
36  import org.apache.http.config.SocketConfig;
37  import org.apache.http.params.HttpParams;
38  import org.apache.http.pool.AbstractConnPool;
39  import org.apache.http.pool.ConnFactory;
40  
41  /**
42   * A very basic {@link org.apache.http.pool.ConnPool} implementation that
43   * represents a pool of blocking {@link HttpClientConnection} connections
44   * identified by an {@link HttpHost} instance. Please note this pool
45   * implementation does not support complex routes via a proxy cannot
46   * differentiate between direct and proxied connections.
47   *
48   * @see HttpHost
49   * @since 4.2
50   */
51  @SuppressWarnings("deprecation")
52  @Contract(threading = ThreadingBehavior.SAFE_CONDITIONAL)
53  public class BasicConnPool extends AbstractConnPool<HttpHost, HttpClientConnection, BasicPoolEntry> {
54  
55      private static final AtomicLong COUNTER = new AtomicLong();
56  
57      public BasicConnPool(final ConnFactory<HttpHost, HttpClientConnection> connFactory) {
58          super(connFactory, 2, 20);
59      }
60  
61      /**
62       * @deprecated (4.3) use {@link BasicConnPool#BasicConnPool(SocketConfig, ConnectionConfig)}
63       */
64      @Deprecated
65      public BasicConnPool(final HttpParams params) {
66          super(new BasicConnFactory(params), 2, 20);
67      }
68  
69      /**
70       * @since 4.3
71       */
72      public BasicConnPool(final SocketConfig sconfig, final ConnectionConfig cconfig) {
73          super(new BasicConnFactory(sconfig, cconfig), 2, 20);
74      }
75  
76      /**
77       * @since 4.3
78       */
79      public BasicConnPool() {
80          super(new BasicConnFactory(SocketConfig.DEFAULT, ConnectionConfig.DEFAULT), 2, 20);
81      }
82  
83      @Override
84      protected BasicPoolEntry createEntry(
85              final HttpHost host,
86              final HttpClientConnection conn) {
87          return new BasicPoolEntry(Long.toString(COUNTER.getAndIncrement()), host, conn);
88      }
89  
90      @Override
91      protected boolean validate(final BasicPoolEntry entry) {
92          return !entry.getConnection().isStale();
93      }
94  
95  }