1.DisallowConcurrentExceution

从字面意思来看也就是不允许并发执行

简单的演示一下

    [DisallowConcurrentExecution]
public class TestDisallowConcurrentExectionJob : IJob
{
public async Task Execute(IJobExecutionContext context)
{
await Task.Run(() =>
{ var nextTime = context.NextFireTimeUtc?.ToLocalTime(); var currentTime = DateTime.Now; Console.WriteLine($"CurrentTime={currentTime}, FireTime={context.ScheduledFireTimeUtc?.ToLocalTime()}, NextTime={nextTime}"); }); Thread.Sleep(); }
} public class TestDisallowConcurrentExectionScheduler
{
public async static Task Test()
{
var scheduler = await StdSchedulerFactory.GetDefaultScheduler(); await scheduler.Start(); var job = JobBuilder.CreateForAsync<TestDisallowConcurrentExectionJob>()
.WithIdentity("TestDisallowConcurrentExectionJob")
.UsingJobData("myKey", "myValue")
.Build(); var trigger = TriggerBuilder.Create()
.WithSimpleSchedule(s =>
s.WithIntervalInSeconds()
.RepeatForever())
.Build(); await scheduler.ScheduleJob(job, trigger);
}
}

未添加特性的结果

 添加特性的结果

 Quartz默认是十个线程工作线程加一个调度线程,当线程在等待时,其他工作线程也会进来,而加上DiallowConcurrentExection特性时则都会处于等待状态

原理图

源码解析

通过QuartzSchedulerThread中的Run方法调用AcquireNextTriggers获取下一次的触发器

        public virtual Task<IReadOnlyCollection<IOperableTrigger>> AcquireNextTriggers(
DateTimeOffset noLaterThan,
int maxCount,
TimeSpan timeWindow,
CancellationToken cancellationToken = default)
{
lock (lockObject)
{
var result = new List<IOperableTrigger>(); // return empty list if store has no triggers.
if (timeTriggers.Count == )
{
return Task.FromResult<IReadOnlyCollection<IOperableTrigger>>(result);
} var acquiredJobKeysForNoConcurrentExec = new HashSet<JobKey>();
var excludedTriggers = new HashSet<TriggerWrapper>();
DateTimeOffset batchEnd = noLaterThan; while (true)
{
var tw = timeTriggers.FirstOrDefault();
if (tw == null)
{
break;
}
if (!timeTriggers.Remove(tw))
{
break;
} if (tw.Trigger.GetNextFireTimeUtc() == null)
{
continue;
} if (ApplyMisfire(tw))
{
if (tw.Trigger.GetNextFireTimeUtc() != null)
{
timeTriggers.Add(tw);
}
continue;
} if (tw.Trigger.GetNextFireTimeUtc() > batchEnd)
{
timeTriggers.Add(tw);
break;
} // If trigger's job is set as @DisallowConcurrentExecution, and it has already been added to result, then
// put it back into the timeTriggers set and continue to search for next trigger.
JobKey jobKey = tw.Trigger.JobKey;
IJobDetail job = jobsByKey[jobKey].JobDetail;
if (job.ConcurrentExecutionDisallowed)
{
if (!acquiredJobKeysForNoConcurrentExec.Add(jobKey))
{
excludedTriggers.Add(tw);
continue; // go to next trigger in store.
}
} tw.state = InternalTriggerState.Acquired;
tw.Trigger.FireInstanceId = GetFiredTriggerRecordId();
IOperableTrigger trig = (IOperableTrigger) tw.Trigger.Clone(); if (result.Count == )
{
var now = SystemTime.UtcNow();
var nextFireTime = tw.Trigger.GetNextFireTimeUtc().GetValueOrDefault(DateTimeOffset.MinValue);
var max = now > nextFireTime ? now : nextFireTime; batchEnd = max + timeWindow;
} result.Add(trig); if (result.Count == maxCount)
{
break;
}
} // If we did excluded triggers to prevent ACQUIRE state due to DisallowConcurrentExecution, we need to add them back to store.
if (excludedTriggers.Count > )
{
foreach (var excludedTrigger in excludedTriggers)
{
timeTriggers.Add(excludedTrigger);
}
}
return Task.FromResult<IReadOnlyCollection<IOperableTrigger>>(result);
}
}

RAMJobStore中的TriggersFired方法

当添加了DisallowConcurrentExecution特性后

先从TimeTriggers中移除Trigger

