0. 关键点

a). 非阻塞网络调用,异步方法立即返回
b). 选择器(Selector)使得单一线程就可监控很多连接上的事件。 <dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId> <!-- Use 'netty-all' for 4.0 or above -->
<version>4.1.13</version>
<scope>compile</scope>
</dependency>

1.1 Channel-类似socket,管道

1.2 回调-操作完成时调用

1.3 Future-异步通知--更精确的异步

类似更精确的异步。

JDK预置concurrent.Future需要手动检查对应的操作是否已完成,非常繁琐。

Netty封装成ChannelFuture消除手动检查,可注册一或多个ChannelFutureListner,回调方法operationComplete()操作完成时调用,可检查是成功还是失败了。

Channel channel = null;
ChannelFuture future = channel.connect(new InetSocketAddress("127.0.0.1", 8888));
//注册监测异步事件调用完成
future.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if( future.isSuccess()){
//异步调用成功
ByteBuf buffer = Unpooled.copiedBuffer("Hello", CharsetUtil.UTF_8);
future.channel().write(buffer);
} else {
//异步调用失败
Throwable cause = future.cause();
cause.printStackTrace();
}
}
});

1.4 事件和ChannelHandler-链

1.5 EventLoop-处理所有IO事件-多线程处理

为每一个channel分配一个EventLoop,处理所有的事件,包括:注册感兴趣的事件、将事件派发给ChannelHandler,安排进一步的动作。

EventLoop本身只由一个线程驱动,处理一个Channel的所有IO事件,并且在整个生命周期不变(不需考虑同步问题)。

1.8 实例代码

1.8.1 server代码

//测试结果
Received Connect, remote IP:/127.0.0.1:56015 //c连接上时打印
channelRead Invoked!!
Server Received:1111111111111111111111111
channelReadComplete Invoked!!!
Received Connect, remote IP:/127.0.0.1:56018
public class EchoServer {
public static void main(String[] args) throws InterruptedException {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup(); try {
ServerBootstrap b = new ServerBootstrap(); //引导类,BS端不同
b.group(bossGroup, workerGroup) //master主线程,slaves处理handler的线程池
.channel(NioServerSocketChannel.class) //何种channel(socket),BS端不同
.childHandler(new ChannelInitializer<SocketChannel>() {
//当一个新连接被接受时,一个新的子channel被创建,将处理类实例添加到ChannelPipeLine(链)中
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new EchoServerHandler());
}
}); //绑定端口,sync()阻塞到调用完成
ChannelFuture f = b.bind(8888).sync();
//阻塞到关闭完成
f.channel().closeFuture().sync();
} finally {
//关闭EventLoopGroup直到完成
bossGroup.shutdownGracefully().sync();
workerGroup.shutdownGracefully().sync();
}
}
}
public class EchoServerHandler  extends ChannelInboundHandlerAdapter{
@Override
public void channelActive(ChannelHandlerContext ctx){
System.out.println("Received Connect, remote IP:"+ctx.channel().remoteAddress());
} @Override
//对每个传入的消息都要调用,存在TCP粘连的问题
public void channelRead(ChannelHandlerContext ctx, Object msg) {
System.out.println("channelRead Invoked!!");
ByteBuf in = (ByteBuf) msg;
System.out.println("Server Received:" + in.toString(CharsetUtil.UTF_8));
ctx.write(in);
} @Override
//读取完当前批次消息时调用
public void channelReadComplete(ChannelHandlerContext ctx){
System.out.println("channelReadComplete Invoked!!!");
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
} @Override
//捕捉发生的异常
public void exceptionCaught( ChannelHandlerContext ctx , Throwable cause){
cause.printStackTrace();
ctx.close();
}
}

1.8.2 Client代码

//测试结果
channelActive Invoked!!!!!
Client received:Netty rocks
public class EchoClient {
public static void main(String[] args) throws InterruptedException {
EventLoopGroup group = new NioEventLoopGroup(); try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.remoteAddress("127.0.0.1", 8888)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new EchoClientHandler());
}
});
ChannelFuture f = b.connect().sync();
f.channel().closeFuture().sync();
}finally {
group.shutdownGracefully().sync();
}
}
}
public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf>{

    @Override
public void channelActive( ChannelHandlerContext ctx){
System.out.println("channelActive Invoked!!!!!");
ctx.writeAndFlush(Unpooled.copiedBuffer("Netty rocks", CharsetUtil.UTF_8));
} @Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {
System.out.println("Client received:"+byteBuf.toString(CharsetUtil.UTF_8));
} @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause){
cause.printStackTrace();
ctx.close();
}
}

