netty5 入门翻译

news/2024/7/15 16:43:41 标签: netty, 网络

翻译网页网址:http://netty.io/wiki/user-guide-for-5.x.html

写道
NioEventLoopGroup is a multithreaded event loop that handles I/O operation. Netty provides various EventLoopGroup implementations for different kind of transports. We are implementing a server-side application in this example, and therefore two NioEventLoopGroup will be used. The first one, often called 'boss', accepts an incoming connection. The second one, often called 'worker', handles the traffic of the accepted connection once the boss accepts the connection and registers the accepted connection to the worker. How many Threads are used and how they are mapped to the created Channels depends on the EventLoopGroup implementation and may be even configurable via a constructor.

 翻译:

写道
NioEventLoopGroup是一个多线程事件环路。它用来处理IO操作。Netty为不同类型的传输提供了不同类型的EventLoopGroup的实现类。在这个例子中我们正在做一个简单的服务器端实现,将使用两个NioEventLoopGroup实现类。第一个通常称为boss,它的作用是接收传入的链接。第二个被称为‘worker’,它的作用是一旦boss接受链接并且注册链接到worker则处理这个接收链接的传输。依赖于EventLoopGroup的实现决定多少线程将被使用并且他们如何匹配到新建的channel,当然并且也可以通过构造函数来配置。

 NioEventLoopGroup的实现类可以查看API,http://netty.io/5.0/api/index.html.路径为io.netty.channel.NioEventLoopGroup

 

写道
ServerBootstrap is a helper class that sets up a server. You can set up the server using a Channel directly. However, please note that this is a tedious process, and you do not need to do that in most cases.

   翻译:

 

写道
ServerBootstrap 是一个工具类。用来启动server。我们也可以使用io.netty.channel.Channel直接启动server,这是一个乏味的过程,大多数情况下我们不必这么做。

 

package io.netty.example.discard;

import io.netty.bootstrap.ServerBootstrap;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * Discards any incoming data.
 */
public class DiscardServer {

    private int port;

    public DiscardServer(int port) {
        this.port = port;
    }

    public void run() throws Exception {
        EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap(); // (2)
            b.group(bossGroup, workerGroup)
             .channel(NioServerSocketChannel.class) // (3)
             .childHandler(new ChannelInitializer<SocketChannel>() { // (4)
                 @Override
                 public void initChannel(SocketChannel ch) throws Exception {
                     ch.pipeline().addLast(new DiscardServerHandler());
                 }
             })
             .option(ChannelOption.SO_BACKLOG, 128)          // (5)
             .childOption(ChannelOption.SO_KEEPALIVE, true); // (6)

            // Bind and start to accept incoming connections.
            ChannelFuture f = b.bind(port).sync(); // (7)

            // 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();
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
        int port;
        if (args.length > 0) {
            port = Integer.parseInt(args[0]);
        } else {
            port = 8080;
        }
        new DiscardServer(port).run();
    }
}

  3) 此处我们指明使用NioServerSocketChannel来初始化一个新的Channel来接收传入的连接。

  4)此处指定的处理程序通常被一个新的接收渠道使用。ChannelInitializer类是一个指定的处理程序,他被用来帮助用户配置一个新的渠道。大多数状态下当我们通过添加一些处理器(例如:DiscardServerHandler)来实现我们的网络应用想要配置一个新的渠道的ChannelPipeline时需要这样做。当这个应用程序变的复杂时,例如我们最终将增加更多的处理器到这个管道里并且提取匿名类到定层类时很有用处。

  5)我们可以设置这个参数,参数是用来指定Channel实现的。这里我们是写一个TCP/IP服务器,所以我们允许设置socket选项,例如:tcpNoDelay或者keepAlive。请查看ChannelOption的apidocs文档和指定ChannelConfig实现来浏览一下支持的ChannelOptions。具体网址为:http://netty.io/5.0/api/io/netty/channel/ChannelConfig.html和http://netty.io/5.0/api/io/netty/channel/ChannelOption.html

  6)option() 是针对接收传入链接的NioServerSocketChannel,childOption()是针对被父ServerChannel接收的Channels,此处是NioServerSocketChannel


http://www.niftyadmin.cn/n/868651.html

相关文章

一个Tomcat支持不同的域名访问各自不同程序的配置方法

更多信息可以参考网址&#xff1a;http://329937021.iteye.com/blog/552494条件是&#xff1a;这样一种实际情况是&#xff0c;就一台服务器&#xff0c;当公网的IP地址也只有一个。应用是&#xff1a;不同的域名访问后访问相对应的不同的程序。举个例子来说如下&#xff1a;有…

花三分钟看完这篇文章你就懂了!薪资翻倍

很多程序员都有这样的觉悟&#xff1b;找工作学历是敲门砖&#xff0c;没有211,985起步的学历&#xff0c;想进一家大公司不太可能。 举个例子好了&#xff1b; 如果你是大厂面试官&#xff0c;参与面试的有10个刚刚毕业没有工作经验的普通学校应届生&#xff0c;还有10个刚刚…

drools书籍

附件中有drools书籍

主线程等待10秒钟,无应答返回(一)

场景需求&#xff1a;其他应用向我们的应用A发来请求&#xff0c;如果应用A10秒内无处理结果则返回数据未处理完成。工作简化&#xff1a;主线程等待10秒钟&#xff0c;如果子线程没有完成工作&#xff0c;则日志标识。下面是代码实现&#xff1a;import java.util.Random; imp…

php表单 菜鸟,PHP完整表单实例 | w3cschool菜鸟教程

PHP 完整表单实例本章节将介绍如何让用户在点击"提交(submit)"按钮提交数据前保证所有字段正确输入。PHP - 在表单中确保输入值在用户点击提交按钮后&#xff0c;为确保字段值是否输入正确&#xff0c;我们在HTML的input元素中插添加PHP脚本&#xff0c; 各字段名为:…

帮助程序员提高核心竞争力的30条建议,深度好文

IT行业的前景 近几年来&#xff0c;大数据、人工智能AI、物联网等一些技术不断发展&#xff0c;也让人们看到了IT行业的繁荣与良好的前景。越来越多的高校学府加大了对计算机的投入&#xff0c;设立相应的热门专业来吸引招生。当然也有越来越多的人选择从事这个行业&#xff0…

ubuntu 上下左右键变成ABCD

Ubuntu vi 上下左右变ABCD问题解决方法 错误问题&#xff1a;vi上下左右键显示为ABCD的问题 解决方法&#xff1a; 只要依次执行以下两个命令即可完美解决Ubuntu下vi编辑器方向键变字母的问题。 一.执行命令 sudo apt-get remove vim-common 二.执行命令 sudo apt-get instal…

matlab不能定位,MATLAB中的事件定位问题

假设在matlab中有以下几点&#xff1a;[t,x,te,xe,ie] ode15s(myfunc,[tStart tFinal],x0,odeset(‘Events’, event));问题11a)仅在求解器成功执行步骤后才调用函数事件.这是真的&#xff1f;1b)在解算器成功完成一步之后,myfunc的最后一次调用可能不是导致成功步骤的调用吗&…