Netty心跳服务
客户端
package com.cnblogs.javalouvre;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.CharsetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import static io.netty.handler.timeout.IdleState.WRITER_IDLE;
public class Client {
private ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
private static final Logger log = LoggerFactory.getLogger(Client.class);
private static final int port = 8080;
private static final String ip = "127.0.0.1";
private static final EventLoopGroup group = new NioEventLoopGroup();
public static void main(String[] args) {
new Client().connect(port, ip);
}
private void connect(final int port, final String ip) {
final Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group)
.channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, Boolean.TRUE)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
final ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new LoggingHandler());
pipeline.addLast(new IdleStateHandler(0, 5, 0)); // 写空闲5秒
pipeline.addLast(new StringDecoder());
pipeline.addLast(new StringEncoder());
pipeline.addLast(new HeartBeatRequestHandler());
}
});
try {
final ChannelFuture channelFuture = bootstrap.connect(new InetSocketAddress(ip, port)).sync();
channelFuture.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (!future.isSuccess()) {
log.error("连接服务失败");
}
}
});
channelFuture.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
executor.execute(new Runnable() {
@Override
public void run() {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("开始重连");
connect(port, ip);
}
});
}
}
}
@ChannelHandler.Sharable
class HeartBeatRequestHandler extends ChannelInboundHandlerAdapter {
private static final Logger log = LoggerFactory.getLogger(HeartBeatRequestHandler.class);
private static final ByteBuf HEARTBEAT_SEQUENCE = Unpooled.unreleasableBuffer(Unpooled.copiedBuffer("HeartBeat", CharsetUtil.UTF_8));
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof IdleStateEvent) {
final IdleStateEvent event = (IdleStateEvent) evt;
if (WRITER_IDLE == event.state()) {
ctx.writeAndFlush(HEARTBEAT_SEQUENCE.duplicate());
}
} else {
super.userEventTriggered(ctx, evt);
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
log.error(cause.getMessage());
ctx.fireExceptionCaught(cause);
}
}
服务端
package com.cnblogs.javalouvre;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.ReadTimeoutHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Server {
private static final Logger log = LoggerFactory.getLogger(Server.class);
private static final int port = 8080;
private void bind(final int port) {
final EventLoopGroup parent = new NioEventLoopGroup();
final EventLoopGroup child = new NioEventLoopGroup();
final ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(parent, child)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100)
.childOption(ChannelOption.SO_KEEPALIVE, Boolean.TRUE)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new LoggingHandler());
ch.pipeline().addLast(new ReadTimeoutHandler(10)); // 10秒未读取到消息,判定读超时
ch.pipeline().addLast(new StringDecoder());
ch.pipeline().addLast(new StringEncoder());
}
});
try {
final ChannelFuture channelFuture = bootstrap.bind(port).sync();
channelFuture.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (!future.isSuccess()) {
log.error("服务启动失败");
}
}
});
channelFuture.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
child.shutdownGracefully();
parent.shutdownGracefully();
}
}
public static void main(String[] args) {
new Server().bind(port);
}
}
-----------------------------------------------------------------------------------------------------------
薔薇猛虎皆成個性,陽光雨露俱是天恩!
薔薇猛虎皆成個性,陽光雨露俱是天恩!