1.- Netty设计理念-异步和事件驱动的更多相关文章

  1. Netty实战一之异步和事件驱动

    Netty是一款异步的事件驱动的网络应用程序框架,支持快速地开发可维护的高性能的面向协议的服务器和客户端. 使用Netty你可以并不是很需要网络编程.多线程处理.并发等专业Java知识的积蓄. Net ...

  2. 关于Web开发里并发、同步、异步以及事件驱动编程的相关技术

    一.开篇语 我的上篇文章<关于如何提供Web服务端并发效率的异步编程技术>又成为了博客园里“编辑推荐”的文章,这是对我写博客很大的鼓励,也许是被推荐的原因很多童鞋在这篇文章里发表了评论,有 ...

  3. suging闲谈-netty 的异步非阻塞IO线程与业务线程分离

    前言 surging 对外沉寂了一段时间了,但是作者并没有闲着,而是针对于客户的需要添加了不少功能,也给我带来了不少外快收益, 就比如协议转化,consul 的watcher 机制,JAVA版本,sk ...

  4. 浅析Netty的异步事件驱动(二)

    上一篇文件浅析了Netty中的事件驱动过程,这篇主要写一下异步相关的东东. 首先,什么是异步了? 异步的概念和同步相对.当一个异步过程调用发出后,调用者不能立刻得到结果.实际处理这个调用的部件在完成后 ...

  5. 浅析Netty的异步事件驱动(一)

    本篇文章着重于浅析一下Netty的事件处理流程,Netty版本为netty-3.6.6.Final. Netty定义了非常丰富的事件类型,代表了网络交互的各个阶段.并且当各个阶段发生时,触发相应的事件 ...

  6. 十九、Node.js-非阻塞IO、异步以及 '事件驱动EventEmitter'解决异步

    1.Nodejs 的单线程 非阻塞 I/O 事件驱动 在 Java.PHP 或者.net 等服务器端语言中,会为每一个客户端连接创建一个新的线程而每个线程需要耗费大约 2MB 内存.也就是说,理论上, ...

  7. Node.js学习笔记(六) --- Nodejs 的非阻塞 I/O、 异步、 事件驱动

    1. Nodejs 的单线程 非阻塞 I/O 事件驱动在 Java. PHP 或者.net 等服务器端语言中,会为每一个客户端连接创建一个新的线程.而每个线程需要耗费大约 2MB 内存.也就是说,理论 ...

  8. 基于netty的异步http请求

    package com.pt.utils; import io.netty.bootstrap.Bootstrap; import io.netty.channel.ChannelFuture; im ...

  9. Nodejs 之非阻塞 I/O、异步、事件驱动

    1.非阻塞 I/O var fs = require('fs'); console.log('); fs.readFile('mime.json',function (err,data) {//rea ...

随机推荐

  1. 【转】 Pro Android学习笔记(四五):Dialog(2):DialogFragment

    [-] 重写onCreateView 通过onCreateView设置UI和按键反馈 信息保存 重写onCreateDialog DialogFragment的实例newInstance()已经在上一 ...

  2. mysql基础之三:char与varchar区别,varchar(M)能存多少

    char与varchar区别 char (13)长度固定, 如'1234567890' 存储需要空间 10个字符; varchar(13) 可变长 如'1234567890' 需要存储空间 11字符; ...

  3. 关于 sklearn.decomposition.KernelPCA的简单介绍

    from sklearn import decomposition import numpy as np A1_mean = [1, 1] A1_cov = [[2, .99], [1, 1]] A1 ...

  4. linux命令-rpm安装和卸载

    软件包 先查看一下rpm包 [root@wangshaojun Packages]# mount /dev/cdrom /mnt/////挂载[root@wangshaojun Packages]# ...

  5. 面试题17:打印1到最大的n位数

    // 面试题17:打印1到最大的n位数 // 题目:输入数字n,按顺序打印出从1最大的n位十进制数.比如输入3,则 // 打印出1.2.3一直到最大的3位数即999. 解题思路: 首先是一个大陷阱,n ...

  6. hdu1057

    #include <iostream> #include <string> #include <cstring> using namespace std; int ...

  7. Note: OBLIVIATE: A Data Oblivious File System for Intel SGX

    OBLIVIATE redesigned ORAM for SGX filesystem operations for confuse access patterns to protect user ...

  8. [CentOS7] firewalld重启失败 Failed to start firewalld - dynamic firewall daemon.

    错误信息: Failed to start firewalld - dynamic firewall daemon. 如图: 解决方法: 也就是kernel不支持现在的firewall防火墙的某些模块 ...

  9. 第三方登录---微信(使用laravel插件)

    转发: https://www.jianshu.com/p/7be757655814 TP框架: http://www.php.cn/php-weizijiaocheng-363509.html

  10. Python实现返回指定范围内的所有素数

    # 获取a, b范围的所有素数 def func(a, b): li = [] for i in range(a, b+1): for j in range(2, i): if i % j == 0: ...