A few questions about netty ChannelPoolMap
I tried to use netty as client for db with key, glad to find netty 4.0.28.Final to provide its own connection pool.
I have a simple connection pool like the following (as per Netty 4.0.28. Final note ):
QdbConnectionPool:
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");
}
}
QdbPoolHandler with decoder for returned content:
public class QdbPoolHandler extends AbstractChannelPoolHandler {
@Override
public void channelCreated(Channel ch) throws Exception{
ch.pipeline().addLast(new QdbDecoder());
}
}
This is what I understand and my questions
1) when the pool .acquire () is called, the pool will connect
2) after Future is aqcuired, I add QdbClientHanlder () to handle sending and receiving messages (QdbClientHandler () will be removed in its own channelRead () method), am I using it correctly?
3) I am running a request via channel.pipeline (). fireChannelActive (), am I using it correctly? Are there other ways to dismiss the request? Pipeline () pipe. FireChannelActive () re-runs ChannelPipeline, can I get a future from it?
4) I am using CowntDownLatch to make sure the request is complete (the countDownLatch countdown () method will be called in QdbClientHanlder's channelRead () or exceptionCaught () method), am I using it correctly? Are there any other ways to make sure the request is complete?
source to share
No one has answered this question yet
Check out similar questions: