Top Description Inners Fields Constructors Methods
org.apache.hc.client5.http.impl.io

public Class BasicHttpClientConnectionManager

extends Object
implements HttpClientConnectionManager
Class Inheritance
All Implemented Interfaces
org.apache.hc.client5.http.io.HttpClientConnectionManager, org.apache.hc.core5.io.ModalCloseable, java.io.Closeable, java.lang.AutoCloseable
Annotations
@Contract
threading:SAFE
Imports
java.io.IOException, java.time.Instant, java.util.Objects, java.util.concurrent.ExecutionException, .TimeoutException, java.util.concurrent.atomic.AtomicBoolean, .AtomicLong, .AtomicReference, org.apache.hc.client5.http.DnsResolver, .HttpRoute, .SchemePortResolver, org.apache.hc.client5.http.config.ConnectionConfig, .TlsConfig, org.apache.hc.client5.http.impl.ConnPoolSupport, .ConnectionShutdownException, org.apache.hc.client5.http.io.ConnectionEndpoint, .HttpClientConnectionManager, .HttpClientConnectionOperator, .LeaseRequest, .ManagedHttpClientConnection, org.apache.hc.client5.http.socket.ConnectionSocketFactory, .PlainConnectionSocketFactory, org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory, org.apache.hc.core5.annotation.Contract, .ThreadingBehavior, org.apache.hc.core5.http.ClassicHttpRequest, .ClassicHttpResponse, .HttpException, .HttpHost, .URIScheme, org.apache.hc.core5.http.config.Lookup, .Registry, .RegistryBuilder, org.apache.hc.core5.http.impl.io.HttpRequestExecutor, org.apache.hc.core5.http.io.HttpConnectionFactory, .SocketConfig, org.apache.hc.core5.http.protocol.HttpContext, org.apache.hc.core5.io.CloseMode, org.apache.hc.core5.util.Args, .Asserts, .Deadline, .TimeValue, .Timeout, org.slf4j.Logger, .LoggerFactory

A connection manager for a single connection. This connection manager maintains only one active connection. Even though this class is fully thread-safe it ought to be used by one execution thread only, as only one thread a time can lease the connection at a time.

This connection manager will make an effort to reuse the connection for subsequent requests with the same route. It will, however, close the existing connection and open it for the given route, if the route of the persistent connection does not match that of the connection request. If the connection has been already been allocated IllegalStateException is thrown.

This connection manager implementation should be used inside an EJB container instead of PoolingHttpClientConnectionManager.

Since
4.3

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv class

Field Summary

Modifier and TypeField and Description
private final AtomicBoolean
private ManagedHttpClientConnection
private ConnectionConfig
private final HttpClientConnectionOperator
private final HttpConnectionFactory<ManagedHttpClientConnection>
private static final AtomicLong
private long
private long
private final String
private boolean
private static final Logger
private HttpRoute
private SocketConfig
private Object
private TlsConfig
private long

Constructor Summary

AccessConstructor and Description
public
BasicHttpClientConnectionManager(final Lookup<ConnectionSocketFactory> socketFactoryRegistry, final HttpConnectionFactory<ManagedHttpClientConnection> connFactory, final SchemePortResolver schemePortResolver, final DnsResolver dnsResolver)

public
public
public
public

Method Summary

Modifier and TypeMethod and Description
private BasicHttpClientConnectionManager.InternalConnectionEndpoint
cast(final ConnectionEndpoint endpoint)

private void
public void
close()

Implements java.io.Closeable.close.

Closes this stream and releases any system resources associated with it.
public void
close(final CloseMode
How to close the receiver.
closeMode
)

Implements org.apache.hc.core5.io.ModalCloseable.close.

Closes this process or endpoint and releases any system resources associated with it.
private synchronized void
closeConnection(final CloseMode closeMode)

public synchronized void
public synchronized void
closeIdle(final TimeValue idleTime)

public synchronized void
connect(final ConnectionEndpoint
the managed endpoint.
endpoint
,
final TimeValue
connect timeout.
timeout
,
final HttpContext
the actual HTTP context.
context
)

Implements org.apache.hc.client5.http.io.HttpClientConnectionManager.connect.

Connects the endpoint to the initial hop (connection target in case of a direct route or to the first proxy hop in case of a route via a proxy or multiple proxies).
pack-priv synchronized ManagedHttpClientConnection
getConnection(final HttpRoute route, final Object state)

public synchronized ConnectionConfig
private static Registry<ConnectionSocketFactory>
pack-priv HttpRoute
public synchronized SocketConfig
pack-priv Object
public synchronized TlsConfig
public TimeValue
pack-priv boolean

Returns:

true if the connection manager has been shut down and is closed, otherwise return false.
isClosed
()

