在Orchard CMS中使用Quartz.NET

时间:2013-06-27 05:47:05

标签: asp.net-mvc cmd orchardcms scheduler quartz.net

我正在建立一个Orchard CMS网站项目,我需要安排一些数据存储在数据库中的工作,所以我在Orchard.Web的Global.asax中使用Quartz.NET,如下所示:

protected void Application_Start() {
    RegisterRoutes(RouteTable.Routes);
    _starter = new Starter<IOrchardHost>(HostInitialization, HostBeginRequest, HostEndRequest);
    _starter.OnApplicationStart(this);

    ISchedulerFactory sf = new StdSchedulerFactory();

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

    var job = JobBuilder.Create<JobWorker>()
    .WithIdentity("job1", "group1")
    .Build();

    ITrigger trigger = TriggerBuilder.Create()
    .WithIdentity("trigger1", "group1")
    .StartAt(DateTime.Now)
    .WithCronSchedule("5 0/1 * * * ?")
    .Build();

    sched.ScheduleJob(job, trigger);
}

和JobWorker类 - 与Orchard.Web中的Global.asax放在同一级别的文件夹中:

public class JobWorker : IJob, IDependency {
    private readonly ISchedulerService _schedulerService;

    public JobWorker (ISchedulerService schedulerService) {
        _schedulerService = schedulerService;
    }

    public void Execute(IJobExecutionContext context) {
        _schedulerService.ExecuteJob();

    }
}

但是,我在调试输出控制台中收到的结果如下:

A first chance exception of type 'System.ArgumentException' occurred in Quartz.dll
A first chance exception of type 'Quartz.SchedulerException' occurred in Quartz.dll
A first chance exception of type 'Quartz.SchedulerException' occurred in Quartz.dll
The thread '<No Name>' (0x2278) has exited with code 0 (0x0).
The thread '<No Name>' (0x3368) has exited with code 0 (0x0).
The thread '<No Name>' (0x22a8) has exited with code 0 (0x0).
The thread '<No Name>' (0x2bc8) has exited with code 0 (0x0).

我尝试在web mvc 4项目中使用此代码 - 而不是果园 - 并且它工作正常。因此,我认为问题出在Orchard CMS。 我该怎么办 ?我只需要一个计时器来反复调用SchedulerService中的方法ExecuteJob()!

1 个答案:

答案 0 :(得分:2)

我不确定如何让Quartz.NET在Orchard中运行,但Orchard有自己的服务来处理重复和/或预定的后台任务。

查看实现IBackgroundTask(sample)的简单重复任务和IScheduledTaskHandler(sample),以获得更复杂的定时重复任务或预定的一次性任务。