private void buildInitialConnectionQ(FutureCallback<Channel> callback) { for (int i = 0; i < POOL_SIZE; i++) { ListenableFuture<Channel> result = connector.connect(); Futures.addCallback( result, new FutureCallback<Channel>() { @Override public void onSuccess(@Nullable Channel channel) { connectionQ.addLast(channel); } // TODO(CK): this error needs to get bubbled back up to the requestor @Override public void onFailure(Throwable throwable) { log.error("Error connecting to " + connector.address(), throwable); } }); Futures.addCallback(result, callback); } }
@Override public void onFailure(Throwable throwable) { log.error("Error connecting to " + connector.address(), throwable); } });
protected Bootstrap cloneBootstrap() { return buildBootstrap().clone().handler(handler()); }
private Bootstrap buildBootstrap() { Bootstrap bootstrap = new Bootstrap(); // TODO(CK): move all of these constants out into Config bootstrap .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 500) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .option( ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(8 * 1024, 32 * 1024)) .option(ChannelOption.AUTO_READ, true) .group(group()) .channel(channel()); return configure(bootstrap); }
public ListenableFuture<Channel> connect() { SettableFuture<Channel> promise = SettableFuture.create(); connectBootstrap(promise); return promise; }
protected void connectBootstrap(SettableFuture<Channel> promise) { cloneBootstrap() .connect(address) .addListener( new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) { if (future.isSuccess()) { promise.set(future.channel()); } else { promise.setException(future.cause()); } } }); }
@Override protected void initChannel(Channel channel) { ChannelPipeline pipeline = channel.pipeline(); pipeline .addLast("frame length codec", new FrameLengthCodec()) .addLast("mux message codec", new Codec()); for (Map.Entry<String, ChannelHandler> entry : payloadHandlers()) { pipeline.addLast(entry.getKey(), entry.getValue()); } pipeline.addLast("mux client codec", new ClientCodec()); } };
connectionQ.remove(xs); Futures.addCallback( connector.connect(), new FutureCallback<Channel>() { @Override
ListenableFuture<Channel> future = connector.connect();