
redis.netty4.RedisClientBase Maven / Gradle / Ivy
The newest version!
package redis.netty4;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundMessageHandlerAdapter;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import spullara.util.concurrent.Promise;
import java.net.InetSocketAddress;
import java.util.LinkedList;
import java.util.Queue;
/**
* Uses netty4 to talk to redis.
*/
public class RedisClientBase {
private final static NioEventLoopGroup group = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors());
private final SocketChannel socketChannel;
private final Queue> queue;
protected RedisClientBase(SocketChannel socketChannel, Queue> queue) {
this.socketChannel = socketChannel;
this.queue = queue;
group.register(socketChannel);
}
public static Promise connect(String host, int port) {
final Queue> queue = new LinkedList<>();
SocketChannel socketChannel = new NioSocketChannel();
final RedisClientBase client = new RedisClientBase(socketChannel, queue);
socketChannel.pipeline().addLast(new RedisCommandEncoder(), new RedisReplyDecoder(),
new ChannelInboundMessageHandlerAdapter>() {
@Override
public void messageReceived(ChannelHandlerContext channelHandlerContext, Reply> reply) throws Exception {
Promise poll;
synchronized (client) {
poll = queue.poll();
if (poll == null) {
throw new IllegalStateException("Promise queue is empty, received reply");
}
}
poll.set(reply);
}
});
final Promise promise = new Promise<>();
socketChannel.connect(new InetSocketAddress(host, port)).addListener(new ChannelFutureListenerPromiseAdapter<>(promise, client));
return promise;
}
public Promise send(Command command) {
Promise reply = new Promise<>();
synchronized (this) {
queue.add(reply);
socketChannel.write(command);
}
return reply;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy