Quart.net.net在asp.net网站上设置

时间:2010-07-14 12:21:00

标签: c# asp.net scheduled-tasks quartz.net

我刚刚将quartz.net dll添加到我的bin中并启动了我的示例。如何根据时间使用quartz.net调用C#方法?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Quartz;
using System.IO;


public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(SendMail())
             Response.write("Mail Sent Successfully");
    }
   public bool SendMail()
   {
    try
    {
        MailMessage mail = new MailMessage();
        mail.To = "test@test.com";
        mail.From = "sample@sample.com";
        mail.Subject = "Hai Test Web Mail";
        mail.BodyFormat = MailFormat.Html;
        mail.Body = "Hai Test Web Service";
        SmtpMail.SmtpServer = "smtp.gmail.com";
        mail.Fields.Clear();
        mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
        mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "redwolf@gmail.com");
        mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "************");
        mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
        mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
        SmtpMail.Send(mail);
        return (true);
    }
    catch (Exception err)
    {
        throw err;
    }
}
}

这里我只是在页面加载时发送邮件。如何使用quartz.net在给定时间(比如说早上6点)的一天内拨打SendMail()一次?我不知道怎么开始。我应该在global.asax文件中配置它吗?有什么建议吗?

4 个答案:

答案 0 :(得分:23)

您是否尝试过quartz.net tutorial

由于您的Web应用程序可能会被回收/重新启动,因此您应该(重新)初始化global.asax.cs中Application_Start处理程序中的quartz.net调度程序。


更新(完整示例和其他一些注意事项):

以下是使用quartz.net完成此操作的完整示例。首先,您必须创建一个实现quartz.net定义的IJob接口的类。此类由quartz.net调度程序在配置的时间调用,因此应包含您的发送邮件功能:

using Quartz;
public class SendMailJob : IJob
{
    public void Execute(JobExecutionContext context)
    {
        SendMail();
    }
    private void SendMail()
    {
        // put your send mail logic here
    }
}

接下来,您必须初始化quartz.net调度程序,以便每天06:00调用您的工作。这可以在Application_Start的{​​{1}}中完成:

global.asax

就是这样。你的工作应该每天06:00执行。对于测试,您可以创建一个每分钟触发的触发器(例如)。看看using Quartz; using Quartz.Impl; public class Global : System.Web.HttpApplication { void Application_Start(object sender, EventArgs e) { ISchedulerFactory schedFact = new StdSchedulerFactory(); // get a scheduler IScheduler sched = schedFact.GetScheduler(); sched.Start(); // construct job info JobDetail jobDetail = new JobDetail("mySendMailJob", typeof(SendMailJob)); // fire every day at 06:00 Trigger trigger = TriggerUtils.MakeDailyTrigger(06, 00); trigger.Name = "mySendMailTrigger"; // schedule the job for execution sched.ScheduleJob(jobDetail, trigger); } ... }

的方法

虽然上述解决方案可能对您有用,但您应该考虑以下事项:如果一段时间内没有活动(即没有活跃用户),您的网络应用程序将被回收/停止。这意味着您的发送邮件功能可能无法执行(仅当发送邮件时有一些活动)。

因此,您应该考虑针对您的问题的其他解决方案:

  • 您可能希望实施Windows服务来发送电子邮件(Windows服务将始终运行)
  • 或者更容易:在小型控制台应用程序中实现发送邮件功能,并在Windows中设置计划任务,以便在所需时间每天调用一次控制台应用程序。

答案 1 :(得分:2)

除了M4N提供的好答案之外,您还可以查看spring.net integration of the quartz.net lib,它允许调用方法而无需实现IJob。

答案 2 :(得分:0)

在schedFact.GetScheduler()末尾添加.Result;

void Application_Start(object sender, EventArgs e)
        {
            ISchedulerFactory schedFact = new StdSchedulerFactory();
            // get a scheduler
            IScheduler sched = schedFact.GetScheduler().Result;
            sched.Start();
            // construct job info
            JobDetail jobDetail = new JobDetail("mySendMailJob", typeof(SendMailJob));
            // fire every`enter code here` day at 06:00
            Trigger trigger = TriggerUtils.MakeDailyTrigger(06, 00);
            trigger.Name = "mySendMailTrigger";
            // schedule the job for execution
            sched.ScheduleJob(jobDetail, trigger);
        }

答案 3 :(得分:-3)

我正在搜索Quartz。我这样做是为了我的工作:

1:从可视控制台安装Quartz:

PM>安装包石英

2:创建一个这样的类:

using Quartz;
public class Quartz : IJob
{
    public void Execute(IJobExecutionContext context)
    {
        //do some 
    }
}

3.in global

using Quartz;
using Quartz.Impl;

    protected void Application_Start(object sender, EventArgs e)
    {
        //for start time at first run after 1 hour
        DateTimeOffset startTime = DateBuilder.FutureDate(1, IntervalUnit.Hour);
        IJobDetail job = JobBuilder.Create<Quartz>()
                                   .WithIdentity("job1")
                                   .Build();
        ITrigger trigger = TriggerBuilder.Create()
                                         .WithIdentity("trigger1")
                                         .StartAt(startTime)
                                         .WithSimpleSchedule(x => x.WithIntervalInSeconds(10).WithRepeatCount(2))
                                         .Build();
        ISchedulerFactory sf = new StdSchedulerFactory();
        IScheduler sc = sf.GetScheduler();
        sc.ScheduleJob(job, trigger);
        sc.Start();
    }

代码是每隔10秒做一次工作3次。 祝你好运