Class DistributedProxySelector
- java.lang.Object
-
- java.net.ProxySelector
-
- org.apache.hc.client5.http.impl.routing.DistributedProxySelector
-
@Contract(threading=SAFE) public class DistributedProxySelector extends java.net.ProxySelector
A DistributedProxySelector is a customProxySelector
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 currentProxySelector
, and anAtomicInteger
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
Fields Modifier and Type Field Description private java.lang.ThreadLocal<java.net.ProxySelector>
currentSelector
AThreadLocal
variable holding the currentProxySelector
for each thread, ensuring thread safety when accessing the currentProxySelector
.private static org.slf4j.Logger
LOG
private java.util.List<java.net.ProxySelector>
selectors
A list ofProxySelector
instances to be used by the DistributedProxySelector for selecting proxies.private java.util.concurrent.atomic.AtomicInteger
sharedIndex
AnAtomicInteger
representing the shared index across all threads for maintaining the current position in the list of ProxySelectors, ensuring proper distribution ofProxySelector
usage.
-
Constructor Summary
Constructors Constructor Description DistributedProxySelector(java.util.List<java.net.ProxySelector> selectors)
Constructs a DistributedProxySelector with the given list ofProxySelector
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
connectFailed(java.net.URI uri, java.net.SocketAddress sa, java.io.IOException ioe)
Notifies the activeProxySelector
of a connection failure.private java.net.ProxySelector
nextSelector()
Retrieves the next availableProxySelector
in the list of selectors, incrementing the shared index atomically to ensure proper distribution across different threads.java.util.List<java.net.Proxy>
select(java.net.URI uri)
Selects a list of proxies for the givenURI
by delegating to the currentProxySelector
or the next availableProxySelector
in the list if the current one returns an empty proxy list.
-
-
-
Field Detail
-
LOG
private static final org.slf4j.Logger LOG
-
selectors
private final java.util.List<java.net.ProxySelector> selectors
A list ofProxySelector
instances to be used by the DistributedProxySelector for selecting proxies.
-
currentSelector
private final java.lang.ThreadLocal<java.net.ProxySelector> currentSelector
AThreadLocal
variable holding the currentProxySelector
for each thread, ensuring thread safety when accessing the currentProxySelector
.
-
sharedIndex
private final java.util.concurrent.atomic.AtomicInteger sharedIndex
AnAtomicInteger
representing the shared index across all threads for maintaining the current position in the list of ProxySelectors, ensuring proper distribution ofProxySelector
usage.
-
-
Constructor Detail
-
DistributedProxySelector
public DistributedProxySelector(java.util.List<java.net.ProxySelector> selectors)
Constructs a DistributedProxySelector with the given list ofProxySelector
. The constructor initializes the currentSelector as aThreadLocal
, and the sharedIndex as anAtomicInteger
.- Parameters:
selectors
- the list of ProxySelectors to use.- Throws:
java.lang.IllegalArgumentException
- if the list is null or empty.
-
-
Method Detail
-
select
public java.util.List<java.net.Proxy> select(java.net.URI uri)
Selects a list of proxies for the givenURI
by delegating to the currentProxySelector
or the next availableProxySelector
in the list if the current one returns an empty proxy list. If anException
occurs, it will be caught and ignored, and the nextProxySelector
will be tried.- Specified by:
select
in classjava.net.ProxySelector
- Parameters:
uri
- theURI
to select a proxy for.- Returns:
- a list of proxies for the given
URI
.
-
connectFailed
public void connectFailed(java.net.URI uri, java.net.SocketAddress sa, java.io.IOException ioe)
Notifies the activeProxySelector
of a connection failure. This method retrieves the currentProxySelector
from theThreadLocal
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 theThreadLocal
variable.- Specified by:
connectFailed
in classjava.net.ProxySelector
- Parameters:
uri
- theURI
that failed to connect.sa
- theSocketAddress
of the proxy that failed to connect.ioe
- theIOException
that resulted from the failed connection.
-
nextSelector
private java.net.ProxySelector nextSelector()
Retrieves the next availableProxySelector
in the list of selectors, incrementing the shared index atomically to ensure proper distribution across different threads.- Returns:
- the next
ProxySelector
in the list.
-
-