Class DistributedProxySelector
java.lang.Object
java.net.ProxySelector
org.apache.hc.client5.http.impl.routing.DistributedProxySelector
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
FieldsModifier and TypeFieldDescriptionprivate final ThreadLocal<ProxySelector> AThreadLocalvariable holding the currentProxySelectorfor each thread, ensuring thread safety when accessing the currentProxySelector.private static final org.slf4j.Loggerprivate final List<ProxySelector> A list ofProxySelectorinstances to be used by the DistributedProxySelector for selecting proxies.private final AtomicIntegerAnAtomicIntegerrepresenting the shared index across all threads for maintaining the current position in the list of ProxySelectors, ensuring proper distribution ofProxySelectorusage. -
Constructor Summary
ConstructorsConstructorDescriptionDistributedProxySelector(List<ProxySelector> selectors) Constructs a DistributedProxySelector with the given list ofProxySelector. -
Method Summary
Modifier and TypeMethodDescriptionvoidconnectFailed(URI uri, SocketAddress sa, IOException ioe) Notifies the activeProxySelectorof a connection failure.private ProxySelectorRetrieves the next availableProxySelectorin the list of selectors, incrementing the shared index atomically to ensure proper distribution across different threads.Selects a list of proxies for the givenURIby delegating to the currentProxySelectoror the next availableProxySelectorin the list if the current one returns an empty proxy list.Methods inherited from class java.net.ProxySelector
getDefault, setDefault
-
Field Details
-
LOG
private static final org.slf4j.Logger LOG -
selectors
A list ofProxySelectorinstances to be used by the DistributedProxySelector for selecting proxies. -
currentSelector
AThreadLocalvariable holding the currentProxySelectorfor each thread, ensuring thread safety when accessing the currentProxySelector.
-
-
Constructor Details
-
DistributedProxySelector
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:
IllegalArgumentException- if the list is null or empty.
-
-
Method Details
-
select
Selects a list of proxies for the givenURIby delegating to the currentProxySelectoror the next availableProxySelectorin the list if the current one returns an empty proxy list. If anExceptionoccurs, it will be caught and ignored, and the nextProxySelectorwill be tried.- Specified by:
selectin classProxySelector- Parameters:
uri- theURIto select a proxy for.- Returns:
- a list of proxies for the given
URI.
-
connectFailed
Notifies the activeProxySelectorof a connection failure. This method retrieves the currentProxySelectorfrom theThreadLocalvariable 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 theThreadLocalvariable.- Specified by:
connectFailedin classProxySelector- Parameters:
uri- theURIthat failed to connect.sa- theSocketAddressof the proxy that failed to connect.ioe- theIOExceptionthat resulted from the failed connection.
-
nextSelector
Retrieves the next availableProxySelectorin the list of selectors, incrementing the shared index atomically to ensure proper distribution across different threads.- Returns:
- the next
ProxySelectorin the list.
-