asp.net 网站中的 Quartz.net 设置

发布于 2024-09-09 22:23:37 字数 1968 浏览 6 评论 0原文

我刚刚将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 = "[email protected]";
        mail.From = "[email protected]";
        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", "[email protected]");
        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 文件中配置它吗?有什么建议吗?

I've just added quartz.net dll to my bin and started my example. How do I call a C# method using quartz.net based on time?

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 = "[email protected]";
        mail.From = "[email protected]";
        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", "[email protected]");
        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;
    }
}
}

Here I am just sending a mail on page load. How do I call SendMail() once in a day at a given time (say 6.00 AM) using quartz.net? I don't know how to get started. Should I configure it in my global.asax file? Any suggestion?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

云雾 2024-09-16 22:23:37

您是否尝试过 quartz.net 教程

由于您的 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 调用您的作业一次。这可以在 global.asaxApplication_Start 中完成:

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);
    }
    ...
}

就是这样。您的作业应在每天 06:00 执行。为了进行测试,您可以创建一个每分钟触发一次的触发器(例如)。看一下TriggerUtils的方法。

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

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

  • 您可能想要实现一个 Windows 服务来发送电子邮件(Windows 服务将始终运行)
  • 或更简单:在小型控制台应用程序中实现发送邮件功能,然后设置Windows 中的计划任务每​​天在所需时间调用控制台应用程序一次。

Did you try the quartz.net tutorial?

Since your web app might get recycled/restarted, you should probably (re-)intialize the quartz.net scheduler in the Application_Start handler in global.asax.cs.


Update (with complete example and some other considerations):

Here's a complete example how to do this using quartz.net. First of all, you have to create a class which implements the IJobinterface defined by quartz.net. This class is called by the quartz.net scheduler at tne configured time and should therefore contain your send mail functionality:

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

Next you have to initialize the quartz.net scheduler to invoke your job once a day at 06:00. This can be done in Application_Start of global.asax:

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);
    }
    ...
}

That's it. Your job should be executed every day at 06:00. For testing, you can create a trigger which fires every minute (for example). Have a look at the method of TriggerUtils.

While the above solution might work for you, there is one thing you should consider: your web app will get recycled/stopped if there is no activity for some time (i.e. no active users). This means that your send mail function might not be executed (only if there was some activity around the time when the mail should be sent).

Therefore you should think about other solutions for your problem:

  • you might want to implement a windows service to send your emails (the windows service will always be running)
  • or much easier: implement your send mail functionality in a small console application, and set up a scheduled task in windows to invoke your console app once a day at the required time.
北斗星光 2024-09-16 22:23:37

在 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);
        }

Add .Result at the end of schedFact.GetScheduler();

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);
        }
初心未许 2024-09-16 22:23:37

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

In addition to the good answer M4N provided, you can take a look at the spring.net integration of the quartz.net lib which allows to call methods without the need to implement IJob.

离去的眼神 2024-09-16 22:23:37

我正在寻找石英。我这样做是为了我的工作:

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

PM> Install-Packagequartz

2:创建一个像这样的类:

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

3.在全局

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次。
祝你好运

i searching for Quartz . i do this for my job:

1:instal Quartz from visual console:

PM> Install-Package quartz

2:create a class like this:

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();
    }

it is code that doing some job in every 10second for 3time.
good luck

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文