How do I force a quartz.net job to restart intervall after completion

后端 未结 4 982
醉话见心
醉话见心 2021-01-22 19:27

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

Following this example I am building my jobs with

                s.         


        
4条回答
  •  感情败类
    2021-01-22 19:46

    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.

    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: 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()
                    .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.KeyEquals(jobKey));
    

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

提交回复
热议问题