/** * Create a client but use a pre-configured bootstrap. * This method however replaces the ChannelInitializer in the bootstrap. All other configuration is preserved. * * @param address remote address */ protected Future<S> createClient(final InetSocketAddress address, final ReconnectStrategy strategy, final Bootstrap bootstrap, final PipelineInitializer<S> initializer) { final ProtocolSessionPromise<S> p = new ProtocolSessionPromise<>(executor, address, strategy, bootstrap); bootstrap.handler( new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(final SocketChannel ch) { initializer.initializeChannel(ch, p); } }); p.connect(); LOG.debug("Client created."); return p; }
synchronized void connect() { final Object lock = this; try { final int timeout = this.strategy.getConnectTimeout(); LOG.debug("Promise {} attempting connect for {}ms", lock, timeout); if(this.address.isUnresolved()) { this.address = new InetSocketAddress(this.address.getHostName(), this.address.getPort()); } this.b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, timeout); final ChannelFuture connectFuture = this.b.connect(this.address); // Add listener that attempts reconnect by invoking this method again. connectFuture.addListener(new BootstrapConnectListener(lock)); this.pending = connectFuture; } catch (final Exception e) { LOG.info("Failed to connect to {}", address, e); setFailure(e); } }
/** * Creates a client. * * @param address remote address * @param connectStrategy Reconnection strategy to be used when initial connection fails * * @return Future representing the connection process. Its result represents the combined success of TCP connection * as well as session negotiation. */ protected Future<S> createClient(final InetSocketAddress address, final ReconnectStrategy strategy, final PipelineInitializer<S> initializer) { final Bootstrap b = new Bootstrap(); final ProtocolSessionPromise<S> p = new ProtocolSessionPromise<>(executor, address, strategy, b); b.option(ChannelOption.SO_KEEPALIVE, true).handler( new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(final SocketChannel ch) { initializer.initializeChannel(ch, p); } }); customizeBootstrap(b); setWorkerGroup(b); setChannelFactory(b); p.connect(); LOG.debug("Client created."); return p; }