本节大纲:

1、Handler的执行顺序
2、自定义二进制协议(每条完整数据的组成),从而解决拆包和粘包。
3、通过为每个channel创建新的handler,从而解决即使handler中使用全局变量,也可以避免竞态条件。

1、Handler的执行顺序。

client中pipeline顺序:
//first,add codec
pipeline.addLast(new BigIntegerDecoder());
pipeline.addLast(new NumberEncoder());
//then,add business logic
pipeline.addLast(new FactorialClientHandler());

server中pipeline顺序:
//first,codec
pipeline.addLast(new BigIntegerDecoder());
pipeline.addLast(new NumberEncoder());
//then,business logic
pipeline.addLast(new FactorialServerHandler());

总结:
写(outbound):自下而上,跳过inbound
读(inbound): 自上而下,跳过outbound
Codec放在上边,业务逻辑handler放在下边。

2、自定义二进制协议(每条完整数据的组成),从而解决拆包和粘包。

每条完整数据的组成:'F'+4个字节的长度+数据

将传进来的number编码为二进制,在其前边加上'F'和4个字节的长度,作为前缀。
例如:42被编码为:'F',0,0,0,1,42 

客户端:

public class FactorialClientHandler extends SimpleChannelInboundHandler<BigInteger> {

    @Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < 1024; j++) {
sb.append(j);
}
BigInteger bigInt = new BigInteger(sb.toString()); ChannelFuture future = ctx.writeAndFlush(bigInt);//只发送1次
log.info("send:{}", bigInt);
log.info("send字节数:{}", bigInt.toByteArray().length);
future.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
log.info("发送成功");
}
}
});
}
/**
* <pre>
* 自定义二进制协议:F+4字节长度+具体数值
* 例如:'F',0,0,0,1,42 解码为new BigInteger("42")
* </pre>
*/
@Slf4j
public class BigIntegerDecoder extends ByteToMessageDecoder {
private int splitCount = 0;
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
log.info(">>>>>splitCount:{},可读字节数:{}",++splitCount,in.readableBytes());
//wait until the length prefix is available
if (in.readableBytes() < 5) {
return ;
}
in.markReaderIndex();
int magicNumber = in.readUnsignedByte();
if (magicNumber !='F') {
throw new CorruptedFrameException("Invalid:"+magicNumber);
}
//wait until the whole data is available
int dataLength = in.readInt();
if (in.readableBytes() < dataLength) {
in.resetReaderIndex();
return ;
}
//convert the received data into a new BigInteger
byte [] decoded = new byte[dataLength];
in.readBytes(decoded);
out.add(new BigInteger(decoded));
}
}

客户端发送了1240个字节的BigInteger,服务端接收:

18:15:13.121 [nioEventLoopGroup-3-1] >>>>>splitCount:1,可读字节数:1024
18:15:13.126 [nioEventLoopGroup-3-1] >>>>>splitCount:2,可读字节数:1245

虽然客户端只发送了1次,但服务端分2次接收在BigIntegerDecoder中都接收完后,才调用FactorialServerHandler的channelRead0方法

注意,

in.markReaderIndex();
。。。
if (in.readableBytes() < dataLength) {
in.resetReaderIndex();
return ;
}

以上的流程:

当第一次时,in.readableBytes()=1024,而dataLength=1245,所以进入该方法,将readerIndex复位到之前mark处,此例为0。舍弃该部分包数据。

当第二次时,in.readableBytes()=1245(说明,从0开始读的),读取到了完整的报文。

如果去掉以上代码,则会报错:

18:09:51.465 [nioEventLoopGroup-3-1] >>>>>splitCount:1,可读字节数:1024
io.netty.handler.codec.DecoderException: java.lang.IndexOutOfBoundsException: readerIndex(5) + length(1240) exceeds writerIndex(1024): PooledUnsafeDirectByteBuf(ridx: 5, widx: 1024, cap: 1024)

当然,服务端处理完并把原文发给客户端后,客户端也是分2次读取的:

18:15:13.119 [nioEventLoopGroup-2-1] send字节数:1240
18:15:13.153 [nioEventLoopGroup-2-1] >>>>>splitCount:1,可读字节数:1024
18:15:13.154 [nioEventLoopGroup-2-1] >>>>>splitCount:2,可读字节数:1245

3、通过为每个channel创建新的handler,从而解决即使handler中使用全局变量,也可以避免竞态条件

并发发送数据包,且每个数据包超过1024个字节,如下代码中的成员变量:

public class FactorialServerHandler extends SimpleChannelInboundHandler<BigInteger> {
private BigInteger lastMultiplier = new BigInteger("1");
private BigInteger factorial = new BigInteger("1"
); @Override
protected void channelRead0(ChannelHandlerContext ctx, BigInteger msg) throws Exception {
//计算阶乘并发送到客户端
lastMultiplier = msg;
factorial =
factorial.multiply(msg);
ctx.writeAndFlush(factorial);
}
。。。

客户端调用:

public static void main(String[] args) throws Exception {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
executor(2,Thread.currentThread().getName());// 2*3*4*5=120
}
});
t1.start();
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
executor(3,Thread.currentThread().getName());// 3*4*5=60
}
});
t2.start();
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
executor(4,Thread.currentThread().getName());// 4*5=20
}
});
t3.start();
}
public static void executor(int next,String threadName) {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class).handler(new FactorialClientInitializer(next));
// make a new connection
ChannelFuture f = b.connect(HOST, PORT).sync();
// get the handler instance to retrieve the answer.
FactorialClientHandler handler = (FactorialClientHandler) f.channel().pipeline().last();
// print out the answer
log.info("threadName:{},开始:{},结束:{},结果:{}", threadName,next,COUNT, handler.getFactorial());
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
}