再把Job添加BlockedJobs中

                    if (job.ConcurrentExecutionDisallowed)
{
IEnumerable<TriggerWrapper> trigs = GetTriggerWrappersForJob(job.Key);
foreach (TriggerWrapper ttw in trigs)
{
if (ttw.state == InternalTriggerState.Waiting)
{
ttw.state = InternalTriggerState.Blocked;
}
if (ttw.state == InternalTriggerState.Paused)
{
ttw.state = InternalTriggerState.PausedAndBlocked;
}
timeTriggers.Remove(ttw);
}
blockedJobs.Add(job.Key);
}

RAMJobStore中的TriggeredJobComplete方法

当Job执行完后

如果添加了DisallowConcurrentExecution特性

先移除BlockedJobs中Job 再把Trigger添加到TimeTriggers中

  if (jd.ConcurrentExecutionDisallowed)
{
blockedJobs.Remove(jd.Key);
IEnumerable<TriggerWrapper> trigs = GetTriggerWrappersForJob(jd.Key);
foreach (TriggerWrapper ttw in trigs)
{
if (ttw.state == InternalTriggerState.Blocked)
{
ttw.state = InternalTriggerState.Waiting;
timeTriggers.Add(ttw);
}
if (ttw.state == InternalTriggerState.PausedAndBlocked)
{
ttw.state = InternalTriggerState.Paused;
}
} signaler.SignalSchedulingChange(null, cancellationToken);
}

2.PersistJobDataAfterExecution

从字面意思来看也就是执行后保留作业数据

简单演示一下

 [PersistJobDataAfterExecution]
public class TestPersistJobDataAfterExecutionJob : IJob
{
public async Task Execute(IJobExecutionContext context)
{
await Task.Run(() =>
{ var myVaule = context.JobDetail.JobDataMap["myKey"]; Console.WriteLine(myVaule); context.JobDetail.JobDataMap["myKey"] = myVaule + new Random().Next(,).ToString();
});
}
} public class TestPersistJobDataAfterExcutionScheduler
{
public async static Task Test()
{
var scheduler = await StdSchedulerFactory.GetDefaultScheduler(); await scheduler.Start(); var job = JobBuilder.CreateForAsync<TestPersistJobDataAfterExecutionJob>()
.WithIdentity("TestPersistJobDataAfterExcutionJob")
.UsingJobData("myKey", "myValue")
.Build(); var trigger = TriggerBuilder.Create()
.WithSimpleSchedule(s =>
s.WithIntervalInSeconds()
.RepeatForever())
.Build(); await scheduler.ScheduleJob(job, trigger);
}
}

未加特性的结果

加特性的结果

原理图

源码解析

QuartzSchedulerThread中的Run方法

                                JobRunShell shell;
try
{
shell = qsRsrcs.JobRunShellFactory.CreateJobRunShell(bndle);
await shell.Initialize(qs, CancellationToken.None).ConfigureAwait(false);
}
catch (SchedulerException)
{
await qsRsrcs.JobStore.TriggeredJobComplete(trigger, bndle.JobDetail, SchedulerInstruction.SetAllJobTriggersError, CancellationToken.None).ConfigureAwait(false);
continue;
} var threadPoolRunResult = qsRsrcs.ThreadPool.RunInThread(() => shell.Run(CancellationToken.None));
if (threadPoolRunResult == false)
{
// this case should never happen, as it is indicative of the
// scheduler being shutdown or a bug in the thread pool or
// a thread pool being used concurrently - which the docs
// say not to do...
Log.Error("ThreadPool.RunInThread() returned false!");
await qsRsrcs.JobStore.TriggeredJobComplete(trigger, bndle.JobDetail, SchedulerInstruction.SetAllJobTriggersError, CancellationToken.None).ConfigureAwait(false);
}

JobRunShell初始化方法

 public virtual async Task Initialize(
QuartzScheduler sched,
CancellationToken cancellationToken = default)
{
qs = sched; IJob job;
IJobDetail jobDetail = firedTriggerBundle.JobDetail; try
{
job = sched.JobFactory.NewJob(firedTriggerBundle, scheduler);
}
catch (SchedulerException se)
{
await sched.NotifySchedulerListenersError($"An error occurred instantiating job to be executed. job= '{jobDetail.Key}'", se, cancellationToken).ConfigureAwait(false);
throw;
}
catch (Exception e)
{
SchedulerException se = new SchedulerException($"Problem instantiating type '{jobDetail.JobType.FullName}'", e);
await sched.NotifySchedulerListenersError($"An error occurred instantiating job to be executed. job= '{jobDetail.Key}'", se, cancellationToken).ConfigureAwait(false);
throw se;
} jec = new JobExecutionContextImpl(scheduler, firedTriggerBundle, job);
}

