group.register(socketChannel);
}
public static Promise<RedisClientBase> connect(String host, int port) {
final Queue<Promise<Reply>> queue = new LinkedList<>();
SocketChannel socketChannel = new NioSocketChannel();
final RedisClientBase client = new RedisClientBase(socketChannel, queue);
socketChannel.pipeline().addLast(new RedisCommandEncoder(), new RedisReplyDecoder(),
new SimpleChannelInboundHandler<Reply<?>>() {
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, Reply<?> reply) throws Exception {
Promise<Reply> poll;
synchronized (client) {
poll = queue.poll();
if (poll == null) {
throw new IllegalStateException("Promise queue is empty, received reply");
}
}
poll.set(reply);
}
});
final Promise<RedisClientBase> promise = new Promise<>();
socketChannel.connect(new InetSocketAddress(host, port)).addListener(new ChannelFutureListenerPromiseAdapter<>(promise, client));
return promise;
}