Netty 连接池未向服务器发送消息



我有一个简单的netty连接池和一个简单的HTTP端点,使用该池将TCP消息发送到ServerSocket。相关代码如下所示,客户端(NettyConnectionPoolClientApplication(为:

@SpringBootApplication
@RestController
public class NettyConnectionPoolClientApplication {
private SimpleChannelPool simpleChannelPool;
public static void main(String[] args) {
SpringApplication.run(NettyConnectionPoolClientApplication.class, args);
}
@PostConstruct
public void setup() throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group);
bootstrap.channel(NioSocketChannel.class);
bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
bootstrap.remoteAddress(new InetSocketAddress("localhost", 9000));
bootstrap.handler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
pipeline.addLast(new StringDecoder());
pipeline.addLast(new StringEncoder());
pipeline.addLast(new DummyClientHandler());
}
});
simpleChannelPool = new SimpleChannelPool(bootstrap, new DummyChannelPoolHandler());
}
@RequestMapping("/test/{msg}")
public void test(@PathVariable String msg) throws Exception {
Future<Channel> future = simpleChannelPool.acquire();
future.addListener((FutureListener<Channel>) f -> {
if (f.isSuccess()) {
System.out.println("Connected");
Channel ch = f.getNow();
ch.writeAndFlush(msg + System.lineSeparator());
// Release back to pool
simpleChannelPool.release(ch);
} else {
System.out.println("not successful");
}
});
}
}

和服务器(ServerSocketRunner(

public class ServerSocketRunner {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(9000);
while (true) {
Socket socket = serverSocket.accept();
new Thread(() -> {
System.out.println("New client connected");
try (PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));) {
String inputLine, outputLine;
out.println("Hello client!");
do {
inputLine = in.readLine();
System.out.println("Received: " + inputLine);
} while (!"bye".equals(inputLine));
System.out.println("Closing connection...");
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
}
}

DummyChannelPoolHandler 和 DummyClientHandler 只是打印出发生的事件,因此它们不相关。当服务器和客户端启动并且我向测试端点发送测试消息时,我可以看到服务器打印"新客户端已连接",但客户端发送的消息未打印。服务器不会打印客户端发送的任何连续消息。

如果我尝试telnet,一切正常,服务器会打印出消息。此外,它适用于具有相同引导配置且没有连接池(SimpleNettyClientApplication(的常规netty客户端。

任何人都可以看到我的连接池出了什么问题,我的想法

Netty versioin:4.1.39.Final

所有代码都在这里找到。

更新

遵循诺曼·毛雷尔的建议。我添加了

ChannelFuture channelFuture = ch
.writeAndFlush(msg + System.lineSeparator());
channelFuture.addListener(writeFuture -> {
System.out
.println("isSuccess(): " + channelFuture.isSuccess() + " : " + channelFuture.cause());
});

这打印出来

isSuccess: false : java.lang.UnsupportedOperationException: unsupported message type: String (expected: ByteBuf, FileRegion)

为了修复它,我只是将String转换为ByteBuf

ch.writeAndFlush(Unpooled.wrappedBuffer((msg + System.lineSeparator()).getBytes()));

您应该检查writeAndFlush(...)返回的ChannelFuture的状态是什么。我怀疑它失败了。

最新更新