一. 耗时任务

static final EventExecutorGroup group = new DefaultEventExecutorGroup(16);

 // Tell the pipeline to run MyBusinessLogicHandler's event handler methods
// in a different thread than an I/O thread so that the I/O thread is not blocked by
// a time-consuming task.
// If your business logic is fully asynchronous or finished very quickly, you don't
// need to specify a group.
pipeline.addLast(group, "handler", new MyBusinessLogicHandler()); 其中EventExecutorGroup 就是专门来处理耗时业务的线程池。

childHandler(new ChannelInitializer<SocketChannel>() {

static final EventExecutorGroup group = new DefaultEventExecutorGroup(16);

@Override
protected void initChannel(SocketChannel ch)
throws Exception {
ChannelPipeline p = ch.pipeline();pipeline.addLast(group, "handler", new MyBusinessLogicHandler());

此方法所在类 每次都是new  ,所以会创建很多group,  所以把group 定义为static

二.执行计划

在实际生产环境中,我们可能会碰到 需要临时执行也行计划任务,,这些任务如果不耗时,我们可以通过channel提供的计划任务方法处理:

future =  channel.eventLoop.scheduleAtFixedRate(new Runnable() {

            @Override
public void run() {
//逻辑代码,非耗时任务
}
}, 6, 6, TimeUnit.HOURS);
....

如果计划任务里面的逻辑比较耗时,那么就不能再用eventLoop,因为这会阻塞IO线程。如果是通过pipeline.addLast(group, "handler", new MyBusinessLogicHandler()); 这种方式添加的业务线程我们可以使用下面的方式添加计划任务方法实现:

***future = ctx.executor().scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
}
}, 6, 6, TimeUnit.HOURS);***

...

netty 源码

public EventExecutor executor() {
return (EventExecutor)(this.executor == null?this.channel().eventLoop():this.executor);
}

如果this.executor为null,就返回channel().eventLoop(),这个是io读写线程,肯定是不能执行耗时任务的。
如果不为空,那么是怎么传进来的呢?

DefaultChannelPipeline

