Sirwan Afifi
Sirwan Afifi

Reputation: 10824

Quartz: Why does this Trigger not fire?

I am using quartz.NET in my project. I have the following problem. I want to run a Scheduled task everyday at 23 o'clock and I am using this code to do that:

public class HelloJob : IJob
{
    public void Execute(IJobExecutionContext context)
    {
        //Download file
    }
}
public interface ISchedule
{
    void Run();
}

public class HelloSchedule : ISchedule
{
    public void Run()
    {

        IJobDetail job = JobBuilder.Create<HelloJob>()
                                   .WithIdentity("job1")
                                   .Build();

        ITrigger trigger = TriggerBuilder.Create()
                                         .ForJob(job)
                                         .WithIdentity("trigger1")
                                         .StartNow()
                                         .WithCronSchedule("0 0 23 ? * MON-FRI *")
                                         .Build();

        ISchedulerFactory sf = new StdSchedulerFactory();
        IScheduler sc = sf.GetScheduler();
        sc.ScheduleJob(job, trigger);

        sc.Start();
    }
}

but unfortunately it's not firing. How can I know what the problem is and solve it?
Thanks for your advice

Upvotes: 0

Views: 2602

Answers (2)

LeftyX
LeftyX

Reputation: 35597

Your job will fire at 11pm.
You can check the next fire time for you job using this code:

private static DateTime getNextFireTimeForJob(IScheduler scheduler, string jobName, string groupName = "")
    {
        JobKey jobKey = null;

        if (!string.IsNullOrEmpty(groupName))
        {
            jobKey = new JobKey(jobName, groupName);
        }
        else
        {
            jobKey = new JobKey(jobName);
        }

        DateTime nextFireTime = DateTime.MinValue;

        bool isJobExisting = Scheduler.CheckExists(jobKey);
        if (isJobExisting)
        {
            var detail = scheduler.GetJobDetail(jobKey);
            var triggers = scheduler.GetTriggersOfJob(jobKey);

            var myTrigger = triggers.Where(f => f.Key.Name == "SecondTrigger").SingleOrDefault();


            if (triggers.Count > 0)
            {
                var nextFireTimeUtc = triggers[0].GetNextFireTimeUtc();
                nextFireTime = TimeZone.CurrentTimeZone.ToLocalTime(nextFireTimeUtc.Value.DateTime);
            }
        }

        return (nextFireTime);
    }

and get the info using this:

var nextFireTime = getNextFireTimeForJob(sc, "job1");

Make sure your HelloJob implements IJob.

If you're integrating your Quartz.net scheduler in a WinApp make sure it's created singleton cause it might be destroyed when it goes out of scope.

I test my jobs in a console application and use Console.ReadLine(); to wait the jobs execution.

Upvotes: 1

roger_that
roger_that

Reputation: 9791

Use org.Quartz and try this:

    JobDetail job = new JobDetail();
    job.setName(Constants.JOB_NAME);
    job.setJobClass(YOUR_JOB_CLASS.class);



    CronTrigger trigger = new CronTrigger();
    trigger.setName(Constants.TRIGGER_NAME);
    trigger.setCronExpression("0 0 23 ? * MON-FRI *");

    // schedule it
    Scheduler scheduler = new StdSchedulerFactory().getScheduler();
    scheduler.start();
    scheduler.scheduleJob(job, trigger);

Your Job class should implement org.Quartz.Job interface and override its execute method which does the actual thing the job needs to perform.

Upvotes: 0

Related Questions