任务队列中Task 典型使用场景:
- 用户程序自定义的普通任务
- 用户自定义定时任务
- 非当前Reactor 线程调用 Channel 的各种方法
例如 推送系统 的业务线程里面,根据用户标识,找到对应的 Channel 引用,然后调用 Write类方法向该用户推送消息,就会进入到这种场景。 最终的 Write 会提交到任务队列中后被异步消费
代码示例:
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
import java.util.concurrent.TimeUnit;
/**
* 1. 自定义一个Handler 需要继承Netty 规定好的某个 HandlerAdapter, 这时才能称为一个Handler
*/
public class NettyServerKeepLiveHandler extends ChannelInboundHandlerAdapter {
//
/**
* 读取数据事件(可以读取客户端发送的消息)
* @param ctx 上下文对象, 含有 管道pipeline, 通道channel, 地址
* @param msg 客户端发送的数据
* @throws Exception
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// 阻塞
// Thread.sleep(10 * 1000);
// ctx.writeAndFlush(Unpooled.copiedBuffer("hello, Netty 10s", CharsetUtil.UTF_8));
// 解决方案1 用户程序自定义的普通任务
ctx.channel().eventLoop().execute(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(10 * 1000);
ctx.writeAndFlush(Unpooled.copiedBuffer("hello, Netty 10s", CharsetUtil.UTF_8));
} catch (InterruptedException e) {
System.out.println("发生异常");
}
}
});
// 通一个线程,在10s之后 休眠 20s
ctx.channel().eventLoop().execute(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(20 * 1000);
ctx.writeAndFlush(Unpooled.copiedBuffer("hello, Netty 30s", CharsetUtil.UTF_8));
} catch (InterruptedException e) {
System.out.println("发生异常");
}
}
});
// 解决方案2 用户自定义定时任务 该任务提交到scheduleTaskQueue
ctx.channel().eventLoop().schedule(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(10 * 1000);
ctx.writeAndFlush(Unpooled.copiedBuffer("hello, Netty 10s", CharsetUtil.UTF_8));
} catch (InterruptedException e) {
System.out.println("发生异常");
}
}
},5, TimeUnit.SECONDS);
System.out.println("go on ...");
}
/**
* 读取数据完毕
* @param ctx
* @throws Exception
*/
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
// write + flush 将数据写入缓存,并刷新
ctx.writeAndFlush(Unpooled.copiedBuffer("hello, Netty", CharsetUtil.UTF_8));
}
/**
* 处理异常,一般是需要关闭通道
* @param ctx
* @param cause
* @throws Exception
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
1240

被折叠的 条评论
为什么被折叠?



