views:

131

answers:

1

I'm trying to get Quartz.net working by embedding into my .Net MVC2 application. I know this is not ideal, but I'm just trying to get it up and running before moving it over to a service. I can't get my jobs to fire off, but I think I'm configured correctly. In my Global.asax.cs:

protected void Application_Start()
{
    Quartz.IScheduler scheduler = BuildQuartzScheduler();
    ... 
 }

And the method, taken straight from the tutorial:

private IScheduler BuildQuartzScheduler()
{
    // construct a scheduler factory
    ISchedulerFactory schedFact = new StdSchedulerFactory();

    // get a scheduler
    IScheduler sched = schedFact.GetScheduler();
    sched.Start();

    // construct job info
    JobDetail jobDetail = new JobDetail("myJob", null, typeof(QuartzController));
    // fire every hour
    Trigger trigger = TriggerUtils.MakeMinutelyTrigger();
    // start on the next even hour
    trigger.StartTimeUtc = TriggerUtils.GetEvenMinuteDate(DateTime.UtcNow);
    trigger.Name = "myTrigger";
    sched.ScheduleJob(jobDetail, trigger);
    return sched;
}

And the "controller:"

    public class QuartzController : IJob
    {
       public QuartzController() {
       }

       public void Execute(JobExecutionContext context) {
           throw new NotImplementedException();
        }
    }

Nothing ever gets fired. What's going on? I'm sure there must be a simple syntax mistake, but it is driving me crazy!

+1  A: 

If Application_Start looks like that, then I reckon your scheduler variable is likely to be garbage collected as soon as that method finishes executing.

I'd store a reference to the scheduler as a static variable in your HttpApplication class. This way, the reference hangs around for the duration of the process. A guess, but worth a shot.

Nicholas Piasecki
it's actually the scheduler factory that needs to be kept as a static variable.
Marko Lahma