Quartz.net CancellationToken

时间:2018-01-24 17:14:49

标签: c# .net quartz-scheduler scheduler quartz.net

在我的调度程序中,使用quartz.net v3实现,我试图测试取消令牌的行为:

....
IScheduler scheduler = await factory.GetScheduler();
....
var tokenSource = new CancellationTokenSource();
CancellationToken ct = tokenSource.Token;
// Start scheduler
await scheduler.Start(ct);
// some sleep 
await Task.Delay(TimeSpan.FromSeconds(60));
// communicate cancellation
tokenSource.Cancel();

我有一个无限运行的测试作业,并且在Execute方法中检查取消令牌:

public async Task Execute(IJobExecutionContext context)
{
    while (true)
    {
        if (context.CancellationToken.IsCancellationRequested)
        {
            context.CancellationToken.ThrowIfCancellationRequested();
        }
    }
}

我希望当tokenSource.Cancel()被解雇时,作业将进入if并抛出异常。但它没有用。

2 个答案:

答案 0 :(得分:2)

根据documentation,您应该使用Interrupt方法取消Quartz个工作。

NameValueCollection props = new NameValueCollection
{
    { "quartz.serializer.type", "binary" }
};
StdSchedulerFactory factory = new StdSchedulerFactory(props);
var scheduler = await factory.GetScheduler();
await scheduler.Start();
IJobDetail job = JobBuilder.Create<HelloJob>()
    .WithIdentity("myJob", "group1")
    .Build();
ITrigger trigger = TriggerBuilder.Create()
    .WithIdentity("myTrigger", "group1")
    .StartNow()
    .WithSimpleSchedule(x => x
        .WithRepeatCount(1)
        .WithIntervalInSeconds(40))
    .Build();
await scheduler.ScheduleJob(job, trigger);
//Configure the cancellation of the schedule job with jobkey
await Task.Delay(TimeSpan.FromSeconds(1));
await scheduler.Interrupt(job.Key);

预定职位;

public class HelloJob : IJob
{
    public async Task Execute(IJobExecutionContext context)
    {
        while (true)
        {
            if (context.CancellationToken.IsCancellationRequested)
            {
                context.CancellationToken.ThrowIfCancellationRequested(); 
                // After interrupt the job, the cancellation request activated
            }
        }
    }
}

执行作业后应用scheduler.Interrupt,石英将终止作业。

修改

根据source code(第2151行),Interrupt方法应用作业执行上下文的取消令牌。因此,使用图书馆的设施可能会更好。

答案 1 :(得分:1)

这是Github Repo的单元测试:https://github.com/quartznet/quartznet/blob/master/src/Quartz.Tests.Unit/InterrubtableJobTest.cs

我尝试以同样的方式实现取消,但它对我不起作用。

@Stormcloak我必须检查取消请求,因为我想对作业进行一些中止操作,例如将状态数据写入数据库。

编辑:

因此,经过多次测试和实施。我已经跑了。

这里有一些伪代码:

    this.scheduler = await StdSchedulerFactory.GetDefaultScheduler();
    this.tokenSource = new CancellationTokenSource();
    this.token = tokenSource.Token;
    // Start scheduler.
    await this.scheduler.Start(token);
    // add some jobs here
    // ...
    // cancel running jobs.
    IReadOnlyCollection<IJobExecutionContext> jobs = await this.scheduler.GetCurrentlyExecutingJobs();
    foreach (IJobExecutionContext context in jobs)
    {
       result = await this.scheduler.Interrupt(context.JobDetail.Key, this.token);
    }
    await this.scheduler.Shutdown(true);

现在您可以在Execute方法中使用CancellationToken。