10 - EmbeddedChannel-测试ChannelHandler链
方法 | 职责 |
---|---|
writeInbound(Object... msgs) | 将入站消息写入到EmbeddedChannel中 |
readInbound() | 从EmbeddedChannel中读取一个入站消息,任何返回的消息都穿过了整个ChannelPipeLine |
writeOutbound(Object... msgs) | 将出站消息写入到EmbeddedChannel中 |
readOutbound() | 从EmbeddedChannel中读取一个出站消息,任何返回的消息都穿过了整个ChannelPipeLine |
10.1 示例
//测试结果
Received message:0
Received Finished!
Received message:1
Received Finished!
Received message:2
Received Finished!
embeddedChannel readInbound:0
embeddedChannel readInbound:1
embeddedChannel readInbound:2
public class EmBeddedChannelTest {
public static void main(String[] args) {
ByteBuf byteBuf = Unpooled.buffer();
for (int i = 0; i < 3; i++) {
byteBuf.writeInt(i);
}
EmbeddedChannel embeddedChannel = new EmbeddedChannel();
//获取channelPipeLine
ChannelPipeline channelPipeline = embeddedChannel.pipeline();
channelPipeline.addLast(new SimpleChannelInBoundHandlerTest());
channelPipeline.addFirst(new DecodeTest());
//写入测试数据
embeddedChannel.writeInbound(byteBuf);
System.out.println("embeddedChannel readInbound:"+embeddedChannel.readInbound());
System.out.println("embeddedChannel readInbound:"+embeddedChannel.readInbound());
System.out.println("embeddedChannel readInbound:"+embeddedChannel.readInbound());
}
}
//解码器
class DecodeTest extends ByteToMessageDecoder {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
if( in.readableBytes()>=4 ){
out.add(in.readInt());
}
}
}
//channelHandler
class SimpleChannelInBoundHandlerTest extends SimpleChannelInboundHandler {
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("Received message:"+msg);
System.out.println("Received Finished!");
ctx.fireChannelRead(msg);
}
}
10 - EmbeddedChannel-测试ChannelHandler链的更多相关文章
- 【转】Java学习---10个测试框架介绍
[原文]https://www.toutiao.com/i6594302925458113027/ JAVA 程序员需要用到 10 个测试框架和库 Java 程序员需要用到十大单元测试和自动化集成测试 ...
- Rspec: everyday-rspec实操: 第10章测试其他功能,第11章TDD 第12章总结。
10.测试文件上传 作者推荐的Paperclip,官方维护组已经不推荐使用deprecated. 推荐使用rails自带的 ActiveStorage. Active Storage: 推进文件上传到 ...
- FreeNAS-9.10虚拟机测试安装
虚拟机安装NreeNAS-9.10步骤 需求:网络监控磁盘要扩容 测试环境: CPU 内存 系统盘 共享盘 网卡 2核 2G 20G 20G 桥接 系统版本:FreeNAS-9.10 一.配置虚拟机 ...
- 在ubuntu16.10 PHP测试连接MySQL中出现Call to undefined function: mysql_connect()
1.问题: 测试php7.0 链接mysql数据库的时候发生错误: Fatal error: Uncaught Error: Call to undefined function mysqli_con ...
- Delphi Berlin 10.1 for 小米平板2 (Win 10) 电子罗盘测试
Windows 10 下没有 Sensor.HeadingXSensor.HeadingYSensor.HeadingZ 需改用 Sensor.CompMagHeading
- 2015.10.14-TransactionScope测试
测试代码: ; ; List<string> lst = null; Action doSth = () => { using (var db = new TestSystemEnt ...
- 【10.10校内测试】【线段树维护第k小+删除】【lca+主席树维护前驱后驱】
贪心思想.将a排序后,对于每一个a,找到对应的删除m个后最小的b,每次更新答案即可. 如何删除才是合法并且最优的?首先,对于排了序的a,第$i$个那么之前就应该删除前$i-1$个a对应的b.剩下$m- ...
- win10序列号 2019年10月测试
win10序列号 N3415-266GF-AH13H-WA3UE-5HBT4 win10序列号 NPK3G-4Q81M-X4A61-D553L-NV68D win10序列号 N617H-84K11-6 ...
- TestNg 10. 多线程测试-xml文件实现
代码如下: package com.course.testng.multiThread; import org.testng.annotations.Test; public class MultiT ...
随机推荐
- ML 徒手系列 最大似然估计
1.最大似然估计数学定义: 假设总体分布为f(x,θ),X1,X2...Xn为总体采样得到的样本.其中X1,X2...Xn独立同分布,可求得样本的联合概率密度函数为: 其中θ是需要求得的未知量,xi是 ...
- 值得细细品读的URL资源
Web安全渗透方面的资源指南: 知乎综合问答:https://www.zhihu.com/question/21914899 渗透测试经典演练系统:http://www.freebuf.com/sec ...
- ssh的两种连接方法(包括无密码访问)
一.正常连接方法:ssh root@10.0.0.20 二.无密码连接方法(有两台机器:此处我把被连接的称为服务器,另一台则称为客户端): 1.先在服务器添加目录 .ssh: mkdir .ssh ...
- 《Spring实战》系列之Bean的装配-Days02
2.1 回顾 对于我第一天在bean的装配中写的,是一些基本的语法或者是Spring本身的一些规定,但是我没有对此进行深究.接下来就让我们仔细的讨论一下细节问题.和传统的类的定义和方法的调用做一些比较 ...
- oracle 新增字段
alter table shop_procategory add (category_ORDER_FIELD number(10) default '0' not null);comment on c ...
- jquery选择器中的find和空格,children和>的区别、及父节点兄弟节点,还有判断是否存在的写法
一.find和空格,children和>及其它的区别 空格:$('parent childchild')表示获取parent下的所有的childchild节点(所有的子孙). 等效成 = ...
- 第四周作业-视频学习、教材作业wireshark
教材总结与作业 总结 网络嗅探技术定义:网络嗅探(sniff)是一种黑客常用的窃听技术,(1)利用计算机的网络接口截获目的地为其他计算机的数据报文,(2)监听数据流中所包含的用户账户密码或私密通信等. ...
- c#事件1
Private void button_clicked( object sender ,RouteEventArgs e) sender :引发事件的对象 源 e : 路由事件,提供可能重要 ...
- C++基础之多态性和动态联编
(1)多态性是指相同的函数名对应不同的实现.多态性采用两种方式:重载方式和覆盖方式.重载方式表现在函数重载和运算符重载:覆盖方式表现在基类与派生类中相同说明的函数.(2)函数重载要求被重载的函数应该在 ...
- Java中Method.invoke方法,反射?
正常来说,我们调用对象的方法是通过dot运算符来进行的,这里我们介绍另一种方法,有以下几个步骤:1,获取该类的Class Type:2,通过getMethod方法获取Method对象:3,通过调用in ...