结果:不乱,各自打印各自的。因为,每发送1条数据则创建1个Channel和handler ,所以不会乱。

4、codec是netty封装好了的handler,简化代码开发。

本例中涉及的是:

ByteToMessageDecoder(inbound):必须实现decode方法

MessageToByteEncoder<Number>(outbound):必须实现encode方法

最后,

以上的3参考代码:userguide-04-factorial。1、2参考代码:userguide-04-2-factorial

Netty(4-1)factorial~总结的更多相关文章

  1. 从netty-example分析Netty组件

    分析netty从源码开始 准备工作: 1.下载源代码:https://github.com/netty/netty.git 我下载的版本为4.1 2. eclipse导入maven工程. netty提 ...

  2. Java Netty 4.x 用户指南

    问题 今天,我们使用通用的应用程序或者类库来实现互相通讯,比如,我们经常使用一个 HTTP 客户端库来从 web 服务器上获取信息,或者通过 web 服务来执行一个远程的调用. 然而,有时候一个通用的 ...

  3. netty Getting Started--reference

    reference from:http://docs.jboss.org/netty/3.1/guide/html/start.html 1.1. Before Getting Started 1.2 ...

  4. [转载] Netty源码分析

    转载自http://blog.csdn.net/kobejayandy/article/details/11836813 Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高 ...

  5. netty 入门二 (传输bytebuf 或者pojo)

    基于流的数据传输:在基于流的传输(如TCP / IP)中,接收的数据被存储到套接字接收缓冲器中. 不幸的是,基于流的传输的缓冲区不是数据包的队列,而是字节队列. 这意味着,即使您将两个消息作为两个独立 ...

  6. Netty 4.1 Getting Start (翻译) + Demo

    一.先来官方入门页面的翻译(翻译不好请多包涵) 入门 本章以简单的例子来介绍Netty的核心概念,以便让您快速入门.当您阅读完本章之后,您就能立即在Netty的基础上写一个客户端和一个服务器. 如果您 ...

  7. User guide for Netty 4.x

    Table of Contents Preface The Solution Getting Started Before Getting Started Writing a Discard Serv ...

  8. Netty 介绍

    本指南对Netty 进行了介绍并指出其意义所在. 1. 问题 现在,我们使用适合一般用途的应用或组件来和彼此通信.例如,我们常常使用一个HTTP客户端从远程服务器获取信息或者通过web service ...

  9. netty参考

    前言 问题 现如今我们使用通用的应用程序或者类库来实现系统之间地互相访问,比如我们经常使用一个HTTP客户端来从web服务器上获取信息,或者通过web service来执行一个远程的调用. 然而,有时 ...

随机推荐

  1. mac快速正确的安装 Ruby, Rails 运行环境

    Mac OS X 任意 Linux 发行版本(Ubuntu,CentOS, Redhat, ArchLinux ...) 强烈新手使用 Ubuntu 省掉不必要的麻烦! 以下代码区域,带有 $ 打头的 ...

  2. Java企业微信开发_10_未验证域名归属,JS-SDK功能受限

    1.现象: 在企业微信后台填写可信域名后,提示:未验证域名归属,JS-SDK功能受限,如下图: 点击“申请域名校验”后, 注意:域名根目录 当时一直不清楚这个域名根目录在哪里,最后让我给试出来了 2. ...

  3. 关于ng-class中添加多个样式类的解决方案

    想要达到ng-class的效果,有两种写法 1.class=“{{class}} class1 class2” 2.ng-class="{true: 'active', false: 'in ...

  4. [JSOI 2018] 潜入行动

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5314 [算法] 考虑dp , 用f[i][j][0 / 1][0 / 1]表示以i为 ...

  5. 【LeetCode】312. Burst Balloons

    题目: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented ...

  6. 理解I/O Completion Port(完成端口)

    欢迎阅读此篇IOCP教程.我将先给出IOCP的定义然后给出它的实现方法,最后剖析一个Echo程序来为您拨开IOCP的谜云,除去你心中对IOCP的烦恼.OK,但我不能保证你明白IOCP的一切,但我会尽我 ...

  7. HL7 Event Type

    Table 0003 - Event type Value Description A01 ADT/ACK - Admit / visit notification A02 ADT/ACK - Tra ...

  8. jQuery之简单的表单验证

    html部分: <body> <form method="post" action=""> <div class="in ...

  9. ubuntu_deb安装命令

    dpkg命令常用格式如下: sudo dpkg -I iptux.deb#查看iptux.deb软件包的详细信息,包括软件名称.版本以及大小等(其中-I等价于--info) sudo dpkg -c ...

  10. Java 打包成exe安装包

    1.在eclipse中导出Runnable JAR file 2.选择主函数所在的类和输出位置后finish: 3.这里选择的打包工具是exe4j,在网上找序列号注册一下,否则在打完后在exe运行时, ...