Method that can be called to determine whether the connection manager has been shut down and is closed or not.

public LeaseRequest
lease(final String id, final HttpRoute route, final Object state)

public LeaseRequest
lease(final String
unique operation ID or null.
id
,
final HttpRoute
HTTP route of the requested connection.
route
,
final Timeout
lease request timeout.
requestTimeout
,
final Object
expected state of the connection or null if the connection is not expected to carry any state.
state
)

Implements org.apache.hc.client5.http.io.HttpClientConnectionManager.lease.

Returns a LeaseRequest object which can be used to obtain a ConnectionEndpoint to cancel the request by calling LeaseRequest#cancel().
public synchronized void
release(final ConnectionEndpoint
the managed endpoint.
endpoint
,
final Object
the new connection state of null if state-less.
state
,
final TimeValue
the duration of time this connection is valid for reuse.
keepAlive
)

Implements org.apache.hc.client5.http.io.HttpClientConnectionManager.release.

Releases the endpoint back to the manager making it potentially re-usable by other consumers.
public synchronized void
setConnectionConfig(final ConnectionConfig connectionConfig)

public synchronized void
setSocketConfig(final SocketConfig socketConfig)

public synchronized void
setTlsConfig(final TlsConfig tlsConfig)

public void
setValidateAfterInactivity(final TimeValue validateAfterInactivity)

Defines period of inactivity after which persistent connections must be re-validated prior to being lease(String, HttpRoute, Object) leased} to the consumer.
public synchronized void
upgrade(final ConnectionEndpoint
the managed endpoint.
endpoint
,
final HttpContext
the actual HTTP context.
context
)

Implements org.apache.hc.client5.http.io.HttpClientConnectionManager.upgrade.

Upgrades transport security of the given endpoint by using the TLS security protocol.
private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

closedback to summary
private final AtomicBoolean closed
connback to summary
private ManagedHttpClientConnection conn
connectionConfigback to summary
private ConnectionConfig connectionConfig
connectionOperatorback to summary
private final HttpClientConnectionOperator connectionOperator
connFactoryback to summary
private final HttpConnectionFactory<ManagedHttpClientConnection> connFactory
COUNTback to summary
private static final AtomicLong COUNT
createdback to summary
private long created
expiryback to summary
private long expiry
idback to summary
private final String id
leasedback to summary
private boolean leased
LOGback to summary
private static final Logger LOG
routeback to summary
private HttpRoute route
socketConfigback to summary
private SocketConfig socketConfig
stateback to summary
private Object state
tlsConfigback to summary
private TlsConfig tlsConfig
updatedback to summary
private long updated

Constructor Detail

BasicHttpClientConnectionManagerback to summary
public BasicHttpClientConnectionManager(final Lookup<ConnectionSocketFactory> socketFactoryRegistry, final HttpConnectionFactory<ManagedHttpClientConnection> connFactory, final SchemePortResolver schemePortResolver, final DnsResolver dnsResolver)
BasicHttpClientConnectionManagerback to summary
public BasicHttpClientConnectionManager(final HttpClientConnectionOperator httpClientConnectionOperator, final HttpConnectionFactory<ManagedHttpClientConnection> connFactory)
Since
4.4
BasicHttpClientConnectionManagerback to summary
public BasicHttpClientConnectionManager(final Lookup<ConnectionSocketFactory> socketFactoryRegistry, final HttpConnectionFactory<ManagedHttpClientConnection> connFactory)
BasicHttpClientConnectionManagerback to summary
public BasicHttpClientConnectionManager(final Lookup<ConnectionSocketFactory> socketFactoryRegistry)
BasicHttpClientConnectionManagerback to summary
public BasicHttpClientConnectionManager()

Method Detail

castback to summary
private BasicHttpClientConnectionManager.InternalConnectionEndpoint cast(final ConnectionEndpoint endpoint)
checkExpiryback to summary
private void checkExpiry()
closeback to summary
public void close()

Implements java.io.Closeable.close.

Doc from java.io.Closeable.close.

Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect.

As noted in AutoCloseable#close(), cases where the close may fail require careful attention. It is strongly advised to relinquish the underlying resources and to internally mark the Closeable as closed, prior to throwing the IOException.

Annotations
@Override
closeback to summary
public void close(final CloseMode closeMode)

Implements org.apache.hc.core5.io.ModalCloseable.close.

Doc from org.apache.hc.core5.io.ModalCloseable.close.

Closes this process or endpoint and releases any system resources associated with it. If the endpoint or the process is already closed then invoking this method has no effect.

Parameters
closeMode:CloseMode

How to close the receiver.

