作者:arthur | 来源:互联网 | 2023-09-10 16:30
在编写
端代码的时候我有这样的需求,将接收到的数据缓存到本地,同时也要将数据推送到远端的数据库,为了不互相影响。
第一种做法就是在缓存数据的
中起一个线程去推送数据。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| public void run() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ObjectDecoder(1024 * 1024, ClassResolvers.weakCachingConcurrentResolver(this.getClass().getClassLoader())));
ch.pipeline().addLast(new ObjectEncoder());
ch.pipeline().addLast(new MyServerHandler()); // (1)
//ch.pipeline().addLast(new MyServerHandler2()); // (2)
}
})
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = b.bind(port).sync();
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
} |
1 2 3 4 5 6 7 8 9 10
| public class MyServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
//缓存到本地
new Thread(new Runnable() {
//推送到远端数据库
}).start();
}
} |
第二种做法就是使用两个
,在第一个
处理完后,调用一次
1
| ctx.fireChannelRead(msg); |
触发第二个
.
1 2
| ch.pipeline().addLast(new MyServerHandler()); // (1)
ch.pipeline().addLast(new MyServerHandler2()); // (2) |
1 2 3 4 5 6 7 8
| public class MyServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
//缓存到本地
ctx.fireChannelRead(msg);
}
} |
1 2 3 4 5 6 7
| public class MyServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
//推送到远端数据库
}
} |
目前可以看出来的是,第一种做法涉及到创建线程销毁线程,会有一定的开销,那么第二种做法又有什么不好的地方么?