public class SocketAddressResolver extends Object
SocketAddress
instances, returning them through a Promise
,
in order to avoid blocking on DNS lookup.
InetSocketAddress.InetSocketAddress(String, int)
attempts to perform a DNS resolution of
the host name, and this may block for several seconds.
This class creates the InetSocketAddress
in a separate thread and provides the result
through a Promise
, with the possibility to specify a timeout for the operation.
Example usage:
SocketAddressResolver resolver = new SocketAddressResolver(executor, scheduler); resolver.resolve("www.google.com", 80, new Promise<SocketAddress>() { public void succeeded(SocketAddress result) { // The address was resolved } public void failed(Throwable failure) { // The address resolution failed } });
Constructor and Description |
---|
SocketAddressResolver(Executor executor,
Scheduler scheduler,
long timeout)
Creates a new instance with the given executor (to perform DNS resolution in a separate thread),
the given scheduler (to cancel the operation if it takes too long) and the given timeout, in milliseconds.
|
Modifier and Type | Method and Description |
---|---|
Executor |
getExecutor() |
Scheduler |
getScheduler() |
long |
getTimeout() |
protected void |
resolve(String host,
int port,
long timeout,
Promise<SocketAddress> promise)
Resolves the given host and port, returning a
SocketAddress through the given Promise
with the given timeout. |
void |
resolve(String host,
int port,
Promise<SocketAddress> promise)
Resolves the given host and port, returning a
SocketAddress through the given Promise
with the default timeout. |
public SocketAddressResolver(Executor executor, Scheduler scheduler, long timeout)
executor
- the thread pool to use to perform DNS resolution in pooled threadsscheduler
- the scheduler to schedule tasks to cancel DNS resolution if it takes too longtimeout
- the timeout, in milliseconds, for the DNS resolution to completepublic Executor getExecutor()
public Scheduler getScheduler()
public long getTimeout()
public void resolve(String host, int port, Promise<SocketAddress> promise)
SocketAddress
through the given Promise
with the default timeout.host
- the host to resolveport
- the port of the resulting socket addresspromise
- the callback invoked when the resolution succeeds or failsresolve(String, int, long, Promise)
protected void resolve(String host, int port, long timeout, Promise<SocketAddress> promise)
SocketAddress
through the given Promise
with the given timeout.host
- the host to resolveport
- the port of the resulting socket addresstimeout
- the timeout, in milliseconds, for the DNS resolution to completepromise
- the callback invoked when the resolution succeeds or failsCopyright © 1995-2015 Webtide. All Rights Reserved.