use of io.netty.resolver.AddressResolver in project redisson by redisson.
the class RedisClient method resolveAddr.
public CompletableFuture<InetSocketAddress> resolveAddr() {
if (resolvedAddrFuture.get() != null) {
return resolvedAddrFuture.get();
}
CompletableFuture<InetSocketAddress> promise = new CompletableFuture<>();
if (!resolvedAddrFuture.compareAndSet(null, promise)) {
return resolvedAddrFuture.get();
}
byte[] addr = NetUtil.createByteArrayFromIpAddressString(uri.getHost());
if (addr != null) {
try {
resolvedAddr = new InetSocketAddress(InetAddress.getByAddress(uri.getHost(), addr), uri.getPort());
} catch (UnknownHostException e) {
// skip
}
promise.complete(resolvedAddr);
return promise;
}
AddressResolver<InetSocketAddress> resolver = (AddressResolver<InetSocketAddress>) bootstrap.config().resolver().getResolver(bootstrap.config().group().next());
Future<InetSocketAddress> resolveFuture = resolver.resolve(InetSocketAddress.createUnresolved(uri.getHost(), uri.getPort()));
resolveFuture.addListener((FutureListener<InetSocketAddress>) future -> {
if (!future.isSuccess()) {
promise.completeExceptionally(future.cause());
return;
}
InetSocketAddress resolved = future.getNow();
byte[] addr1 = resolved.getAddress().getAddress();
resolvedAddr = new InetSocketAddress(InetAddress.getByAddress(uri.getHost(), addr1), resolved.getPort());
promise.complete(resolvedAddr);
});
return promise;
}
use of io.netty.resolver.AddressResolver in project redisson by redisson.
the class MasterSlaveConnectionManager method resolveIP.
protected CompletableFuture<RedisURI> resolveIP(String scheme, RedisURI address) {
if (address.isIP()) {
RedisURI addr = applyNatMap(address);
return CompletableFuture.completedFuture(addr);
}
CompletableFuture<RedisURI> result = new CompletableFuture<>();
AddressResolver<InetSocketAddress> resolver = resolverGroup.getResolver(getGroup().next());
InetSocketAddress addr = InetSocketAddress.createUnresolved(address.getHost(), address.getPort());
Future<InetSocketAddress> future = resolver.resolve(addr);
future.addListener((FutureListener<InetSocketAddress>) f -> {
if (!f.isSuccess()) {
log.error("Unable to resolve " + address, f.cause());
result.completeExceptionally(f.cause());
return;
}
InetSocketAddress s = f.getNow();
RedisURI uri = toURI(scheme, s.getAddress().getHostAddress(), "" + address.getPort());
result.complete(uri);
});
return result;
}
use of io.netty.resolver.AddressResolver in project netty by netty.
the class BootstrapTest method testGetResolverFailed.
@Test
public void testGetResolverFailed() throws Exception {
class TestException extends RuntimeException {
}
final Bootstrap bootstrapA = new Bootstrap();
bootstrapA.group(groupA);
bootstrapA.channel(LocalChannel.class);
bootstrapA.resolver(new AddressResolverGroup<SocketAddress>() {
@Override
protected AddressResolver<SocketAddress> newResolver(EventExecutor executor) {
throw new TestException();
}
});
bootstrapA.handler(dummyHandler);
final ServerBootstrap bootstrapB = new ServerBootstrap();
bootstrapB.group(groupB);
bootstrapB.channel(LocalServerChannel.class);
bootstrapB.childHandler(dummyHandler);
SocketAddress localAddress = bootstrapB.bind(LocalAddress.ANY).sync().channel().localAddress();
// Connect to the server using the asynchronous resolver.
ChannelFuture connectFuture = bootstrapA.connect(localAddress);
// Should fail with the IllegalStateException.
assertThat(connectFuture.await(10000), is(true));
assertThat(connectFuture.cause(), instanceOf(IllegalStateException.class));
assertThat(connectFuture.cause().getCause(), instanceOf(TestException.class));
assertThat(connectFuture.channel().isOpen(), is(false));
}
Aggregations