文档章节

Netty NIO transport && OIO transport

秋风醉了
 秋风醉了
发布于 2014/06/20 02:33
字数 1103
阅读 1365
收藏 2
点赞 0
评论 0

Netty NIO transport && OIO transport


OIO  transport

The  OIO  transport  is  a  compromise(妥协)  in  Netty.  It  builds  on  the  known  unified(统一)  API  but isn t asynchronous(异步) by nature because it uses the blocking java.net implementations under the hood. At  first  glance, this  transport  may  not  look  useful  to  you,  but  it  has  its  use  cases. 

Because the OIO transport uses the java.net classes internally(内部的), it also uses the same logic that you may already be familiar with if you previously written network applications. When using these classes, you usually have one thread that handles the acceptance of new sockets (server-side) and then creates a new thread for each accepted connection to serve the traffic  over  the  socket.  This  is  needed  as  every  I/O  operation  on  the  socket  may  block  at  any time. If you share the same thread over more than one connection (socket), this could lead to a situation where blocking an operation could block all other sockets from doing their work. Knowing  that  operations  may  block,  you  may  start  to  wonder  how  Netty  uses  it  while  still providing  the  same  way  of  building  APIs.  Here Netty  makes  use  of  the SO_TIMEOUT that  you can set on a socket. This timeout specifies the maximum number of milliseconds to wait for an I/O operation  to  complete.  If  the  operation  doesn t  complete  within  the  specified  timeout,  a SocketTimeoutException is  thrown.  Netty  catches  this SocketTimeoutException and moves on with its work. Then on the next EventLooprun, it tries again. Unfortunately, this is the  only  way  to  do  this  and  still confirm the  inner  working  of  Netty.  The  problem  with  this approach(途径)  is  that  firing  the SocketTimeoutException isn t  free,  as  it  needs  to  fill  the StrackTrace, and so on. 

Netty OIO 编程模型

package nio2;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.oio.OioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.oio.OioServerSocketChannel;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

/**
 * Start  by  writing  a  blocking  version  of  the  application,
 * but  this  time  use  Netty  as  a  network
 * framework, as shown in the following listing.
 * 这是netty的阻塞IO的写法,同时也是同步的
 */
public class NettyOioServer {

    public void server(int port) throws Exception {
        final ByteBuf buf = Unpooled.wrappedBuffer(
                Unpooled.copiedBuffer("Hi!\r\n", Charset.forName("UTF-8")));

        // Use OioEventLoopGroup Ito allow blocking mode (Old-IO)
        EventLoopGroup group = new OioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(group)
                    .channel(OioServerSocketChannel.class)
                    .localAddress(new InetSocketAddress(port))
                            //Specify ChannelInitializer that will be called for each accepted connection
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(
                                    //Add ChannelHandler to intercept events and allow to react on them
                                    new ChannelInboundHandlerAdapter() {
                                        @Override
                                        public void channelActive(ChannelHandlerContext ctx) throws Exception {
                                            System.out.println("--active--");
                                            //Write message to client and add ChannelFutureListener to close connection once message written
                                            ctx.write(buf.duplicate()).addListener(ChannelFutureListener.CLOSE);
                                        }
                                    });
                        }
                    });

            /**
             * Bind server to accept connections
             */
            ChannelFuture f = b.bind().sync();
            f.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully().sync();
        }
    }


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

        int port = 9898;
        NettyOioServer server = new NettyOioServer();
        System.out.println("bind port " + port);
        server.server(port);
    }
}


NIO  transport

selector-based

The  NIO  transport  is  currently  the  most  used.  It  provides  a  full  asynchronous  implementation of all I/O operations by using the selector-based approach that s included in Java since Java 1.4 and the NIO subsystem. 

The  idea  is  that  a  user  can  register  to  get  notified  once  a  channel s  state  changes. 

Selection operation bit-set

  1. OP_ACCEPT 

    Get notified once a new connection is accepted and a channel is created. 

  2. OP_CONNECT 

    Get notified once a connection attempt finishes. 

  3. OP_READ 

    Get notified once data is ready to be read out of the channel. 

  4. OP_WRITE 

    Get notified once it s possible to write more data to the channel. Most of the time this is possible, but it may not be because the OS socket buffer is completely filled. This usually happens when you write faster then the remote peer can handle it. 

