本文介绍了netty如何使服务器程序不退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的项目中理解和使用Netty,因此从Netty用户指南中介绍的基本DiscardServer示例开始.

I am trying to understand and use Netty in my projects, so started off with the basic DiscardServer Example presnted in Netty's user guide.

以下内容几乎是原始代码的复制粘贴...

The below is almost a copy-paste of the original code...

public void run() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap sb = new ServerBootstrap();
        sb.group(bossGroup, workerGroup);
        sb.channel(NioServerSocketChannel.class);
        sb.childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast(new DiscardServerHandler());
            }
        });

        // Set some socket options such as socket queue backlog and keepalive.
        sb.option(ChannelOption.SO_BACKLOG, 128);
        sb.childOption(ChannelOption.SO_KEEPALIVE, true);

        // bind to port and start listening.
        ChannelFuture f = sb.bind(port).sync();

    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }
}

当我创建一个main方法时,如下所示,它运行良好,但随后被终止.

And when I create a main method, as below, it runs fine, but then just gets terminated.

public static void main(String[] args) throws Exception {

    int port = Integer.parseInt(args[0]);
    System.out.println("Starting discard server...");
    new DiscardServer(port).run();
    System.out.println("Discard server returning...");
}

程序输出为:

然后程序终止.

我期望netty类内部的事件循环可以使我的服务器工作,但似乎不会发生这种情况.我应该在main方法中编写一个while循环以确保服务器继续运行吗?

I am expecting that the event loop inside the netty classes should make my server, but it doesn't seem to happen that way. Should I write a while loop in my main method to ensure my server keeps running?

更新:

如果我使用Netty 4.x系列的jar文件,则该程序可以完美运行.我认为版本5仍在发展中,因此可能会出现错误.

The same program works flawlessly, if I use Netty 4.x series of jar files. I think the version 5 is still evolving, so errors are expected.

推荐答案

您忘记添加关闭操作的等待时间:

You forgot to add the wait on close operation:

// Bind and start to accept incoming connections.
ChannelFuture f = b.bind(PORT).sync();
// Wait until the server socket is closed.
// In this example, this does not happen, but you can do that to gracefully
// shut down your server.
f.channel().closeFuture().sync();

您现在要做的是启动服务器以进行侦听,然后在关闭执行程序后完成自己的main.

What you do right now is starting the server to listen, then finishing your own main after shutdown your executors.

您必须阻止某些事情,对于服务器而言,最好的方法是等待closeFuture操作.

You have to block on something, and the best for the server is to wait on the closeFuture operation.

然后,在处理程序中,您必须确定何时关闭服务器(f.channel().close()),这将唤醒主要过程并完成服务器.

Then in your handler, you have to decide when to close the server (f.channel().close()), which will turn to awake the main procedure and finishing the server.

重要提示:您必须区分子"通道(从处理程序中的ctx.channel()获取,并附加到一个唯一的客户端连接)和父"通道(从ctx.channel().parent()获得).关闭子级"不会终止f.channel(),因为该级是父级"(侦听器).因此,您必须执行类似ctx.channel().parent().close()的操作.

Important notice: you have to distinguish the "child" channel (which you get from ctx.channel() in your handler, attached to one unique client connection) and the "parent" channel (which you get from ctx.channel().parent()). Closing the "child" will not terminate the f.channel() since this one is the "parent" one (the listener). So you have to do something like ctx.channel().parent().close().

请注意,DiscardServerHandler的当前示例不包含任何要停止的代码(它是永远运行的处理程序).

Note that the current example of DiscardServerHandler does not include any code to stop (it is a forever running handler).

这篇关于netty如何使服务器程序不退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 21:00
查看更多