是否在 Channel 之间共享 DefaultEventExecutor
Is DefaultEventExecutor shared between Channels
在下面的代码中,我引入了一个 DefaultEventExecutor,在其中完成了实际的数据库插入。我怀疑这是否真的是正确的方法,因为 DefaultEventExecutors 和 Channels 一样多。有人可以帮我解决这个问题吗?
public class SocketChannelInitializer extends ChannelInitializer<SocketChannel> {
final static Logger logger = Logger.getLogger("com.my.snapshot");
public ComboPooledDataSource cpds;
public SocketChannelInitializer(ComboPooledDataSource cpds) {
this.cpds = cpds;
}
@Override
public void initChannel(SocketChannel ch) throws Exception {
if (logger.isDebugEnabled()) {
logger.debug("New SocketChannel connection from " + ch.remoteAddress());
}
ChannelPipeline pipeline = ch.pipeline();
EventExecutor e1 = new DefaultEventExecutor();
// and then business logic.
pipeline.addLast("readTimeoutHandler", new ReadTimeoutHandler(200));
pipeline.addLast("decoder", new MyPacketHeaderDecoder());
/**
* Inserting in DB happens in single Thread (e1)
*
*/
pipeline.addLast(e1, "logic", new PacketDatabaseHandler(cpds));
}
}
您应该创建一个 DefaultEventExecutorGroup 实例并在您的 initChannel(....) 方法中使用相同的实例。否则它没有意义,因为你最终会得到与 Channel.s
一样多的线程
在下面的代码中,我引入了一个 DefaultEventExecutor,在其中完成了实际的数据库插入。我怀疑这是否真的是正确的方法,因为 DefaultEventExecutors 和 Channels 一样多。有人可以帮我解决这个问题吗?
public class SocketChannelInitializer extends ChannelInitializer<SocketChannel> {
final static Logger logger = Logger.getLogger("com.my.snapshot");
public ComboPooledDataSource cpds;
public SocketChannelInitializer(ComboPooledDataSource cpds) {
this.cpds = cpds;
}
@Override
public void initChannel(SocketChannel ch) throws Exception {
if (logger.isDebugEnabled()) {
logger.debug("New SocketChannel connection from " + ch.remoteAddress());
}
ChannelPipeline pipeline = ch.pipeline();
EventExecutor e1 = new DefaultEventExecutor();
// and then business logic.
pipeline.addLast("readTimeoutHandler", new ReadTimeoutHandler(200));
pipeline.addLast("decoder", new MyPacketHeaderDecoder());
/**
* Inserting in DB happens in single Thread (e1)
*
*/
pipeline.addLast(e1, "logic", new PacketDatabaseHandler(cpds));
}
}
您应该创建一个 DefaultEventExecutorGroup 实例并在您的 initChannel(....) 方法中使用相同的实例。否则它没有意义,因为你最终会得到与 Channel.s
一样多的线程