SimpleJobFactory中的NewJob函数可以看出Job是无状态的直接通过反射创建的

    public virtual IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
{
IJobDetail jobDetail = bundle.JobDetail;
Type jobType = jobDetail.JobType;
try
{
if (log.IsDebugEnabled())
{
log.Debug($"Producing instance of Job '{jobDetail.Key}', class={jobType.FullName}");
} return ObjectUtils.InstantiateType<IJob>(jobType);
}
catch (Exception e)
{
SchedulerException se = new SchedulerException($"Problem instantiating class '{jobDetail.JobType.FullName}'", e);
throw se;
}
}

JobRunShell中Run方法将JobExecutionContextImpl塞给了Execute方法

                   private JobExecutionContextImpl jec;

                   // Execute the job
try
{
if (log.IsDebugEnabled())
{
log.Debug("Calling Execute on job " + jobDetail.Key);
} await job.Execute(jec).ConfigureAwait(false); endTime = SystemTime.UtcNow();
}

JobRunShell中Run方法调用的NotifyJobStoreJobComplete函数

await qs.NotifyJobStoreJobComplete(trigger, jobDetail, instCode, cancellationToken).ConfigureAwait(false);

JobRunShell中的NotifyJobStoreJobComplete 可以看出调用了JobStore.TriggeredJobComplete

 public virtual Task NotifyJobStoreJobComplete(
IOperableTrigger trigger,
IJobDetail detail,
SchedulerInstruction instCode,
CancellationToken cancellationToken = default)
{
return resources.JobStore.TriggeredJobComplete(trigger, detail, instCode, cancellationToken);
}

Quartz.NET中StdScheduleFactory如果未指定JobStore则默认RAMJobStore

从而可以看出RAMJobStore中通过TriggerJobComplete方法来检查是否有PersistJobDataAfterExecution特性

如果有通过MemberwiseClone函数克隆出数据来再通过JobBuilder来构建一个JobDetail

                  if (jobDetail.PersistJobDataAfterExecution)
{
JobDataMap newData = jobDetail.JobDataMap;
if (newData != null)
{
newData = (JobDataMap) newData.Clone();
newData.ClearDirtyFlag();
}
jd = jd.GetJobBuilder().SetJobData(newData).Build();
jw.JobDetail = jd;
}
if (jd.ConcurrentExecutionDisallowed)
{
blockedJobs.Remove(jd.Key);
IEnumerable<TriggerWrapper> trigs = GetTriggerWrappersForJob(jd.Key);
foreach (TriggerWrapper ttw in trigs)
{
if (ttw.state == InternalTriggerState.Blocked)
{
ttw.state = InternalTriggerState.Waiting;
timeTriggers.Add(ttw);
}
if (ttw.state == InternalTriggerState.PausedAndBlocked)
{
ttw.state = InternalTriggerState.Paused;
}
} signaler.SignalSchedulingChange(null, cancellationToken);
}

最终会通过JobRunShell中的Run方法中的ReturnJob方法 返回Job

                qs.RemoveInternalSchedulerListener(this);
if (jec != null)
{
if (jec.JobInstance != null)
{
qs.JobFactory.ReturnJob(jec.JobInstance);
} jec.Dispose();
}
        public virtual void ReturnJob(IJob job)
{
var disposable = job as IDisposable;
disposable?.Dispose();
}