public final ChannelPipeline addLast(EventExecutorGroup group, String name, ChannelHandler handler) {
final AbstractChannelHandlerContext newCtx;
synchronized(this) {
checkMultiplicity(handler);
newCtx = this.newContext(group, this.filterName(name, handler), handler);
private AbstractChannelHandlerContext newContext(EventExecutorGroup group, String name, ChannelHandler handler) {
return new DefaultChannelHandlerContext(this, this.childExecutor(group), name, handler);
}

通过源码发现:其实就是我们在添加handler时指定的DefaultEventExecutorGroup。
所以结论是:如果在处理耗时任务的Handler添加时用到了DefaultEventExecutorGroup是可以 ctx.executor().scheduleAtFixedRate这么用的,但是如果你再添加handler时没有没有指定特殊的EventExecutorGroup,是不能执行耗时任务的。

如果是在IO线程,如果想处理耗时任务逻辑,那么就需要新建一个EventExecutorGroup,并调用他的相关方法


EventLoop:其本质是一个用来处理IO事件的线程,EventLoopGroup 其本质是一个线程池。一个EventLoop可以和多个Channel绑定,处理多个Channel的IO事件;但是一个Channel在整个生命周期内只会被一个EventLoop处理,这就也就保证了线程安全。

 

耗时任务DefaultEventExecutorGroup 定时任务的更多相关文章

  1. Dubbo原理解析-监控

    Dubbo发布代码中,自带了一个简易的监控中心实现.对于一般的小业务这个监控中心应该能够满足需求,对于那些大业务量的大公司一般都会有自己的监控中心,更加丰富的功能如常用的报警短信通知等等.这章讲解分析 ...

  2. Java定时任务的常用实现

    Java的定时任务有以下几种常用的实现方式: 1)Timer 2)ScheduledThreadPoolExecutor 3)Spring中集成Cron Quartz 接下来依次介绍这几类具体实现的方 ...

  3. 使用Spring的@Scheduled实现定时任务

    Spring配置文件xmlns加入xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation ...

  4. 使用Timer和ScheduledThreadPoolExecutor执行定时任务

    Java使用Timer和ScheduledThreadPoolExecutor执行定时任务 定时任务是在指定时间执行程序,或周期性执行计划任务.Java中实现定时任务的方法有很多,主要JDK自带的一些 ...

  5. 【Spring】Spring的定时任务

    > 参考的优秀文章 Task Execution and Scheduling > 版本说明 <dependencies> <dependency> <gro ...

  6. Spring 定时任务2

    转载自http://www.cnblogs.com/nick-huang/p/4864737.html > 版本说明 <dependencies> <dependency> ...

  7. Spring 定时任务1

    转载自 http://blog.csdn.net/prisonbreak_/article/details/49180307 Spring配置文件xmlns加入 xmlns:task="ht ...

  8. android自定义进度圆与定时任务

    先看代码:自定进度圆 public class ProgressCircle extends View { private Paint paint; private int strokewidth = ...

  9. [mysql] 一次sql耗时高引发报警的分析和处理

    1.现象: 最近两天在每天的凌晨0:15-20分左右收到报警短息,报警内容: JDBC-SQL请求最近三分钟内平均耗时时间过高的报警,监控类型:SQL... 2.分析: 从现象来看 每天凌晨15分,可 ...

随机推荐

  1. UESTC--1468

    题目:A Coin Problem 原题链接:http://acm.uestc.edu.cn/problem.php?pid=1468 分析:满足裴波纳契数列,打表找周期. #include<i ...

  2. python抓取

    我要抓取奥巴马每周的演讲内容http://www.putclub.com/html/radio/VOA/presidentspeech/index.html 如果手动提取,就需要一个个点进去,再复制保 ...

  3. Java质数求解

    质数概念 质数,又称素数,指在一个大于1的自然数中,除了1和此整数自身外,无法被其他自然数整除的数(也可定义为只有1和本身两个因数的数).最小的素数是2,也是素数中唯一的偶数:其他素数都是奇数.质数有 ...

  4. Hadoop基础-Protocol Buffers串行化与反串行化

    Hadoop基础-Protocol Buffers串行化与反串行化 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我们之前学习过很多种序列化文件格式,比如python中的pickl ...

  5. Liunx常用命令(Mile)

    记录一些平时经常用到的liunx命令,解决用过一段时间不用又忘记的问题.慢慢添加,持续更新~~~ 1.文件操作 a.zip.war包解压 war包 用的zip的方式压缩 ,也可以用的zip的 解压命令 ...

  6. HDU 3802 矩阵快速幂 化简递推式子 加一点点二次剩余知识

    求$G(a,b,n,p) = (a^{\frac {p-1}{2}}+1)(b^{\frac{p-1}{2}}+1)[(\sqrt{a} + \sqrt{b})^{2F_n} + (\sqrt{a} ...

  7. android 自定义TODO

    1.找到setting -> editor -> TODO 2. 正则的写法参考以前的就可以 这样我们就可以自己写一个todo了 3.TODO过滤: 4. 然后选择要展示的TODO 这里就 ...

  8. [整理]LumiSoft.Net 开源组件

    http://www.lumisoft.ee/lsWWW/download/downloads/Net/info.txt SVN: https://svn.lumisoft.ee:8443/svn/L ...

  9. AngularJS 、Backbone.js 和 Ember.js 的比较

    1 介绍 我们准备在这篇文章中比较三款流行于Web的“模型-视图-*”框架:AngularJS.Backbone和Ember.为你的项目选择正确的框架能够对你及时交付项目的能力和在以后维护你自己代码的 ...

  10. 【JSON】Ajax获得JSON字符串的处理方法

    一. success : function(data){ var caseInfoView = eval('('+data.caseInfoView+')'); for(var key in case ...