netty 心跳与IdleStateHandler与断线重连
2016-08-15 16:22
609 查看
netty的心跳机制只是触发事件,但没有包心跳包,需要自己发心跳包
IdleStateHandler不是发心跳包,而是触发心跳机制,在你设定的时候内没有收到包,就触发读心跳,没有发包就触发写心跳,如果都没有,就触发all
参考
http://blog.csdn.net/albertfly/article/details/52182248
Netty Client重连实现
当我们用Netty实现一个TCP client时,我们当然希望当连接断掉的时候Netty能够自动重连。
Netty Client有两种情况下需要重连:
Netty Client启动的时候需要重连
在程序运行中连接断掉需要重连。
对于第一种情况,Netty的作者在stackoverflow上给出了 解决方案,
对于第二种情况,Netty的例子uptime中实现了一种 解决方案。
而Thomas在他的 文章中提供了这两种方式的实现的例子。
实现ChannelFutureListener 用来启动时监测是否连接成功,不成功的话重试:
public class Client
{
private EventLoopGroup loop = new NioEventLoopGroup();
public static void main( String[] args )
{
new Client().run();
}
public Bootstrap createBootstrap(Bootstrap bootstrap, EventLoopGroup eventLoop) {
if (bootstrap != null) {
final MyInboundHandler handler = new MyInboundHandler(this);
bootstrap.group(eventLoop);
bootstrap.channel(NioSocketChannel.class);
bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
bootstrap.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(handler);
}
});
bootstrap.remoteAddress("localhost", 8888);
bootstrap.connect().addListener(new ConnectionListener(this));
}
return bootstrap;
}
public void run() {
createBootstrap(new Bootstrap(), loop);
}
}
ConnectionListener 负责重连:
public class ConnectionListener implements ChannelFutureListener {
private Client client;
public ConnectionListener(Client client) {
this.client = client;
}
@Override
public void operationComplete(ChannelFuture channelFuture) throws Exception {
if (!channelFuture.isSuccess()) {
System.out.println("Reconnect");
final EventLoop loop = channelFuture.channel().eventLoop();
loop.schedule(new Runnable() {
@Override
public void run() {
client.createBootstrap(new Bootstrap(), loop);
}
}, 1L, TimeUnit.SECONDS);
}
}
}
同样在ChannelHandler监测连接是否断掉,断掉的话也要重连:
public class MyInboundHandler extends SimpleChannelInboundHandler {
private Client client;
public MyInboundHandler(Client client) {
this.client = client;
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
final EventLoop eventLoop = ctx.channel().eventLoop();
eventLoop.schedule(new Runnable() {
@Override
public void run() {
client.createBootstrap(new Bootstrap(), eventLoop);
}
}, 1L, TimeUnit.SECONDS);
super.channelInactive(ctx);
}
}
IdleStateHandler不是发心跳包,而是触发心跳机制,在你设定的时候内没有收到包,就触发读心跳,没有发包就触发写心跳,如果都没有,就触发all
参考
http://blog.csdn.net/albertfly/article/details/52182248
Netty Client重连实现
当我们用Netty实现一个TCP client时,我们当然希望当连接断掉的时候Netty能够自动重连。
Netty Client有两种情况下需要重连:
Netty Client启动的时候需要重连
在程序运行中连接断掉需要重连。
对于第一种情况,Netty的作者在stackoverflow上给出了 解决方案,
对于第二种情况,Netty的例子uptime中实现了一种 解决方案。
而Thomas在他的 文章中提供了这两种方式的实现的例子。
实现ChannelFutureListener 用来启动时监测是否连接成功,不成功的话重试:
public class Client
{
private EventLoopGroup loop = new NioEventLoopGroup();
public static void main( String[] args )
{
new Client().run();
}
public Bootstrap createBootstrap(Bootstrap bootstrap, EventLoopGroup eventLoop) {
if (bootstrap != null) {
final MyInboundHandler handler = new MyInboundHandler(this);
bootstrap.group(eventLoop);
bootstrap.channel(NioSocketChannel.class);
bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
bootstrap.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(handler);
}
});
bootstrap.remoteAddress("localhost", 8888);
bootstrap.connect().addListener(new ConnectionListener(this));
}
return bootstrap;
}
public void run() {
createBootstrap(new Bootstrap(), loop);
}
}
ConnectionListener 负责重连:
public class ConnectionListener implements ChannelFutureListener {
private Client client;
public ConnectionListener(Client client) {
this.client = client;
}
@Override
public void operationComplete(ChannelFuture channelFuture) throws Exception {
if (!channelFuture.isSuccess()) {
System.out.println("Reconnect");
final EventLoop loop = channelFuture.channel().eventLoop();
loop.schedule(new Runnable() {
@Override
public void run() {
client.createBootstrap(new Bootstrap(), loop);
}
}, 1L, TimeUnit.SECONDS);
}
}
}
同样在ChannelHandler监测连接是否断掉,断掉的话也要重连:
public class MyInboundHandler extends SimpleChannelInboundHandler {
private Client client;
public MyInboundHandler(Client client) {
this.client = client;
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
final EventLoop eventLoop = ctx.channel().eventLoop();
eventLoop.schedule(new Runnable() {
@Override
public void run() {
client.createBootstrap(new Bootstrap(), eventLoop);
}
}, 1L, TimeUnit.SECONDS);
super.channelInactive(ctx);
}
}
相关文章推荐
- VS的MFC项目生成不依赖运行时不依赖MFC的程序
- NoSuchMethodError的解决方法
- 使用C++11实现类似C#的属性概念设计
- Codeforces Round #328 (Div. 2) A(模拟) B(数学) C(数学)
- 文件类型
- cloudant文档简介
- 通过css注入实现的android webview的夜间模式
- RxJava实用指南
- BCB编程规范
- redis-不常用的命令
- Python的numpy库下的几个小函数的用法
- Java面试常见知识点总结(一)
- poi操作oracle数据库导出excel文件
- 数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历
- JavaScript 实现类似Java Map对象
- odom_out_and_back.cpp
- 数据结构实验之查找一:二叉排序树
- 转运维的兄弟分享一案例
- leetcode Trapping Rain Water
- 用纯JAVA语言编程读取MAC地址的实现