Annotations
@Override
closeConnectionback to summary
private synchronized void closeConnection(final CloseMode closeMode)
closeExpiredback to summary
public synchronized void closeExpired()
closeIdleback to summary
public synchronized void closeIdle(final TimeValue idleTime)
connectback to summary
public synchronized void connect(final ConnectionEndpoint endpoint, final TimeValue timeout, final HttpContext context) throws IOException

Implements org.apache.hc.client5.http.io.HttpClientConnectionManager.connect.

Doc from org.apache.hc.client5.http.io.HttpClientConnectionManager.connect.

Connects the endpoint to the initial hop (connection target in case of a direct route or to the first proxy hop in case of a route via a proxy or multiple proxies).

Parameters
endpoint:ConnectionEndpoint

the managed endpoint.

timeout:TimeValue

connect timeout.

context:HttpContext

the actual HTTP context.

Annotations
@Override
getConnectionback to summary
pack-priv synchronized ManagedHttpClientConnection getConnection(final HttpRoute route, final Object state) throws IOException
getConnectionConfigback to summary
public synchronized ConnectionConfig getConnectionConfig()
Since
5.2
getDefaultRegistryback to summary
private static Registry<ConnectionSocketFactory> getDefaultRegistry()
getRouteback to summary
pack-priv HttpRoute getRoute()
getSocketConfigback to summary
public synchronized SocketConfig getSocketConfig()
getStateback to summary
pack-priv Object getState()
getTlsConfigback to summary
public synchronized TlsConfig getTlsConfig()
Since
5.2
getValidateAfterInactivityback to summary
public TimeValue getValidateAfterInactivity()

Deprecated

Use getConnectionConfig()

Annotations
@Deprecated
Since
5.1
See Also
setValidateAfterInactivity(TimeValue)
isClosedback to summary
pack-priv boolean isClosed()

Method that can be called to determine whether the connection manager has been shut down and is closed or not.

Returns:boolean

true if the connection manager has been shut down and is closed, otherwise return false.

leaseback to summary
public LeaseRequest lease(final String id, final HttpRoute route, final Object state)
leaseback to summary
public LeaseRequest lease(final String id, final HttpRoute route, final Timeout requestTimeout, final Object state)

Implements org.apache.hc.client5.http.io.HttpClientConnectionManager.lease.

Doc from org.apache.hc.client5.http.io.HttpClientConnectionManager.lease.

Returns a LeaseRequest object which can be used to obtain a ConnectionEndpoint to cancel the request by calling LeaseRequest#cancel().

Please note that newly allocated endpoints can be leased disconnected. The consumer of the endpoint is responsible for fully establishing the route to the endpoint target by calling connect(ConnectionEndpoint, TimeValue, HttpContext) in order to connect directly to the target or to the first proxy hop, and optionally calling upgrade(ConnectionEndpoint, HttpContext) method to upgrade the underlying transport to Transport Layer Security after having executed a CONNECT method to all intermediate proxy hops.

Parameters
id:String

unique operation ID or null.

route:HttpRoute

HTTP route of the requested connection.

requestTimeout:Timeout

lease request timeout.

state:Object

expected state of the connection or null if the connection is not expected to carry any state.

Annotations
@Override
releaseback to summary
public synchronized void release(final ConnectionEndpoint endpoint, final Object state, final TimeValue keepAlive)

Implements org.apache.hc.client5.http.io.HttpClientConnectionManager.release.

Doc from org.apache.hc.client5.http.io.HttpClientConnectionManager.release.

Releases the endpoint back to the manager making it potentially re-usable by other consumers. Optionally, the maximum period of how long the manager should keep the connection alive can be defined using validDuration and timeUnit parameters.

Parameters
endpoint:ConnectionEndpoint

the managed endpoint.

state:Object

the new connection state of null if state-less.

keepAlive:TimeValue

the duration of time this connection is valid for reuse.

Annotations
@Override
setConnectionConfigback to summary
public synchronized void setConnectionConfig(final ConnectionConfig connectionConfig)
Since
5.2
setSocketConfigback to summary
public synchronized void setSocketConfig(final SocketConfig socketConfig)
setTlsConfigback to summary
public synchronized void setTlsConfig(final TlsConfig tlsConfig)
Since
5.2
setValidateAfterInactivityback to summary
public void setValidateAfterInactivity(final TimeValue validateAfterInactivity)

Defines period of inactivity after which persistent connections must be re-validated prior to being lease(String, HttpRoute, Object) leased} to the consumer. Negative values passed to this method disable connection validation. This check helps detect connections that have become stale (half-closed) while kept inactive in the pool.

Annotations
@Deprecated
Since
5.1
upgradeback to summary
public synchronized void upgrade(final ConnectionEndpoint endpoint, final HttpContext context) throws IOException

