繁体   English   中英

Quartz.net CancellationToken

[英]Quartz.net CancellationToken

在我的调度程序中,用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并抛出异常。 但它不起作用。

根据文档,您应该使用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 ,石英将终止作业。

编辑

根据源代码(第 2151 行), Interrupt方法应用作业执行上下文的取消标记。 因此,最好使用图书馆的设施。

这是 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。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM