use of com.github.jvm.io.protocol.c2d.heart.PingHandler in project jdepth by Crab2died.
the class C2DClient method connect.
public void connect(String remoteHost, int remotePort, String localeHost, int localPort) throws InterruptedException {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap().group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, Boolean.TRUE).handler(new LoggingHandler(LogLevel.WARN)).handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new C2DHessianMsgDecoder(1024 * 1024, 4, 4, -8, 0)).addLast("MessageEncoder", new C2DHessianMsgEncoder()).addLast("ReadTimeoutHandler", new ReadTimeoutHandler(TIME_OUT)).addLast("LoginAuthReq", new LoginAuthReqHandler()).addLast("Ping", new PingHandler());
}
});
// 发起异步连接操作
ChannelFuture future = bootstrap.connect(new InetSocketAddress(remoteHost, remotePort), new InetSocketAddress(localeHost, localPort)).sync();
// 当对应的channel关闭的时候,就会返回对应的channel。
future.channel().closeFuture().sync();
} finally {
group.shutdownGracefully();
// 所有资源释放完成之后,清空资源,再次发起重连操作
executor.execute(() -> {
try {
TimeUnit.SECONDS.sleep(5);
try {
// 发起重连操作
connect(REMOTE_HOST, REMOTE_PORT, LOCAL_HOST, LOCAL_PORT);
} catch (Exception e) {
logger.error("reconnect error", e);
}
} catch (InterruptedException e) {
logger.error("InterruptedException", e);
}
});
}
}
Aggregations