简体   繁体   中英

Scheduling Cron Jobs With Quartz.NET

I'm trying to schedule some jobs using Quartz.NET but I cannot get it working. I tried the following code but nothing happened when the specified time was met.

    public void Test()
    {
        ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
        IScheduler scheduler = schedulerFactory.GetScheduler();

        IJobDetail jobDetail = JobBuilder.Create<SatellitePaymentGenerationJob>()
            .WithIdentity("TestJob")
            .Build();
        ITrigger trigger = TriggerBuilder.Create()
            .ForJob(jobDetail)
            .WithCronSchedule("0 26 18 * * ?")
            .WithIdentity("TestTrigger")
            .StartNow()
            .Build();
        scheduler.ScheduleJob(jobDetail, trigger);
        scheduler.Start();
    }

UPDATE:

I also tried the following just to make sure it's not the Cron expression that is causing the problem. Did not work for me either...

IJobDetail jobDetail = JobBuilder.Create<SatellitePaymentGenerationJob>()
                .WithIdentity("TestJob", "TestGroup")
                .Build();
            ITrigger trigger = TriggerBuilder.Create()  
                .ForJob(jobDetail)
                .WithSimpleSchedule(x=> x.RepeatForever().WithIntervalInSeconds(10).WithMisfireHandlingInstructionFireNow())
                .StartAt(new DateTimeOffset(DateTime.UtcNow.AddSeconds(10)))
                .WithIdentity("TestTrigger", "TestGroup")
                .Build();
            scheduler.ScheduleJob(jobDetail, trigger);
            scheduler.Start();
            Console.WriteLine(DateTime.UtcNow.ToLongTimeString());
            Console.WriteLine(trigger.GetNextFireTimeUtc());

Note that trigger.GetNextFireTimeUtc() returns a valid time value, but the job never gets triggered.

Where did I go wrong?

Everything is ok with your sample code, maybe the Execute method from SatellitePaymentGenerationJob implementation is wrong, or the job is executed but not in the expected time. In the current shape it will be fired at 18:26 every day, is that what you want?

Compare with my code (working):

class Program
{
    static void Main(string[] args)
    {
        Test();
    }

    public static void Test()
    {
        ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
        IScheduler scheduler = schedulerFactory.GetScheduler();

        IJobDetail jobDetail = JobBuilder.Create<SatellitePaymentGenerationJob>()
            .WithIdentity("TestJob")
            .Build();
        ITrigger trigger = TriggerBuilder.Create()
            .ForJob(jobDetail)
            .WithCronSchedule("0 45 20 * * ?")
            .WithIdentity("TestTrigger")
            .StartNow()
            .Build();
        scheduler.ScheduleJob(jobDetail, trigger);
        scheduler.Start();
    }
}

internal class SatellitePaymentGenerationJob : IJob
{
    public void Execute(IJobExecutionContext context)
    {
        Console.WriteLine("test");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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