2

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?

Kassem
  • 8,116
  • 17
  • 75
  • 116

1 Answers1

7

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");
    }
}
Steve Macculan
  • 2,292
  • 5
  • 22
  • 32
  • Yes that was the intention. I've just set it to run at 20:00 everyday (which is right now as per my timezone) and nothing happened. The breakpoint at the beginning of the `Execute` method was not hit. Any ideas? – Kassem Feb 16 '13 at 18:02
  • Did you call the Test() method in your application? – Steve Macculan Feb 16 '13 at 19:28
  • Certainly. I called it in the `Main` method of a console application. I also tried an interval based trigger (as you can see in the updated question) but it did not work either. – Kassem Feb 16 '13 at 19:42
  • 4
    Ok I finally got it working. Apparently, the problem is with the job that implements the `IJob` interface because it does not have a default constructor. I'm guessing I will need to initialize the dependencies inline (as opposed to doing it through constructor params). Thanks for the help :) – Kassem Feb 16 '13 at 20:05