Implements org.apache.hc.client5.http.io.HttpClientConnectionManager.upgrade.

Doc from org.apache.hc.client5.http.io.HttpClientConnectionManager.upgrade.

Upgrades transport security of the given endpoint by using the TLS security protocol.

Parameters
endpoint:ConnectionEndpoint

the managed endpoint.

context:HttpContext

the actual HTTP context.

Annotations
@Override
validateback to summary
private void validate()
org.apache.hc.client5.http.impl.io back to summary

pack-priv Class BasicHttpClientConnectionManager.InternalConnectionEndpoint

extends ConnectionEndpoint
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final AtomicReference<ManagedHttpClientConnection>
private final HttpRoute

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
close(final CloseMode
How to close the receiver.
closeMode
)

Implements org.apache.hc.core5.io.ModalCloseable.close.

Closes this process or endpoint and releases any system resources associated with it.
public void
close()

Implements java.io.Closeable.close.

Closes this stream and releases any system resources associated with it.
pack-priv ManagedHttpClientConnection
public ClassicHttpResponse
execute(final String
unique operation ID or null.
exchangeId
,
final ClassicHttpRequest
the request message.
request
,
final HttpRequestExecutor
the request executor.
requestExecutor
,
final HttpContext
the execution context.
context
)

Implements abstract org.apache.hc.client5.http.io.ConnectionEndpoint.execute.

Executes HTTP request using the provided request executor.
pack-priv ManagedHttpClientConnection
pack-priv HttpRoute
pack-priv ManagedHttpClientConnection
public boolean
isConnected()

Implements abstract org.apache.hc.client5.http.io.ConnectionEndpoint.isConnected.

Determines if the connection to the remote endpoint is still open and valid.
public void
setSocketTimeout(final Timeout
timeout value
timeout
)

Implements abstract org.apache.hc.client5.http.io.ConnectionEndpoint.setSocketTimeout.

Sets the socket timeout value.

Field Detail

connRefback to summary
private final AtomicReference<ManagedHttpClientConnection> connRef
routeback to summary
private final HttpRoute route

Constructor Detail

InternalConnectionEndpointback to summary
public InternalConnectionEndpoint(final HttpRoute route, final ManagedHttpClientConnection conn)

Method Detail

closeback to summary
public void close(final CloseMode closeMode)

Implements org.apache.hc.core5.io.ModalCloseable.close.

Doc from org.apache.hc.core5.io.ModalCloseable.close.

Closes this process or endpoint and releases any system resources associated with it. If the endpoint or the process is already closed then invoking this method has no effect.

Parameters
closeMode:CloseMode

How to close the receiver.

Annotations
@Override
closeback to summary
public void close() throws IOException

Implements java.io.Closeable.close.

Doc from java.io.Closeable.close.

Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect.

As noted in AutoCloseable#close(), cases where the close may fail require careful attention. It is strongly advised to relinquish the underlying resources and to internally mark the Closeable as closed, prior to throwing the IOException.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs
detachback to summary
pack-priv ManagedHttpClientConnection detach()
executeback to summary
public ClassicHttpResponse execute(final String exchangeId, final ClassicHttpRequest request, final HttpRequestExecutor requestExecutor, final HttpContext context) throws IOException, HttpException

Implements abstract org.apache.hc.client5.http.io.ConnectionEndpoint.execute.

Doc from org.apache.hc.client5.http.io.ConnectionEndpoint.execute.

Executes HTTP request using the provided request executor.

Once the endpoint is no longer needed it MUST be released with close(org.apache.hc.core5.io.CloseMode).

Parameters
exchangeId:String

unique operation ID or null.

request:ClassicHttpRequest

the request message.

requestExecutor:HttpRequestExecutor

the request executor.

context:HttpContext

the execution context.

Annotations
@Override
getConnectionback to summary
pack-priv ManagedHttpClientConnection getConnection()
getRouteback to summary
pack-priv HttpRoute getRoute()
getValidatedConnectionback to summary
pack-priv ManagedHttpClientConnection getValidatedConnection()
isConnectedback to summary
public boolean isConnected()

Implements abstract org.apache.hc.client5.http.io.ConnectionEndpoint.isConnected.

Doc from org.apache.hc.client5.http.io.ConnectionEndpoint.isConnected.

Determines if the connection to the remote endpoint is still open and valid.

Annotations
@Override
setSocketTimeoutback to summary
public void setSocketTimeout(final Timeout timeout)

Implements abstract org.apache.hc.client5.http.io.ConnectionEndpoint.setSocketTimeout.

Doc from org.apache.hc.client5.http.io.ConnectionEndpoint.setSocketTimeout.

Sets the socket timeout value.

Parameters
timeout:Timeout

timeout value

Annotations
@Override