我的工作不会在isscheduler上中断
本文关键字:中断 isscheduler 我的工作 | 更新日期: 2023-09-27 18:06:41
我正在尝试实现一个IInterruptableJob
接口,以便我可以停止我的工作。
这是我的工作实现的样子(取自https://github.com/quartznet/quartznet/blob/master/src/Quartz.Examples/example7/InterruptExample.cs)
public class HelloJob : IInterruptableJob
{
// logging services
private static readonly ILog Log = LogManager.GetLogger(typeof(HelloJob));
// has the job been interrupted?
private bool _interrupted;
// job name
private JobKey _jobKey;
/// <summary>
/// Called by the <see cref="IScheduler" /> when a <see cref="ITrigger" />
/// fires that is associated with the <see cref="IJob" />.
/// </summary>
public virtual void Execute(IJobExecutionContext context)
{
_jobKey = context.JobDetail.Key;
Log.InfoFormat("---- {0} executing at {1}", _jobKey, DateTime.Now.ToString("r"));
try
{
// main job loop... see the JavaDOC for InterruptableJob for discussion...
// do some work... in this example we are 'simulating' work by sleeping... :)
for (int i = 0; i < 4; i++)
{
try
{
Thread.Sleep(10 * 1000);
}
catch (Exception ignore)
{
Console.WriteLine(ignore.StackTrace);
}
// periodically check if we've been interrupted...
if (_interrupted)
{
Log.InfoFormat("--- {0} -- Interrupted... bailing out!", _jobKey);
throw new JobExecutionException("Interrupt job");
//return;
// could also choose to throw a JobExecutionException
// if that made for sense based on the particular
// job's responsibilities/behaviors
}
}
}
finally
{
Log.InfoFormat("---- {0} completed at {1}", _jobKey, DateTime.Now.ToString("r"));
}
}
/// <summary>
/// Called by the <see cref="IScheduler" /> when a user
/// interrupts the <see cref="IJob" />.
/// </summary>
public virtual void Interrupt()
{
Log.Info("--- -- INTERRUPTING --");
_interrupted = true;
}
}
这是我的主要方法。
private static void Main(string[] args)
{
try
{
LogManager.Adapter = new Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter
{
Level = LogLevel.Info
};
// Grab the Scheduler instance from the Factory
IScheduler scheduler = GetScheduler();
//scheduler.Interrupt(@"group1.69decadb-2385-4c30-8d19-22088601670c");
Guid guid = Guid.NewGuid();
IJobDetail job = JobBuilder.Create<HelloJob>()
.WithIdentity(guid.ToString(), "group1")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(10)
.RepeatForever()
.WithMisfireHandlingInstructionNextWithRemainingCount())
.Build();
var key = job.Key;
// Tell quartz to schedule the job using our trigger
scheduler.ScheduleJob(job, trigger);
}
catch (SchedulerException se)
{
Console.WriteLine(se);
}
Console.WriteLine("Press any key to close the application");
Console.ReadKey();
}
作业在正确的间隔时间内被正确地触发。但是,当我发送中断时,它不会停止作业:(
我正在尝试用
停止工作IScheduler scheduler = GetScheduler();
var jobkey = new JobKey("69decadb-2385-4c30-8d19-22088601670c","group1");
scheduler.Interrupt(jobkey);
我通过在作业调度时使用断点来获取作业键,保存键,然后测试中断。从数据库中交叉验证。
但是,即使我发送中断,工作仍然继续,触发器仍然触发。我从qrtz_simple_triggers
检查了这一点,table.TIMES_TRIGGERED
即使在发送中断后也总是递增。
我不知道我做错了什么
中断是用来运行作业的。它不会将作业作为一个整体置于暂停状态。不管在处理过程中是否被中断,都会触发作业(计数器递增)。中断是指长时间运行的作业,需要发出停止信号——下一次运行将按计划进行。
您可能要查找的是IScheduler.PauseJob(JobKey),它将暂停所有作业触发器,从而停止执行,直到触发器再次恢复。