10 - EmbeddedChannel-测试ChannelHandler链
2024-09-01 16:54:28
方法 | 职责 |
---|---|
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);
}
}
最新文章
- 物联网框架SuperIO 2.2.9和ServerSuperIO 2.1同时更新,更适用于类似西门子s7-200发送多次数据,才能读取数据的情况
- Delphi DLL的创建、静态及动态调用
- HDU 1712 分组背包
- Lua小技巧
- Sum of Left Leaves
- FK JavaScript:ArcGIS JavaScript类库加载不成功而导致的程序异常
- 【HAOI2006】【BZOJ1051】【p1233】最受欢迎的牛
- SVN理解
- 【Android - 框架】之XBanner的使用
- 给tcpdump加点颜色看看
- C C++ OC iOS面试重点问题(一)
- centos 添加epel、remi仓库和ELRepo仓库
- codeforces #550E Brackets in Implications 结构体
- mysql c connector 多条sql语句执行示例
- ABP入门系列之1——ABP总体介绍
- uploadify上传图片的使用
- Linux期中架构
- Android dp、dip、dpi、px、sp简介及相关换算,及其应用实例
- ";Native table &#39;performance_schema&#39;.&#39;session_variables&#39; has the wrong structure";) [SQL: ";SHOW VARIABLES LIKE &#39;sql_mode&#39;";]
- etcd 集群运维实践
热门文章
- k8s基础 etcd参数
- 第三天的 No session 问题
- Struts2学习第七课 ActionSupport
- c++中的const关键字的理解
- 历届试题_log大侠
- 微信小程序小结(2) ------ 自定义组件
- Codeforces Round #523 (Div. 2)D(二分,多重集)
- SnapKit swift实现高度自适应的新浪微博布局
- P2742 【模板】二维凸包 / [USACO5.1]圈奶牛Fencing the Cows
- express-http-proxy 的基础使用