Java 关闭Netty服务器本身
我使用Netty服务器来解决这个问题:逐行读取一个大文件并进行处理。在一台机器上执行仍然很慢,所以我决定使用服务器为客户机提供大块的数据。这已经起作用了,但我还希望服务器在处理整个文件时自动关闭。我现在使用的源代码是:Java 关闭Netty服务器本身,java,netty,Java,Netty,我使用Netty服务器来解决这个问题:逐行读取一个大文件并进行处理。在一台机器上执行仍然很慢,所以我决定使用服务器为客户机提供大块的数据。这已经起作用了,但我还希望服务器在处理整个文件时自动关闭。我现在使用的源代码是: public static void main(String[] args) { new Thread(() -> { //reading the big file and populating 'dq' - data queue }).st
public static void main(String[] args) {
new Thread(() -> {
//reading the big file and populating 'dq' - data queue
}).start();
final EventLoopGroup bGrp = new NioEventLoopGroup(1);
final EventLoopGroup wGrp = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) {
ChannelPipeline p = ch.pipeline();
p.addLast(new StringDecoder());
p.addLast(new StringEncoder());
p.addLast(new ServerHandler(dq, bGrp, wGrp));
}
});
ChannelFuture f = b.bind(PORT).sync();
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
class ServerHandler extends ChannelInboundHandlerAdapter {
public ServerHandler(dq, bGrp, wGrp) {
//assigning params to instance fields
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
//creating bulk of data from 'dq' and sending to client
/* e.g.
ctx.write(dq.get());
*/
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
if (dq.isEmpty() /*or other check that file was processed*/ ) {
try {
ctx.channel().closeFuture().sync();
} catch (InterruptedException ie) {
//...
}
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
ctx.close();
ctx.executor().parent().shutdownGracefully();
}
}
publicstaticvoidmain(字符串[]args){
新线程(()->{
//读取大文件并填充“dq”-数据队列
}).start();
final EventLoopGroup bGrp=新的NioEventLoopGroup(1);
final EventLoopGroup wGrp=新的NioEventLoopGroup();
试一试{
ServerBootstrap b=新的ServerBootstrap();
b、 组(bossGroup、workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG,100)
.childHandler(新的通道初始值设定项(){
@凌驾
公共频道(SocketChannel ch){
ChannelPipeline p=通道管道();
p、 addLast(新的StringDecoder());
p、 addLast(新的StringEncoder());
p、 addLast(新服务器处理程序(dq、bGrp、wGrp));
}
});
ChannelFuture f=b.bind(PORT.sync();
f、 通道().closeFuture().sync();
}最后{
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
类ServerHandler扩展ChannelInboundHandlerAdapter{
公共服务器处理程序(dq、bGrp、wGrp){
//将参数指定给实例字段
}
@凌驾
public void channelRead(ChannelHandlerContext ctx,Object msg){
//从“dq”创建大量数据并发送到客户端
/*例如。
ctx.write(dq.get());
*/
}
@凌驾
公共无效channelReadComplete(ChannelHandlerContext ctx){
如果(dq.isEmpty()/*或其他检查文件是否已处理*/){
试一试{
ctx.channel().closeFuture().sync();
}捕获(中断异常ie){
//...
}
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
@凌驾
公共无效例外情况(ChannelHandlerContext ctx,可丢弃原因){
ctx.close();
ctx.executor().parent().shutdownGracefully();
}
}
channelReadComplete(…)方法中的服务器关闭是否正确?我担心的是,还可能有另一个服务客户机(例如,在其他客户机中发送大量数据,并且当前客户机到达“dq”的末尾)
基本代码来自netty EchoServer/DiscardServer示例
问题是:当达到特定条件时,如何关闭netty服务器(从处理程序)
谢谢您不能从处理程序关闭服务器。您可以做的是向另一个线程发出信号,表示它应该关闭服务器 无法从处理程序关闭服务器。您可以做的是向另一个线程发出信号,表示它应该关闭服务器 我已经搜索过了,但没有找到解决方案。你能发个代码片段吗?应该是完全分离的线程或在netty中注册的东西?谢谢,我已经搜索过了,但没有找到解决方案。你能发个代码片段吗?应该是完全分离的线程或在netty中注册的东西?谢谢