任务队列典型应用场景
- 用户自定义普通任务
- 用户自定义定时任务
- 推送系统
- 异步任务
自定义普通任务
ctx.channel().eventLoop().execute(()->{
try {
Thread.sleep(5000);
ctx.writeAndFlush(Unpooled.copiedBuffer("hello,客户端2",CharsetUtil.UTF_8));
} catch (InterruptedException e) {
e.printStackTrace();
}
});
重写一个Runnable方法
定时任务Schedule
ctx.channel().eventLoop().schedule(()->{
ctx.writeAndFlush(Unpooled.copiedBuffer("hello,客户端2",CharsetUtil.UTF_8));
},5, TimeUnit.SECONDS);
异步模型
例如,可以给前面创建的channelFuture对象添加完成监听器
ChannelFuture future = bootstrap.bind(6668).sync();
future.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture channelFuture) throws Exception {
if(channelFuture.isSuccess()){
System.out.println("监听成功");
}else{
System.out.println("监听失败");
}
}
});