我如何强制quartz.net作业完成后,重新启动INTERVALL作业、重新启动、完成后、quartz

2023-09-06 10:49:12 作者:不再回忆谁

我有,我用一个项目 TopShelf TopShelf.Quartz

I have a project where I use TopShelf and TopShelf.Quartz

继这个例子我建设我的工作与

                s.ScheduleQuartzJob(q =>
                    q.WithJob(() => JobBuilder.Create<MyJob>().Build())
                    .AddTrigger(() => TriggerBuilder.Create()
                        .WithSimpleSchedule(builder => builder
                            .WithIntervalInSeconds(5)
                            .RepeatForever())
                        .Build())
                );

这激发我的工作,每五秒钟即使previous仍在运行。我真正想要的才达到的开始工作,并在完成后等待五秒钟,然后再次启动。这是可能的还是我有(通过一个静态变量为例),以实现自己的逻辑。

which fires my job every five seconds even if the previous is still running. What I really want to achive is to start a job and after the completion wait five seconds and start again. Is this possible or do I have to implement my own logic (for example via a static variable).

推荐答案

该JobListener解决方案是一个非常强大和灵活的方式完成后,重新安排你的工作。由于内特Kerkhofs和stuartd为输入。

The JobListener solution is a very powerful and flexible way to reschedule your job after completion. Thanks to Nate Kerkhofs and stuartd for the input.

在我的情况下,它足以来装饰我的作业类与 DisallowConcurrentExecution 属性,因为我没有我的工作的不同实例

In my case it was sufficient to decorate my Job class with the DisallowConcurrentExecution attribute since I don't have different instances of my job

[DisallowConcurrentExecution]
public class MyJob : IJob
{
}

FYI:使用JobListerener与 TopShelf.Quartz 的code可能看起来像这样

FYI: Using a JobListerener with TopShelf.Quartz the code could look like this

var jobName = "MyJob";
var jobKey = new JobKey(jobName);

s.ScheduleQuartzJob(q =>
           q.WithJob(() => JobBuilder.Create<MyJob>()
                .WithIdentity(jobKey).Build())
            .AddTrigger(() => TriggerBuilder.Create()
                .WithSimpleSchedule(builder => builder
                    .WithIntervalInSeconds(5)
                .Build())

var listener = new RepeatAfterCompletionJobListener(TimeSpan.FromSeconds(5));
var listenerManager = ScheduleJobServiceConfiguratorExtensions
      .SchedulerFactory().ListenerManager;
listenerManager.AddJobListener(listener, KeyMatcher<JobKey>.KeyEquals(jobKey));

如果您使用的是 TopShelf.Quartz.Ninject (像我这样做),不要忘了叫 UseQuartzNinject()在调用 ScheduleJobServiceConfiguratorExtensions.SchedulerFactory()

If you are using TopShelf.Quartz.Ninject (like I do) don't forget to call UseQuartzNinject() prior to calling ScheduleJobServiceConfiguratorExtensions.SchedulerFactory()