Java Netty冻结了多个客户端连接
我正在尝试测试netty,但当我创建多个客户端来连接到服务器时,一些客户端只是冻结了,永远无法完成。 这里是我的代码(基本上我使用了她提供的代码,并对其进行了修改以使用多个客户端):Java Netty冻结了多个客户端连接,java,asynchronous,nio,netty,Java,Asynchronous,Nio,Netty,我正在尝试测试netty,但当我创建多个客户端来连接到服务器时,一些客户端只是冻结了,永远无法完成。 这里是我的代码(基本上我使用了她提供的代码,并对其进行了修改以使用多个客户端): 呼叫系统退出(-1);将终止JVM,而其他客户端线程可能仍处于活动状态。此行为是否必要?呼叫系统。退出(-1);将终止JVM,而其他客户端线程可能仍处于活动状态。此行为是否必要?代码每次都使用相同的端口号为客户端创建新线程。这可能会产生问题,因为多个线程正在同一端口上处理消息。代码每次都使用相同的端口号为客户端创建
呼叫系统退出(-1);将终止JVM,而其他客户端线程可能仍处于活动状态。此行为是否必要?呼叫系统。退出(-1);将终止JVM,而其他客户端线程可能仍处于活动状态。此行为是否必要?代码每次都使用相同的端口号为
客户端创建新线程。这可能会产生问题,因为多个线程正在同一端口上处理消息。代码每次都使用相同的端口号为客户端创建新线程。这可能会产生问题,因为多个线程在同一端口上处理消息。我想说这无关紧要。只有在客户端未启动时出现错误时,此行才会运行。而且它不运行,我会说它不重要。只有在客户端未启动时出现错误时,此行才会运行。而且它没有运行
for (int i = numthr; i > 0; i--) {
Runnable runner = new Runnable() {
public void run() {
final Client client = new Client("localhost", 10400, nummes, 0);
if (!client.start()) {
System.exit(-1);
return;
}
client.flood();
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
client.stop();
}
});
}
};
executor.execute(runner);
}
public void messageReceived(Envelope message) {
if (this.received.incrementAndGet() == this.messages) {
System.out.println(nmb.incrementAndGet());
}
}
public boolean start() {
// For production scenarios, use limited sized thread pools
this.clientFactory = new NioClientSocketChannelFactory(Executors.newCachedThreadPool(),
Executors.newCachedThreadPool(),1);
this.channelGroup = new DefaultChannelGroup(this + "-channelGroup");
this.handler = new ClientHandler(this, this.channelGroup);
ChannelPipelineFactory pipelineFactory = new ChannelPipelineFactory() {
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("encoder", new Encoder());
pipeline.addLast("decoder", new Decoder());
pipeline.addLast("handler", handler);
return pipeline;
}
};
ClientBootstrap bootstrap = new ClientBootstrap(this.clientFactory);
bootstrap.setOption("reuseAddress", true);
bootstrap.setOption("tcpNoDelay", true);
bootstrap.setOption("keepAlive", true);
bootstrap.setPipelineFactory(pipelineFactory);
boolean connected = bootstrap.connect(new InetSocketAddress(host, port)).awaitUninterruptibly().isSuccess();
if (!connected) {
this.stop();
}
return connected;
}
this.serverFactory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(),
Executors.newCachedThreadPool());
this.channelGroup = new DefaultChannelGroup(this + "-channelGroup");
ExecutionHandler executionHandler = new ExecutionHandler(
new MemoryAwareThreadPoolExecutor(270, 1048576, 1048576));
ServerBootstrap bootstrap = new ServerBootstrap(this.serverFactory);
bootstrap.setPipelineFactory(
new DatabaseGatewayPipelineFactory(executionHandler));
bootstrap.setOption("reuseAddress", true);
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.keepAlive", true);
bootstrap.setOption("child.connectTimeoutMillis", 10000);
Channel channel = bootstrap.bind(new InetSocketAddress(this.host, this.port));
if (!channel.isBound()) {
this.stop();
return false;
}
this.channelGroup.add(channel);