2

我正在尝试使用 Quartz.NET 安排一些工作,但我无法让它工作。我尝试了以下代码,但在达到指定时间时没有任何反应。

    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();
    }

更新:

我还尝试了以下操作,以确保不是 Cron 表达式导致问题。也没有为我工作......

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());

请注意,它trigger.GetNextFireTimeUtc()返回一个有效的时间值,但该作业永远不会被触发。

我哪里做错了?

4

1 回答 1

7

您的示例代码一切正常,可能是 SatellitePaymentGenerationJob 实现中的 Execute 方法错误,或者作业已执行但未在预期时间执行。以现在的形式,每天18:26开火,是你想要的吗?

与我的代码(工作)比较:

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");
    }
}
于 2013-02-16T17:31:29.157 回答