我在测试netty服务中保存接收到的消息,一直处于线程BLOCKED的状态,无法持久化到数据库。服务端如下,客户端继承SimpleChannelInboundHandler,在channelRead0中数据持久化一直处于死锁状态。
public void start() throws Exception {
LOGGER.info("启动服务!");
// 多线程事件循环器,用来接收进来的连接
bossGroup = new NioEventLoopGroup();
// 用来处理接收的线程
workerGroup = new NioEventLoopGroup();
// 用来处理业务线程
group = new DefaultEventExecutorGroup(16);
try {
// 当前线程使用数
// workerGroup非阻塞模式
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
// 注册handler
ch.pipeline().addLast(new MessageDecoder(MessageRequest.class));
ch.pipeline().addLast(new MessageEncoder(MessageResponse.class));
ch.pipeline().addLast(group,new BusinessHandler());
}
}).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = b.bind(host, port).sync();
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
如果手动注入的bean中有多线程启动的情况,最好还是使用depends-on依次进行bean的实例化。
这个得看你的handler代码。