我需要开发一个返回频道的Netty连接池,这样我就可以避免每次需要时创建频道。客户端应该通过Http每秒发出几个请求,并从一组10个线程执行。 目前我已经创建了一个Netty Channel Pool,它可以解决这个机制,但它似乎并没有连接。
(虽然它会产生频道,但频道无法在网络上成功'写'。
//Code follows
//get Channel method
public Channel getChannel() throws Exception
{
synchronized (_channels) {
PoolChannel pc = null;
for (int i = 0; i < _channels.size(); i++) {
pc = _channels.get(i);
if (pc.lease()) {
// PoolConnection is available
if (!_checkChannels) {
return pc;
}
else {
// Check the status of the connection
boolean isHealthy = true;
try {
if (!pc.isOpen()) {
// If something wrong with the channel
// then don't use it anymore.
isHealthy = false;
}
}
catch(Exception ex) {
// If we can't even ask for that information, we
// certainly don't want to use it anymore.
isHealthy = false;
}
if (isHealthy) {
return pc;
}
else {
try {
pc.expire();
}
catch(Exception e) {
// ignore
}
_channels.remove(i);
}
}
}
}
}
// Create a new Connection
PoolChannel pc=null;
URI uri = _uri;
String host = _host;
int port = _port;
//create channel
Executor bossPool = Executors.newCachedThreadPool();
Executor workerPool = Executors.newCachedThreadPool();
ClientBootstrap bstrap = new ClientBootstrap(
new NioClientSocketChannelFactory(
bossPool, workerPool));
bstrap.setPipelineFactory(new HttpClientPipelineFactory());
ChannelFuture future = bstrap.connect(new InetSocketAddress(host, port));
Channel chann = future.awaitUninterruptibly().getChannel();
if (future.isSuccess()) {
System.out.println("Channel success");
}
System.out.println("Channel = "+chann);
pc = new PoolChannel(chann);
pc.lease();
// Add it to the pool
synchronized (_channels) {
_channels.add(pc);
if (_cleaner == null) {
// Put a new PoolCleaner
_cleaner = new PoolCleaner(_cleaningInterval);
_cleaner.start();
}
}
return pc;
}
//===========================
请问我可以获得帮助吗? 此外,当我完成频道时,如何将此频道返回到频道? 关闭频道是否足够? 提前谢谢。