What I'm trying to achieve: I have a secondly trigger that fires every 5 secods, and stateful job, that takes sometimes more than 5 seconds (7 for example) and what I have now
start: 00:00:00
end : 00:00:07
start: 00:00:07 < right after previous has finished
what I want :
start: 00:00:00
it should run at 00:00:05 but it hasn't
end : 00:00:07
start: 00:00:10 (5 seconds after previous, successive or not)
I have tried quartz.net version 2 and 1.
Job:
[PersistJobDataAfterExecution]
[DisallowConcurrentExecution]
public class StatefulJob : IJob (or IStatefulJob in 1.x version)
{
public void Execute(IJobExecutionContext context)
{
Console.WriteLine("StateFull START " + DateTime.Now.ToString());
Thread.Sleep(7000);
Console.WriteLine("StateFull END " + DateTime.Now.ToString());
}
}
Trigger:
var trigger1 = TriggerBuilder
.Create()
.WithSimpleSchedule(x =>
x.WithIntervalInSeconds(timeout)
.RepeatForever()
.Build();
EDIT
I have tried to use WithMisfireHandlingInstructionIgnoreMisfires()
, but missfires happens due to scheduler being shutdown, or because there are no available threads, In my case - jobs does not execute because I use StatefulJob. Maybe I'm wrong, but behavior stays the same.
EDIT2 Ok, solution with 'running' flag works perfect in single threaded app. But if I runs this job in few threads (with different params) it would not work So, is it possible to achieve behavior like I want using quartz ?
What if you let your job run concurrently, but amend it to do nothing if the job is already running, eg something like.
public class StatefulJob : IJob
{
private static bool Running;
public void Execute(IJobExecutionContext context)
{
if (Running)
return;
Running = true;
try
{
Console.WriteLine(" StateFull START " + DateTime.Now.ToString());
Thread.Sleep(7000);
Console.WriteLine(" StateFull END " + DateTime.Now.ToString());
}
finally
{
Running = false;
}
}
}
来源:https://stackoverflow.com/questions/16520645/how-to-create-quartz-job-that-will-runs-every-n-seconds-even-if-job-takes-more-t