Netty s NIO transport uses this model internally to receive and send data, but exposes its own API to the user, which completely hides the internal implementation. As mentioned previously, that  helps  to  expose  only  one  unified(统一的)  API  to  the  user,  while  hiding  all  of  the  internals(内部).

One feature that offers only the NIO transport at the moment is called zero-file-copy . This feature allows you to quickly and efficiently transfer content from your file system. The feature provides a way to transfer the bytes from the file system to the network stack without copying the bytes from the kernel space(内核态) to the user space(用户态)

Netty NIO 异步非阻塞编程模型

package nio2;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

/**
 * Asynchronous networking with Netty
 * For now, I'll note that you can use ChannelHandlerfor these tasks:
 *  Transforming data from one format to another.
 *  Notifying you of exceptions.
 *  Notifying you when a Channel becomes active or inactive.
 *  Notifying you once a channel is registered/deregistered from an EventLoop.
 *  Notifying you about user-specific events.
 */
public class NettyNioServer {

    public void server(int port) throws Exception {
        final ByteBuf buf = Unpooled.wrappedBuffer(
                Unpooled.copiedBuffer("Hi!\r\n", Charset.forName("UTF-8")));

        EventLoopGroup group = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(group)
                    .channel(NioServerSocketChannel.class)
                    .localAddress(new InetSocketAddress(port))
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(
                                    new ChannelInboundHandlerAdapter() {
                                        @Override
                                        public void channelActive(ChannelHandlerContext ctx) throws Exception {
                                            System.out.println("--active--");
                                            //Write message to client and add ChannelFutureListener to close connection once message written
                                            ctx.write(buf.duplicate()).addListener(ChannelFutureListener.CLOSE);
                                        }
                                    });
                        }
                    });

            /**
             * Bind server to accept connections
             */
            ChannelFuture f = b.bind().sync();
            f.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully().sync();
        }
    }


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

        int port = 9898;
        NettyOioServer server = new NettyOioServer();
        System.out.println("bind port " + port);
        server.server(port);
    }
}

附:Netty提供的传输方式包括:

  • OIO

  • NIO

  • Local

  • Embedded

四种,以上只讲了两种。


写在后边

虽然通篇都是英文,但读起来感觉还是不错的,不常见的英文单词标注了中文。

通过最近一段时间的读英文文档,发现读起来不是很费劲了,至少感觉比读中文文档少了很多歧义。

自然有了google翻译的帮助,不会的单词都是小case,呵呵

=======END=======

© 著作权归作者所有

共有 人打赏支持
秋风醉了
粉丝 229
博文 577
码字总数 407134
作品 0
朝阳
程序员
第四章:Transports(传输)

本章内容 Transports(传输) NIO(non-blocking IO,New IO), OIO(Old IO,blocking IO), Local(本地), Embedded(嵌入式) Use-case(用例) APIs(接口) 网络应用程序都是以字节码传输。Java开发网络...

李矮矮
2016/09/23
16
0
Java NIO && Netty的epoll实现

Java NIO && Netty的epoll实现 Java NIO Java NIO根据操作系统不同, 针对nio中的Selector有不同的实现: macosx: KQueueSelectorProvider solaris: DevPollSelectorProvider Linux: EPollSe......

秋风醉了
2016/03/18
324
0
dubbo服务启动是包错误dubbo> to /127.0.0.1:56798, cause: null 但服务能正常提供