Quartz.Net系列(十四):详解Job中两大特性(DisallowConcurrentExecution、PersistJobDataAfterExecution)的更多相关文章

  1. java基础(十四)-----详解匿名内部类——Java高级开发必须懂的

    在这篇博客中你可以了解到匿名内部类的使用.匿名内部类要注意的事项.匿名内部类使用的形参为何要为final. 使用匿名内部类内部类 匿名内部类由于没有名字,所以它的创建方式有点儿奇怪.创建格式如下: n ...

  2. Android笔记(七十四) 详解Intent

    我们最常使用Intent来实现Activity之间的转跳,最近做一个app用到从系统搜索图片的功能,使用到了intent的 setType 方法和 setAction 方法,网上搜索一番,发现实现转跳 ...

  3. 广告行业中那些趣事系列8:详解BERT中分类器源码

    最新最全的文章请关注我的微信公众号:数据拾光者. 摘要:BERT是近几年NLP领域中具有里程碑意义的存在.因为效果好和应用范围广所以被广泛应用于科学研究和工程项目中.广告系列中前几篇文章有从理论的方面 ...

  4. 爬虫系列 | 6、详解爬虫中BeautifulSoup4的用法

    bs4,全称BeautifulSoup 4 , 它是Python独有的一种解析方式.也就是说只有Python语言才可以通过这种方式去解析数据. BeautifulSoup 3 只支持Python2,所 ...

  5. 详解Redis中两种持久化机制RDB和AOF(面试常问,工作常用)

    redis是一个内存数据库,数据保存在内存中,但是我们都知道内存的数据变化是很快的,也容易发生丢失.幸好Redis还为我们提供了持久化的机制,分别是RDB(Redis DataBase)和AOF(Ap ...

  6. 详解Redis中两种持久化机制RDB和AOF

    redis是一个内存数据库,数据保存在内存中,但是我们都知道内存的数据变化是很快的,也容易发生丢失.幸好Redis还为我们提供了持久化的机制,分别是RDB(Redis DataBase)和AOF(Ap ...

  7. “全栈2019”Java第八十四章:接口中嵌套接口详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  8. Hexo系列(二) 配置文件详解

    Hexo 是一款优秀的博客框架,在使用 Hexo 搭建一个属于自己的博客网站后,我们还需要对其进行配置,使得 Hexo 更能满足自己的需求 这里所说的配置文件,是位于站点根目录下的 _config.y ...

  9. 十图详解tensorflow数据读取机制(附代码)转知乎

    十图详解tensorflow数据读取机制(附代码) - 何之源的文章 - 知乎 https://zhuanlan.zhihu.com/p/27238630

随机推荐

  1. Linux - Python的虚拟环境配置的坑 virtualenv: error: unrecognized arguments: --no-site-packages

    如果你在CentOS8下面配置虚拟环境时,遇到如下错误: [root@localhost ~]# mkvirtualenv my_django usage: virtualenv [--version ...

  2. 《Java并发编程的艺术》第10章 Executor框架

    Java的线程既是工作单元,也是执行机制.从JDK5开始,把工作单元与执行机制分离开来.工作单元包括Runnable和Callable,执行机制由Executor框架提供. 10.1 Executor ...

  3. PIP设置镜像源

    PIP设置镜像源 pip安装Python包时候,默认是国外的下载源,速度太慢,本文介绍几种设置pip国内镜像源的方法 镜像源 阿里云 http://mirrors.aliyun.com/pypi/si ...

  4. java之FTP上传下载

    import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import ...

  5. intellij配置github

    一.在IDEA中设置Git,在File-->Setting->Version Control-->Git-->Path to Git executable选择你的git安装后的 ...

  6. 黎活明8天快速掌握android视频教程--18_在SQLite中使用事务

    1 所谓的事业就是一系列的操作 比如:执行转账操作:将personid=1的账户转账10元到personid=2的账号中 所以的一系列操作就是:personid=1的账户钱要减少10元 personi ...

  7. Python3-cx_Oracle模块-数据库操作之Oracle

    模块安装 1.安装cx_Oracle模块之前必须要安装Oracle客户端,否则无法使用 2.系统上需要装有对应版本的c++编译套件(Linux下:g++ Windows下:VC++) 参考文档 htt ...

  8. 【SpringBoot MQ 系列】RabbitListener 消费基本使用姿势介绍

    [MQ 系列]RabbitListener 消费基本使用姿势介绍 之前介绍了 rabbitmq 的消息发送姿势,既然有发送,当然就得有消费者,在 SpringBoot 环境下,消费可以说比较简单了,借 ...

  9. Tensorflow入门学习笔记汇总

    一.环境准备 1.安装python:下载地址https://www.python.org/downloads/windows/下载并安装(推荐python3) 2.安装对应python版本的库:htt ...

  10. 计算机网络之HTTPS协议

    • HTTPS协议是以安全为目的的HTTP通道,比单纯的HTTP协议更安全,相当于HTTP的升级版.• HTTPS的安全基础为SSL,就是在HTTP下加入SSL层,意思是HTTPS通过安全传输机制进行 ...