Netty(6)关闭
客户端:
public static void main(String[] args) throws Exception {
final SslContext sslCtx;
if (SSL) {
sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
} else {
sslCtx = null;
}
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
if (sslCtx != null) {
p.addLast(sslCtx.newHandler(ch.alloc(),HOST,PORT));
}
p.addLast(new DiscardClientHandler());
}
});
//make the connection attempt.
ChannelFuture f = b.connect(HOST,PORT).sync();
//wait until the connection is closed.
f.channel().closeFuture().sync();
log.info("connection is closed");
} finally {
group.shutdownGracefully();
}
}
或者
public static void main(String[] args) throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.handler(new TelnetClientInitializer()); Channel ch = b.connect(HOST, PORT).channel();
//read commands from the stdbin.
ChannelFuture lastWriteFuture = null;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
for(;;) {
String line = in.readLine();
if (line == null) {
break;
}
//send the received line to the server
lastWriteFuture = ch.writeAndFlush(line +"\r\n");
//if user typed the 'bye' command,wait unitl the server closes the connection.
if ("bye".equals(line.toLowerCase())) {
ch.closeFuture().sync();
break;
}
}
//wait unitl all messages are flushed before closing the channel.
if (lastWriteFuture != null) {
lastWriteFuture.sync();
}
} finally {
group.shutdownGracefully();
}
}
或者
@Override
protected void channelRead0(ChannelHandlerContext ctx, BigInteger msg) throws Exception {
receivedMessages ++;
if (receivedMessages == FactorialClient.COUNT-start+1) {
//offer(放入)the answer after closing the connection
ctx.channel().close().addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
boolean offered = answer.offer(msg);
log.info("offer the answer result:{}",offered);
}
});
}
}
服务端:
1、继承SimpleChannelInboundHandler或ChannelInboundHandlerAdapter的server端
@Override
protected void channelRead0(ChannelHandlerContext ctx, String request) throws Exception {
//Generate and write a response
String response;
boolean close = false;
if (request.isEmpty()) {
response = "Please type something.\r\n";
} else if("bye".equals(request.toLowerCase())) {
response = "Have a good day!\r\n";
close = true;
} else {
response = "Did you say '"+request+"'?\r\n";
}
//不需要write ByteBuf,只需write string,因为传递给StringEncoder
ChannelFuture future = ctx.write(response);
if (close) {
future.addListener(ChannelFutureListener.CLOSE);
}
}
如果是短链接,必须在服务端关闭该channel。此时,才能通知到客户端的chanel.future.close()方法。
2、需要解码器(decoder)的server端
在serverHandler类中的channelRead方法中,无需加入future.addListener(ChannelFutureListener.CLOSE);如下,
@Override
protected void channelRead0(ChannelHandlerContext ctx, BigInteger msg) throws Exception {
//计算阶乘并发送到客户端
lastMultiplier = msg;
factorial = factorial.multiply(msg);
ctx.writeAndFlush(factorial);
}
因为,ByteToMessageDecoder类中,已执行了关闭,如下
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
channelInputClosed(ctx, true);
}
Netty(6)关闭的更多相关文章
- Netty关闭连接流程分析
在实际场景中,使用Netty4来实现RPC框架,服务端一般会验证协议,最简单的方法的协议探测,判断魔数是否正确.如果服务端无法识别协议会立即抛出异常,并主动关闭连接,此时客户端会收到read信号,在发 ...
- 我为 Netty 贡献源码 | 且看 Netty 如何应对 TCP 连接的正常关闭,异常关闭,半关闭场景
欢迎关注公众号:bin的技术小屋,本文图片加载不出来的话可查看公众号原文 本系列Netty源码解析文章基于 4.1.56.Final版本 写在前面..... 本文是笔者肉眼盯 Bug 系列的第三弹,前 ...
- Netty系列之Netty可靠性分析
作者 李林锋 发布于 2014年6月19日 | 29 讨论 分享到:微博微信FacebookTwitter有道云笔记邮件分享 稍后阅读 我的阅读清单 1. 背景 1.1. 宕机的代价 1.1. ...
- [编织消息框架][netty源码分析]4 eventLoop 实现类NioEventLoop职责与实现
NioEventLoop 是jdk nio多路处理实现同修复jdk nio的bug 1.NioEventLoop继承SingleThreadEventLoop 重用单线程处理 2.NioEventLo ...
- Netty高可靠性设计:优化建议
尽管Netty的可靠性已经做得非常出色,但是在生产实践中还是发现了一些待优化点,本小节将进行简单说明.希望后续的版本中可以解决,当然用户也可以根据自己的实际需要决定自行优化. 1 发送队列容量上限控 ...
- Netty:option和childOption参数设置说明
Channel配置参数 (1).通用参数 CONNECT_TIMEOUT_MILLIS : Netty参数,连接超时毫秒数,默认值30000毫秒即30秒. MAX_MESSAGES_PER_REA ...
- Netty Bootstrap(图解)|秒懂
目录 Netty Bootstrap(图解) 源码工程 写在前面 图解几个重要概念 父子 channel EventLoop 线程与线程组 通道与Reactor线程组 Channel 通道的类型 启动 ...
- netty可靠性
Netty的可靠性 首先,我们要从Netty的主要用途来分析它的可靠性,Netty目前的主流用法有三种: 1) 构建RPC调用的基础通信组件,提供跨节点的远程服务调用能力: 2) NIO通信框架,用于 ...
- Netty系列之Netty可靠性分析--转载
原文地址:http://www.infoq.com/cn/articles/netty-reliability 1. 背景 1.1. 宕机的代价 1.1.1. 电信行业 毕马威国际(KPMG Inte ...
- netty服务端的创建
服务端的创建 示例代码 netty源码中有一个netty-example项目,不妨以经典的EchoServer作为楔子. // 步骤1 EventLoopGroup bossGroup = new N ...
随机推荐
- Trustzone——利用硬件对数据加密,秘钥存在芯片里
我是看 https://zhuanlan.zhihu.com/p/26441212 这个文章知道trustzone自身会存储秘钥,这个秘钥可以来自用户指纹,也可以来自云端下发的key. Truztzo ...
- kettle 设置变量
以下只是本人在使用过程中一些经验,可能有误解不对的地方,希望大家指正. 这个控件可以在job中调用,也可以在transformation中使用.下面将分别说明在两个不同任务中调用时的使用方法和需要注意 ...
- NCEE2018游记
前言 悠闲的高中生活结束啦.俺たちの戦いはこれからだ!(无误) Day0 看考场 听考前教育,前面还挺常规,后面讲了半个多小时相关法律,听了几句后实在没兴趣了,开始瞎想.那个人连续读了近一个小时也不嫌 ...
- 洛谷P2530 [SHOI2001]化工厂装箱员
题目描述 118号工厂是世界唯一秘密提炼锎的化工厂,由于提炼锎的难度非常高,技术不是十分完善,所以工厂生产的锎成品可能会有3种不同的纯度,A:100%,B:1%,C:0.01%,为了出售方便,必须把不 ...
- Jenkins安装配置简单使用
安装启动是十分简单的,直接去https://jenkins.io/download/下载对应的rpm包就好了,需要注意的是我们的机器上要提前有java环境,相对应要选择你java环境可以支持的jenk ...
- java 基础知识学习 priorityQueue
ArrayList:动态扩容(相对于数组),数组实现查询非常快但要求连续内存空间. 双向队列LinkedList:不需要像ArrayList一样创建连续的内存空间,它以链表的形式连接各个节点,但是 ...
- httpd服务相关实验
实验环境: CentOS6.8 1.连接测试: 在/etc/httpd/conf/httpd.conf telnet 172.16.252.242 80 GET /index.html HTTP/1. ...
- 最小化安装linux CentOS-6.6后 部署fastdfs +下载地址 很干很干的干货
参考:http://blog.itpub.net/7734666/viewspace-1292485/ 安装一些必要软件 yum -y install wget gcc perl mkdir ~/zy ...
- FlexPaper+SwfTools实现的在线文档功能
最近一个项目需要实现一个在线浏览文档的功能.准备使用FlexPaper配合Pdf2Swf实现. 主要需求在于: ➔ 文档页数很多,少则几百页,多则上千页. ➔ 相应的文档大小也在50MB以上. ...
- 你所不知道的html5与html中的那些事(四)——文本标签
文章简介: 关于html5相信大家早已经耳熟能详,但是他真正的意义在具体的开发中会有什么作用呢?相对于html,他又有怎样的新的定义与新理念在里面呢?为什么一些专家认为html5完全完成后 ...