@Test public void shouldResolveAddressAndForwardEventWhenConnectIsCalledWithAnUnresolvedInetSocketAddress() throws Exception { DefaultChannelFuture originalEventFuture = new DefaultChannelFuture(channel, false); ChannelStateEvent event = new DownstreamChannelStateEvent( channel, originalEventFuture, ChannelState.CONNECTED, createUnresolvedRemoteAddress() ); handler.connectRequested(ctx, event); ArgumentCaptor<DownstreamChannelStateEvent> eventCaptor = ArgumentCaptor.forClass(DownstreamChannelStateEvent.class); verify(ctx).sendDownstream(eventCaptor.capture()); DownstreamChannelStateEvent forwardedEvent = eventCaptor.getValue(); assertThat(forwardedEvent.getChannel(), equalTo(channel)); assertThat(forwardedEvent.getState(), equalTo(ChannelState.CONNECTED)); assertThat(forwardedEvent.getFuture(), Matchers.<ChannelFuture>equalTo(originalEventFuture)); InetSocketAddress forwardedAddress = (InetSocketAddress) forwardedEvent.getValue(); assertThat(forwardedAddress.isUnresolved(), equalTo(false)); }
@Override public void connectRequested(final ChannelHandlerContext ctx, final ChannelStateEvent e) throws Exception { if (!(e.getValue() instanceof InetSocketAddress)) { super.connectRequested(ctx, e); return; } final InetSocketAddress originalAddress = (InetSocketAddress) e.getValue(); if (!originalAddress.isUnresolved()) { super.connectRequested(ctx, e); return; } ListenableFuture<InetSocketAddress> resolvedFuture = resolveService.submit(new Callable<InetSocketAddress>() { @Override public InetSocketAddress call() throws Exception { return resolvedAddressProvider.createResolved(originalAddress); } }); Futures.addCallback(resolvedFuture, new FutureCallback<InetSocketAddress>() { @Override public void onSuccess(InetSocketAddress resolvedAddress) { try { DownstreamChannelStateEvent forwardedEvent = new DownstreamChannelStateEvent(e.getChannel(), e.getFuture(), e.getState(), resolvedAddress); AddressResolverHandler.super.connectRequested(ctx, forwardedEvent); } catch (Exception cause) { failConnect(cause); } } @Override public void onFailure(Throwable cause) { failConnect(cause); } private void failConnect(Throwable cause) { LOGGER.warn("fail connect to unresolved address:{}", originalAddress, cause); Channels.fireExceptionCaughtLater(ctx, cause); } }); }
AddressResolverHandler extends SimpleChannelDownstreamHandler { @Override public void connectRequested(final ChannelHandlerContext ctx, final ChannelStateEvent e) throws Exception { if (!(e.getValue() instanceof InetSocketAddress)) { super.connectRequested(ctx, e); return; } final InetSocketAddress originalAddress = (InetSocketAddress) e.getValue(); if (!originalAddress.isUnresolved()) { super.connectRequested(ctx, e); return; } ListenableFuture<InetSocketAddress> resolvedFuture = resolveService.submit(new Callable<InetSocketAddress>() { @Override public InetSocketAddress call() throws Exception { return resolvedAddressProvider.createResolved(originalAddress); } }); Futures.addCallback(resolvedFuture, new FutureCallback<InetSocketAddress>() { @Override public void onSuccess(InetSocketAddress resolvedAddress) { try { DownstreamChannelStateEvent forwardedEvent = new DownstreamChannelStateEvent(e.getChannel(), e.getFuture(), e.getState(), resolvedAddress); AddressResolverHandler.super.connectRequested(ctx, forwardedEvent); } catch (Exception cause) { failConnect(cause); } } @Override public void onFailure(Throwable cause) { failConnect(cause); } private void failConnect(Throwable cause) { LOGGER.warn("fail connect to unresolved address:{}", originalAddress, cause); Channels.fireExceptionCaughtLater(ctx, cause); } }); } }
AddressResolverHandler extends SimpleChannelDownstreamHandler { @Override public void connectRequested(final ChannelHandlerContext ctx, final ChannelStateEvent e) throws Exception { if (!(e.getValue() instanceof InetSocketAddress)) { super.connectRequested(ctx, e); return; } final InetSocketAddress originalAddress = (InetSocketAddress) e.getValue(); if (!originalAddress.isUnresolved()) { super.connectRequested(ctx, e); return; } ListenableFuture<InetSocketAddress> resolvedFuture = resolveService.submit(new Callable<InetSocketAddress>() { @Override public InetSocketAddress call() throws Exception { return resolvedAddressProvider.createResolved(originalAddress); } }); Futures.addCallback(resolvedFuture, new FutureCallback<InetSocketAddress>() { @Override public void onSuccess(InetSocketAddress resolvedAddress) { try { DownstreamChannelStateEvent forwardedEvent = new DownstreamChannelStateEvent(e.getChannel(), e.getFuture(), e.getState(), resolvedAddress); AddressResolverHandler.super.connectRequested(ctx, forwardedEvent); } catch (Exception cause) { failConnect(cause); } } @Override public void onFailure(Throwable cause) { failConnect(cause); } private void failConnect(Throwable cause) { LOGGER.warn("fail connect to unresolved address:{}", originalAddress, cause); Channels.fireExceptionCaughtLater(ctx, cause); } }); } AddressResolverHandler(ListeningExecutorService resolveService);   AddressResolverHandler(ListeningExecutorService resolveService, ResolvedAddressProvider resolvedAddressProvider); }
AddressResolverHandler extends SimpleChannelDownstreamHandler { @Override public void connectRequested(final ChannelHandlerContext ctx, final ChannelStateEvent e) throws Exception { if (!(e.getValue() instanceof InetSocketAddress)) { super.connectRequested(ctx, e); return; } final InetSocketAddress originalAddress = (InetSocketAddress) e.getValue(); if (!originalAddress.isUnresolved()) { super.connectRequested(ctx, e); return; } ListenableFuture<InetSocketAddress> resolvedFuture = resolveService.submit(new Callable<InetSocketAddress>() { @Override public InetSocketAddress call() throws Exception { return resolvedAddressProvider.createResolved(originalAddress); } }); Futures.addCallback(resolvedFuture, new FutureCallback<InetSocketAddress>() { @Override public void onSuccess(InetSocketAddress resolvedAddress) { try { DownstreamChannelStateEvent forwardedEvent = new DownstreamChannelStateEvent(e.getChannel(), e.getFuture(), e.getState(), resolvedAddress); AddressResolverHandler.super.connectRequested(ctx, forwardedEvent); } catch (Exception cause) { failConnect(cause); } } @Override public void onFailure(Throwable cause) { failConnect(cause); } private void failConnect(Throwable cause) { LOGGER.warn("fail connect to unresolved address:{}", originalAddress, cause); Channels.fireExceptionCaughtLater(ctx, cause); } }); } AddressResolverHandler(ListeningExecutorService resolveService);   AddressResolverHandler(ListeningExecutorService resolveService, ResolvedAddressProvider resolvedAddressProvider); @Override void connectRequested(final ChannelHandlerContext ctx, final ChannelStateEvent e); }
AddressResolverHandler extends SimpleChannelDownstreamHandler { @Override public void connectRequested(final ChannelHandlerContext ctx, final ChannelStateEvent e) throws Exception { if (!(e.getValue() instanceof InetSocketAddress)) { super.connectRequested(ctx, e); return; } final InetSocketAddress originalAddress = (InetSocketAddress) e.getValue(); if (!originalAddress.isUnresolved()) { super.connectRequested(ctx, e); return; } ListenableFuture<InetSocketAddress> resolvedFuture = resolveService.submit(new Callable<InetSocketAddress>() { @Override public InetSocketAddress call() throws Exception { return resolvedAddressProvider.createResolved(originalAddress); } }); Futures.addCallback(resolvedFuture, new FutureCallback<InetSocketAddress>() { @Override public void onSuccess(InetSocketAddress resolvedAddress) { try { DownstreamChannelStateEvent forwardedEvent = new DownstreamChannelStateEvent(e.getChannel(), e.getFuture(), e.getState(), resolvedAddress); AddressResolverHandler.super.connectRequested(ctx, forwardedEvent); } catch (Exception cause) { failConnect(cause); } } @Override public void onFailure(Throwable cause) { failConnect(cause); } private void failConnect(Throwable cause) { LOGGER.warn("fail connect to unresolved address:{}", originalAddress, cause); Channels.fireExceptionCaughtLater(ctx, cause); } }); } AddressResolverHandler(ListeningExecutorService resolveService);   AddressResolverHandler(ListeningExecutorService resolveService, ResolvedAddressProvider resolvedAddressProvider); @Override void connectRequested(final ChannelHandlerContext ctx, final ChannelStateEvent e);  }