概述:
netty 最简单的丢弃服务:客户端发送消息,好,发送过去了,服.务器也收到了,但是抛弃了.
源码:
EventLoopGroup bossGroup = new NioEventLoopGroup(1); //住循环队列设置为1个
EventLoopGroup workerGroup = new NioEventLoopGroup(); //创建的事件组循环
try {
ServerBootstrap b = new ServerBootstrap(); //创建循环
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) {
ChannelPipeline p = ch.pipeline();
if (sslCtx != null) { //创建一个pipeline
p.addLast(sslCtx.newHandler(ch.alloc()));
}
p.addLast(new DiscardServerHandler());
}
});
// Bind and start to accept incoming connections.
ChannelFuture f = b.bind(PORT).sync(); //绑定和端口和同步
// Wait until the server socket is closed.
// In this example, this does not happen, but you can do that to gracefully
// shut down your server.
f.channel().closeFuture().sync(); //同步关闭channel:
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
2.实现一个hander 响应discard 服务事件:处理一个channel: 复写simpleReader0事件,和exceptionCauthg事件:
/**
* Handles a server-side channel.
*/
public class DiscardServerHandler extends SimpleChannelInboundHandler<Object> {
@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
// discard
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
// Close the connection when an exception is raised.
cause.printStackTrace();
ctx.close();
}
}
3.客户端创建hander处理:
public static void main(String[] args) throws Exception {
// Configure SSL.
final SslContext sslCtx;
if (SSL) {
sslCtx = SslContextBuilder.forClient()
.trustManager(InsecureTrustManagerFactory.INSTANCE).build();
} else {
sslCtx = null;
}
EventLoopGroup group = new NioEventLoopGroup(); //创建一个事件循环
try {
Bootstrap b = new Bootstrap(); //启动一个bootStrap
b.group(group)
.channel(NioSocketChannel.class) //配置一个chanel:
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception { //添加一个chanel 处理器:
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(); //等待连接关闭
} finally {
group.shutdownGracefully();
}
}
4.客户端对应的handler 处理:
复写的对应的chnelActivate, chanbnelIndeactivate, channelRed0,exceptionCaught.
@Override
public void channelInactive(ChannelHandlerContext ctx) {
content.release();
}
@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
// Server is supposed to send nothing, but if it sends something, discard it.
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
// Close the connection when an exception is raised.
cause.printStackTrace();
ctx.close();
}
回调一个监听器:
private void generateTraffic() {
// Flush the outbound buffer to the socket.
// Once flushed, generate the same amount of traffic again.
ctx.writeAndFlush(content.retainedDuplicate()).addListener(trafficGenerator);
}
private final ChannelFutureListener trafficGenerator = new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
if (future.isSuccess()) {
generateTraffic();
} else {
future.cause().printStackTrace();
future.channel().close();
}
}
};