001/* 002 * ==================================================================== 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 * ==================================================================== 020 * 021 * This software consists of voluntary contributions made by many 022 * individuals on behalf of the Apache Software Foundation. For more 023 * information on the Apache Software Foundation, please see 024 * <http://www.apache.org/>. 025 * 026 */ 027package org.apache.http.impl.pool; 028 029import java.util.concurrent.atomic.AtomicLong; 030 031import org.apache.http.HttpClientConnection; 032import org.apache.http.HttpHost; 033import org.apache.http.annotation.ThreadingBehavior; 034import org.apache.http.annotation.Contract; 035import org.apache.http.config.ConnectionConfig; 036import org.apache.http.config.SocketConfig; 037import org.apache.http.params.HttpParams; 038import org.apache.http.pool.AbstractConnPool; 039import org.apache.http.pool.ConnFactory; 040 041/** 042 * A very basic {@link org.apache.http.pool.ConnPool} implementation that 043 * represents a pool of blocking {@link HttpClientConnection} connections 044 * identified by an {@link HttpHost} instance. Please note this pool 045 * implementation does not support complex routes via a proxy cannot 046 * differentiate between direct and proxied connections. 047 * 048 * @see HttpHost 049 * @since 4.2 050 */ 051@SuppressWarnings("deprecation") 052@Contract(threading = ThreadingBehavior.SAFE_CONDITIONAL) 053public class BasicConnPool extends AbstractConnPool<HttpHost, HttpClientConnection, BasicPoolEntry> { 054 055 private static final AtomicLong COUNTER = new AtomicLong(); 056 057 public BasicConnPool(final ConnFactory<HttpHost, HttpClientConnection> connFactory) { 058 super(connFactory, 2, 20); 059 } 060 061 /** 062 * @deprecated (4.3) use {@link BasicConnPool#BasicConnPool(SocketConfig, ConnectionConfig)} 063 */ 064 @Deprecated 065 public BasicConnPool(final HttpParams params) { 066 super(new BasicConnFactory(params), 2, 20); 067 } 068 069 /** 070 * @since 4.3 071 */ 072 public BasicConnPool(final SocketConfig sconfig, final ConnectionConfig cconfig) { 073 super(new BasicConnFactory(sconfig, cconfig), 2, 20); 074 } 075 076 /** 077 * @since 4.3 078 */ 079 public BasicConnPool() { 080 super(new BasicConnFactory(SocketConfig.DEFAULT, ConnectionConfig.DEFAULT), 2, 20); 081 } 082 083 @Override 084 protected BasicPoolEntry createEntry( 085 final HttpHost host, 086 final HttpClientConnection conn) { 087 return new BasicPoolEntry(Long.toString(COUNTER.getAndIncrement()), host, conn); 088 } 089 090 @Override 091 protected boolean validate(final BasicPoolEntry entry) { 092 return !entry.getConnection().isStale(); 093 } 094 095}