耗时任务DefaultEventExecutorGroup 定时任务
一. 耗时任务
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 定时任务的更多相关文章
- Dubbo原理解析-监控
Dubbo发布代码中,自带了一个简易的监控中心实现.对于一般的小业务这个监控中心应该能够满足需求,对于那些大业务量的大公司一般都会有自己的监控中心,更加丰富的功能如常用的报警短信通知等等.这章讲解分析 ...
- Java定时任务的常用实现
Java的定时任务有以下几种常用的实现方式: 1)Timer 2)ScheduledThreadPoolExecutor 3)Spring中集成Cron Quartz 接下来依次介绍这几类具体实现的方 ...
- 使用Spring的@Scheduled实现定时任务
Spring配置文件xmlns加入xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation ...
- 使用Timer和ScheduledThreadPoolExecutor执行定时任务
Java使用Timer和ScheduledThreadPoolExecutor执行定时任务 定时任务是在指定时间执行程序,或周期性执行计划任务.Java中实现定时任务的方法有很多,主要JDK自带的一些 ...
- 【Spring】Spring的定时任务
> 参考的优秀文章 Task Execution and Scheduling > 版本说明 <dependencies> <dependency> <gro ...
- Spring 定时任务2
转载自http://www.cnblogs.com/nick-huang/p/4864737.html > 版本说明 <dependencies> <dependency> ...
- Spring 定时任务1
转载自 http://blog.csdn.net/prisonbreak_/article/details/49180307 Spring配置文件xmlns加入 xmlns:task="ht ...
- android自定义进度圆与定时任务
先看代码:自定进度圆 public class ProgressCircle extends View { private Paint paint; private int strokewidth = ...
- [mysql] 一次sql耗时高引发报警的分析和处理
1.现象: 最近两天在每天的凌晨0:15-20分左右收到报警短息,报警内容: JDBC-SQL请求最近三分钟内平均耗时时间过高的报警,监控类型:SQL... 2.分析: 从现象来看 每天凌晨15分,可 ...
随机推荐
- 如何让自己的广播只让指定的 app 接收?
1.自己的应用(假设名称为应用 A)在发送广播的时候给自己发送的广播添加自定义权限,假设权限名为:com.itheima.android.permission , 然后需要在应用 A 的 Androi ...
- loj2541【PKUWC2018】猎人杀
题解 题目中的选择条件等价于正常选择所有猎人,而如果选到已经出局的猎人就继续选: 这两种选法是一样的因为(设$W=\sum_{i=1}^{n}w_{i}$ , $X$为已经出局的猎人的$w$之和): ...
- 数据库之MySQL的介绍与使用20180703
/*******************************************************************************************/ 一.mysq ...
- 用递归的方法求一个数组的前n项和
用递归的方法求一个数组的前n项和 public class Demo1 { /* * 用递归的方法求一个数组的前n项和 */ public static void main(String[] args ...
- matlab和C语言实现最小二乘法
参考:https://blog.csdn.net/zengxiantao1994/article/details/70210662 Matlab代码: N = ; x = [ ]; y = [ ]; ...
- IOS方形头像如何变成圆形
方法一:直接使用UIView对应图层的cornerRadius self.layer.cornerRadius = CGRectGetWidth(self.bounds)/2.f; self. ...
- webpack的基础入门
webpack的基础入门 这里对于 webpack 的基础入门进行一些总结,可以参考 github 上的 webpack-demo ,链接是 https://github.com/RealAndMe/ ...
- Django中url()
使用django的时候,如果我们希望我们编写的view可以被正常访问,就需要配置url. 在django的官方文档中,url()的例子如下: polls/urls.py from django.con ...
- JS开启浏览器全屏模式,需要手动触发
<html > <meta charset="UTF-8"> <body> <button onclick="launchFul ...
- solr配置中文分词器——(十二)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAqcAAAGzCAIAAACdKClDAAAgAElEQVR4nOydd5gUxdbGx5xASZKXLB