@Contract(threading=SAFE) public class DistributedProxySelector extends ProxySelector
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.
Constructor and Description |
---|
DistributedProxySelector(List<ProxySelector> selectors)
Constructs a DistributedProxySelector with the given list of
ProxySelector . |
Modifier and Type | Method and Description |
---|---|
void |
connectFailed(URI uri,
SocketAddress sa,
IOException ioe)
Notifies the active
ProxySelector of a connection failure. |
List<Proxy> |
select(URI uri)
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. |
getDefault, setDefault
public DistributedProxySelector(List<ProxySelector> selectors)
ProxySelector
.
The constructor initializes the currentSelector as a ThreadLocal
, and
the sharedIndex as an AtomicInteger
.selectors
- the list of ProxySelectors to use.IllegalArgumentException
- if the list is null or empty.public List<Proxy> select(URI uri)
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.select
in class ProxySelector
uri
- the URI
to select a proxy for.URI
.public void connectFailed(URI uri, SocketAddress sa, IOException ioe)
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.connectFailed
in class ProxySelector
uri
- the URI
that failed to connect.sa
- the SocketAddress
of the proxy that failed to connect.ioe
- the IOException
that resulted from the failed connection.Copyright © 1999–2021 The Apache Software Foundation. All rights reserved.