如何使用Quartz.net 2.0安排任务? [英] How to schedule task using Quartz.net 2.0?

查看:96
本文介绍了如何使用Quartz.net 2.0安排任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在ASP.NET MVC 4应用程序中使用Quartz.net 2.0计划任务,但是我无法执行该任务.

I am trying to schedule a task using Quartz.net 2.0 in ASP.NET MVC 4 application, but i can not get the task to be executed.

这是代码:

public class ScheduleTaskConfig
{
    public static void StartScheduler()
    {
        ISchedulerFactory schedulerFactory = new StdSchedulerFactory();

        IScheduler scheduler = schedulerFactory.GetScheduler();

        JobKey emailSenderTaskKey = new JobKey("emailSenderTask", "email");
        IJobDetail emailSenderTask = JobBuilder.Create<QueuedEmailsSendTask>()
            .WithIdentity(emailSenderTaskKey)
            .Build();

        TriggerKey emailSenderTriggerKey = new TriggerKey("emailSenderTrigger", "email");
        ITrigger emailSenderTrigger = TriggerBuilder.Create()
            .WithIdentity(emailSenderTriggerKey)
            .WithSimpleSchedule(s => s.RepeatForever().WithIntervalInSeconds(5))
            .StartNow()
            .Build();

        scheduler.ScheduleJob(emailSenderTask, emailSenderTrigger);
        scheduler.Start();
    }
}

它在global.asax应用程序启动中被调用

It is called in global.asax application start

protected void Application_Start()
{
    ScheduleTaskConfig.StartScheduler();
    ...
}

这是实现IJob接口的类:

And here is the class that implements the IJob interface:

public class QueuedEmailsSendTask : IJob
{
    private IQueuedEmailsService _queuedEmailsService { get; set; }
    private IEmailSenderService _emailSenderService { get; set; }

    public QueuedEmailsSendTask(IQueuedEmailsService queuedEmailsService, IEmailSenderService emailSenderService)
    {
        this._queuedEmailsService = queuedEmailsService;
        this._emailSenderService = emailSenderService;
    }

    public void Execute(IJobExecutionContext executeContext)
    {
        //do stuff ...
    }
}

我在Execute方法的开头放置了一个断点,但是调试器并没有在此停下来.我在做什么错了?

I placed a breakpoint at the beginning of the Execute method, but the debugger does not stop there. What am i doing wrong?

更新: 它与实现IJob接口的类没有默认构造函数有关. 如果我这样修改构造函数,它将起作用:

UPDATE: It has something to do with the class that implements the IJob interface not having a default constructor. It works, if I modify the constructor like this:

public QueuedEmailsSendTask()
{
}

但是我需要能够注入我的依赖关系.我正在使用Autofac IoC容器.

But I need to be able to inject my dependencies. I am using the Autofac IoC container.

推荐答案

要使Quartz.net与IoC容器一起工作,您需要具有一个实现IJobFactory的工厂类:

To make Quartz.net work with an IoC container you need to have a factory class implementing IJobFactory:

public class AutofacJobFactory : IJobFactory
{
    private readonly IContainer _container;

    public AutofacJobFactory(IContainer container)
    {
        _container = container;
    }

    public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
    {
        return (IJob)_container.Resolve(bundle.JobDetail.JobType);
    }
}

然后需要在初始化代码中将JobFactory实例分配给调度程序:

The JobFactory instance then needs to be assigned to the scheduler in the initialization code:

ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
IScheduler scheduler = schedulerFactory.GetScheduler();
scheduler.JobFactory = new AutofacJobFactory(container);

别忘了在IoC容器中注册作业.

And don't forget to register the job in the IoC container.

这篇关于如何使用Quartz.net 2.0安排任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