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

public Class DistributedProxySelector

extends ProxySelector
Class Inheritance
Annotations
@Contract
threading:SAFE
Imports
org.apache.hc.core5.annotation.Contract, .ThreadingBehavior, org.slf4j.Logger, .LoggerFactory, java.io.IOException, java.net.Proxy, .ProxySelector, .SocketAddress, .URI, java.util.ArrayList, .Collections, .List, java.util.concurrent.atomic.AtomicInteger

A DistributedProxySelector is a custom ProxySelector implementation that delegates proxy selection to a list of underlying ProxySelectors in a distributed manner. It ensures that proxy selection is load-balanced across the available ProxySelectors, and provides thread safety by maintaining separate states for each thread.

The DistributedProxySelector class maintains a list of ProxySelectors, a ThreadLocal variable for the current ProxySelector, and an AtomicInteger to keep track of the shared index across all threads. When the select() method is called, it delegates the proxy selection to the current ProxySelector or the next available one in the list if the current one returns an empty proxy list. Any exceptions that occur during proxy selection are caught and ignored, and the next ProxySelector is tried.

The connectFailed() method notifies the active ProxySelector of a connection failure, allowing the underlying ProxySelector to handle connection failures according to its own logic.

Since
5.3

Field Summary

Modifier and TypeField and Description
private final ThreadLocal<ProxySelector>
currentSelector

A ThreadLocal variable holding the current ProxySelector for each thread, ensuring thread safety when accessing the current ProxySelector.

private static final Logger
private final List<ProxySelector>
selectors

A list of ProxySelector instances to be used by the DistributedProxySelector for selecting proxies.

private final AtomicInteger
sharedIndex

An AtomicInteger representing the shared index across all threads for maintaining the current position in the list of ProxySelectors, ensuring proper distribution of ProxySelector usage.

Constructor Summary

AccessConstructor and Description
public
DistributedProxySelector(final List<ProxySelector>
the list of ProxySelectors to use.
selectors
)

Constructs a DistributedProxySelector with the given list of ProxySelector.

Method Summary

Modifier and TypeMethod and Description
public void
connectFailed(final URI
the URI that failed to connect.
uri
,
final SocketAddress
the SocketAddress of the proxy that failed to connect.
sa
,
final IOException
the IOException that resulted from the failed connection.
ioe
)

Implements abstract java.net.ProxySelector.connectFailed.

Notifies the active ProxySelector of a connection failure.
private ProxySelector

Returns:

the next ProxySelector in the list.
nextSelector
()

Retrieves the next available ProxySelector in the list of selectors, incrementing the shared index atomically to ensure proper distribution across different threads.

public List<Proxy>

Returns:

a list of proxies for the given URI.
select
(final URI
the URI to select a proxy for.
uri
)

Implements abstract java.net.ProxySelector.select.

Selects a list of proxies for the given URI by delegating to the current ProxySelector or the next available ProxySelector in the list if the current one returns an empty proxy list.
Inherited from java.net.ProxySelector:
getDefaultofsetDefault

Field Detail

currentSelectorback to summary
private final ThreadLocal<ProxySelector> currentSelector

A ThreadLocal variable holding the current ProxySelector for each thread, ensuring thread safety when accessing the current ProxySelector.

LOGback to summary
private static final Logger LOG
selectorsback to summary
private final List<ProxySelector> selectors

A list of ProxySelector instances to be used by the DistributedProxySelector for selecting proxies.

sharedIndexback to summary
private final AtomicInteger sharedIndex

An AtomicInteger representing the shared index across all threads for maintaining the current position in the list of ProxySelectors, ensuring proper distribution of ProxySelector usage.

Constructor Detail

DistributedProxySelectorback to summary
public DistributedProxySelector(final List<ProxySelector> selectors)

Constructs a DistributedProxySelector with the given list of ProxySelector. The constructor initializes the currentSelector as a ThreadLocal, and the sharedIndex as an AtomicInteger.

Parameters
selectors:List<ProxySelector>

the list of ProxySelectors to use.

Exceptions
IllegalArgumentException:
if the list is null or empty.

Method Detail

connectFailedback to summary
public void connectFailed(final URI uri, final SocketAddress sa, final IOException ioe)

Implements abstract java.net.ProxySelector.connectFailed.

Notifies the active ProxySelector of a connection failure. This method retrieves the current ProxySelector from the ThreadLocal variable and delegates the handling of the connection failure to the underlying ProxySelector's connectFailed() method. After handling the connection failure, the current ProxySelector is removed from the ThreadLocal variable.

Parameters
uri:URI

the URI that failed to connect.

sa:SocketAddress

the SocketAddress of the proxy that failed to connect.

ioe:IOException

the IOException that resulted from the failed connection.

Annotations
@Override
nextSelectorback to summary
private ProxySelector nextSelector()

Retrieves the next available ProxySelector in the list of selectors, incrementing the shared index atomically to ensure proper distribution across different threads.

Returns:ProxySelector

the next ProxySelector in the list.

selectback to summary
public List<Proxy> select(final URI uri)

Implements abstract java.net.ProxySelector.select.

Selects a list of proxies for the given URI by delegating to the current ProxySelector or the next available ProxySelector in the list if the current one returns an empty proxy list. If an Exception occurs, it will be caught and ignored, and the next ProxySelector will be tried.

Parameters
uri:URI

the URI to select a proxy for.

Returns:List<Proxy>

a list of proxies for the given URI.

Annotations
@Override