关于netty';s通道池图
我试图使用netty作为键值数据库的客户端,很高兴发现netty 4.0.28.Final提供了一个本机连接池 我有一个简单的连接池,如下所示(根据: QDB连接池:关于netty';s通道池图,netty,Netty,我试图使用netty作为键值数据库的客户端,很高兴发现netty 4.0.28.Final提供了一个本机连接池 我有一个简单的连接池,如下所示(根据: QDB连接池: public class QdbConnectionPool { private ChannelPoolMap<InetSocketAddress,FixedChannelPool> poolMap; public void init(){ EventLoopGroup group = new NioEve
public class QdbConnectionPool {
private ChannelPoolMap<InetSocketAddress,FixedChannelPool> poolMap;
public void init(){
EventLoopGroup group = new NioEventLoopGroup();
final Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group).channel(NioSocketChannel.class)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS,10000);
poolMap = new AbstractChannelPoolMap<InetSocketAddress, FixedChannelPool>() {
@Override
protected FixedChannelPool newPool(InetSocketAddress key) {
bootstrap.remoteAddress(key);
return new FixedChannelPool(bootstrap,new QdbPoolHandler(),100);
}
};
}
public QdbResult query(InetSocketAddress address,String bkey){
final QdbResult result = new QdbResult(bkey);
final CountDownLatch countDownLatch = new CountDownLatch(1);
final FixedChannelPool pool = poolMap.get(address);
Future<Channel> future = pool.acquire();
future.addListener(new FutureListener<Channel>() {
@Override
public void operationComplete(Future<Channel> future) {
if (future.isSuccess()) {
Channel ch = future.getNow();
System.out.println(ch.toString());
ch.pipeline().addLast(new QdbClientHandler(result, countDownLatch));
ch.pipeline().fireChannelActive();
} else {
System.out.println("future not succ");
}
}
});
try{
countDownLatch.await();
}catch (InterruptedException ex){
}
pool.release(future.getNow());
return result;
}
public static void main(String[] args) throws Exception{
InetSocketAddress address = new InetSocketAddress("127.0.0.1", 8888);
QdbConnectionPool pool = new QdbConnectionPool();
pool.init();
QdbResult result = pool.query(address, "xxxxxx");
}
}
以下是我的理解和我的问题
1) 调用pool.acquire()时,池将进行连接
2) 在对未来进行aqcuired后,我添加QdbClientHandler()来处理消息发送和接收(QdbClientHandler()将在其自己的channelRead()方法中删除),我是否正确使用了它
3) 我通过channel.pipeline().fireChannelActive()激发请求,我是否正确使用它?有没有其他方法可以触发请求?channel.pipeline().fireChannelActive()打开ChannelPipeline,我能从中获得未来吗
4) 我使用CowntDownLatch来确保请求完成(countDownLatch的countdown()方法将在QDBClientHander的channelRead()或exceptionCaught()方法中调用),我是否正确使用了它?是否有其他方法确保请求完成
public class QdbPoolHandler extends AbstractChannelPoolHandler {
@Override
public void channelCreated(Channel ch) throws Exception{
ch.pipeline().addLast(new QdbDecoder());
}