Netty 自定义任务

任务队列中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();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值