com.alibaba.dubbo.remoting.RemotingException: Failed to send message OK dubbo> to /127.0.0.1:56798, cause: null at com.alibaba.dubbo.remoting.transport.netty.NettyChannel.send(N......

xavier1
2017/08/23
56
1
SonarQube启动失败求解答

环境:window7 jdk:java version "1.8.0_131" sonar.log日志: --> Wrapper Started as Console Launching a JVM... Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org Copyright ......

Hey__
2017/06/30
201
0
java.nio.channels.ClosedChannelException: null

要从一个服务器下载大量文件到本地,在服务器部署了一个采集软件,但是有几个文件始终没采集下来,看了下日志,有这个报错,请问大家有解决办法吗? com.alibaba.dubbo.remoting.RemotingExc...

fengjidly
2017/01/09
806
0
SonarQube连接mysql时报错,感谢各位

2018.01.01 15:12:24 INFO app[][o.s.a.SchedulerImpl] SonarQube is stopped 2018.01.01 15:12:24 WARN app[][o.e.t.n.Netty4Transport] exception caught on transport layer [[id: 0x1d8d......

gaomq
01/01
15
1
Epoll_native non-blocking transport for Linux

Epoll_native non-blocking transport for Linux As we explained earlier, Netty’s NIO transport is based on the common abstraction for asynchronous/non-blocking networking provide......

秋风醉了
2016/03/24
23
0
java.io.IOException: 远程主机强迫关闭了一个现有的连接。

我在创建索引的时候,索引成功创建了,但elasticsearch服务报出了错误: [2014-03-05 19:17:58,806][WARN ][transport.netty ] [Diablo] exception caught on transport layer [[id: 0x28fb......

小天_C
2014/03/05
5.9K
2
Netty 4.1.8 io.netty.util.ResourceLeakDetector - LEAK:

源代码及项目:https://github.com/zhouhailin555/NettyProtocol.git 希望各位大拿帮忙看看。 运行时报错如下: 1.ERROR io.netty.util.ResourceLeakDetector - LEAK: ByteBuf.release() was......

zhouhl
2017/02/19
563
0
zookeeper+dubbo实现分布式服务调用,提供者关闭后重新注册,消费者一定要重启吗?

提供者关闭重启,消费者不重启,就会down,怎么解啊 2017-08-16 16:18:36.790 [DubboClientReconnectTimer-thread-1|] WARN o.j.n.channel.DefaultChannelPipeline- [DUBBO] An exception wa......

看见一座山
2017/08/16
218
4

没有更多内容

加载失败,请刷新页面

加载更多

下一页

时间复杂度

1. 维基上的定义 在计算机科学中,算法的时间复杂度是一个函数,它定性描述该算法的运行时间。这是一个代表算法输入值的字符串的长度的函数。时间复杂度常用大O符号表述,不包括这个函数的低...

liuyan_lc
13分钟前
0
0
js中的~符

~是js里的按位取反操作符,~~就是执行两次按位取反,其实就是保持原值,但是注意虽然是原值,但是对布尔型变量执行这个操作,会转化成相应的数值型变量,也就是 ~~true === 1,~~false === 0...

JamesView
14分钟前
0
0
webpack安装

npm install --save-dev webpack-cli

Vincent-Duan
17分钟前
0
0
实时监听EditText内容变化

主要是addTextChangedListener方法的使用 aswerEdittext.addTextChangedListener(new TextWatcher() { //编辑框的内容发生改变之前的回调方法 @Override public void before...

王先森oO
20分钟前
0
0
python连接mysql数据库

安装好必要的pymysql插件 pip install pymysql 写好配置文档 DATABASES = { # 'default': { # 'ENGINE': 'django.db.backends.sqlite3', # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), #......

南桥北木
22分钟前
0
0
linux下安装 Zookeeper

Zookeeper 注册中心安装 安装: wget http://www.apache.org/dist/zookeeper/zookeeper-3.3.3/zookeeper-3.3.3.tar.gztar zxvf zookeeper-3.3.3.tar.gzcd zookeeper-3.3.3cp conf/zoo_......

颖辉小居
23分钟前
0
0
C#判断文件的真实类型(非扩展名判断)

public static void CheckFileType() { string path = @"D:\word.doc"; System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read); ......

Lytf
25分钟前
0
0
java - 把日志生成到指定目录

1.按天小时生成日志文件 log4j.appender.ruiming.DatePattern='_'yyyy-MM-dd-HH 2.最大日志文件数据 log4j.appender.ruiming.MaxBackupIndex=100 3.下图画圈圈的很关键,是生成到知道目录必备...

轻量级赤影
25分钟前
0
0
少走弯路,给Java 1~5 年程序员的建议

今天LZ是打算来点干货,因此咱们就不说一些学习方法和技巧了,直接来谈每个阶段要学习的内容甚至是一些书籍。这一部分的内容,同样适用于一些希望转行到Java的同学。 在大家看之前,LZ要先声...

Java填坑之路
26分钟前
1
0
Java transient关键字

transient关键字使用方法 我们都知道一个对象只要实现了Serilizable接口,这个对象就可以被序列化,java的这种序列化模式为开发者提供了很多便利,我们可以不必关系具体序列化的过程,只要这...

蔡小鹏
27分钟前
0
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部