我决定找出我的笔记本可以从多个客户端获得多少msg /秒。 我已经通过下一个简单的方式从示例中更改了Echo客户端/服务器: 1)在客户端,我在channelActive()方法中有无限循环。循环发送消息。 2)在服务器端,我有处理传入消息的channelRead()方法
当我运行我的客户端2次(在2个separet线程中)时,我希望看到服务器如何处理2个线程中的客户端。 相反,服务器只处理1个客户端连接,有时根本不处理任何连接。 我查看了我的客户端线程,发现他们无法退出ChannelOutboundBuffer.addFlush()方法中的while循环。 我无法理解我做错了什么。我使用netty 4.0.21
EchoClient.java
public static void main(String[] args) throws Exception {
// Configure the client.
final EventLoopGroup group = new NioEventLoopGroup();
Runnable r = new Runnable() {
@Override
public void run() {
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(new EchoClientHandler());
}
});
// Start the client.
ChannelFuture f = b.connect(HOST, PORT).sync();
// Wait until the connection is closed.
f.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// Shut down the event loop to terminate all threads.
group.shutdownGracefully();
}
}
};
for (int i = 0; i < 2; i++) {
Thread t = new Thread(r, i + " lalala");
t.start();
}
}
EchoClientHandler.java
public class EchoClientHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) {
while (true) {
ByteBuf time = ctx.alloc().buffer(4);
time.writeInt(number);
ctx.writeAndFlush(time);
ctx.flush();
}
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.flush();
ctx.fireChannelReadComplete();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
// Close the connection when an exception is raised.
cause.printStackTrace();
ctx.close();
ctx.fireExceptionCaught(cause);
}
}
EchoServer.java
public final class EchoServer {
public static void main(String[] args) throws Exception {
// Configure the server.
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(new EchoServerHandler());
}
});
// Start the server.
ChannelFuture f = b.bind(8007).sync();
// Wait until the server socket is closed.
f.channel().closeFuture().sync();
} finally {
// Shut down all event loops to terminate all threads.
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
EchoServerHandler.java
@Sharable
公共类EchoServerHandler扩展了ChannelInboundHandlerAdapter {
long max_msg = 10000;
long cur_msg = 0;
long startTime = System.nanoTime();
final int NANOS_IN_SEC = 1000000000;
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
ReferenceCountUtil.release(msg);
++cur_msg;
if (cur_msg == max_msg) {
System.out.println("Throughput (msg/sec) : " + max_msg * NANOS_IN_SEC / (System.nanoTime() - startTime));
cur_msg = 0;
startTime = System.nanoTime();
}
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.flush();
ctx.fireChannelReadComplete();
}
}
答案 0 :(得分:1)
你的channelActive(...)方法不能有无限循环,因为这将阻止用于多个连接的EventLoop。这样,您将基本上阻止使用此EventLoop的所有Channel的所有事件处理。