简体   繁体   English

关闭Netty服务器本身

[英]Shutting down Netty server itself

I'm using Netty server to solve this: reading a big file line by line and processing it. 我正在使用Netty服务器来解决此问题:逐行读取一个大文件并进行处理。 Doing it on single machine is still slow, so I've decided to use server to serve chunks of data to clients. 在单台计算机上执行该操作仍然很慢,因此我决定使用服务器向客户端提供数据块。 That already works, but what I also want is that server shut downs itself when processed whole file. 那已经可以了,但是我还想要的是服务器在处理整个文件时会自行关闭。 The source code I'm using right now is: 我现在正在使用的源代码是:

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();
    }
}

Is the server shutdown in channelReadComplete(...) method correct? 在channelReadComplete(...)方法中关闭服务器是否正确? What I'm afraid is that there can still be another served client (eg sending big bulk in other client and with current client reached the end of 'dq'). 我担心的是,仍然会有另一个服务的客户端(例如,向其他客户端发送大量邮件,并且当前客户端已到达“ dq”的末尾)。

The base code is from netty EchoServer/DiscardServer examples. 基本代码来自netty EchoServer / DiscardServer示例。

The question is : how to shut down netty server (from handler) when reached specific condition. 问题是:达到特定条件时如何关闭Netty服务器(从处理程序)。

Thanks 谢谢

You can not shutdown a server from a handler. 您无法从处理程序中关闭服务器。 What you could do is signal to a different thread that it should shutdown the server. 您可以做的是向另一个线程发出信号,通知它应该关闭服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM