Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quartz.net setup in an asp.net website

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?

like image 945
ACP Avatar asked Jul 14 '10 12:07

ACP


People also ask

What is Quartz in asp net core?

Quartz.NET provides lots of scheduling features and has an easy to use API for implementing scheduled jobs. Code: https://github.com/damienbod/AspNetCoreQuartz. A simple ASP.NET Core Razor Page web application is used to implement the scheduler and the SignalR messaging. The Quartz Nuget package and the Quartz.

What is Quartz job in C#?

What is QUARTZ? Quartz.Net is a . NET port of the popular Java job scheduling framework. It's an open source job scheduling system that can be used from the smallest apps to the large-scale enterprise systems.

What is Quartz server?

Quartz is a job scheduling library that can be integrated into a wide variety of Java applications. Quartz is generally used for enterprise class applications to support process workflow, system management (maintenance) actions and to provide timely services within the applications. Quartz also supports clustering.

How do I know what version of Quartz I have?

The Quartz package includes a number of jar files, located in root directory of the distribution. The main Quartz library is named quartz-xxx. jar (where xxx is a version number). In order to use any of Quartz's features, this jar must be located on your application's classpath.


3 Answers

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);
        }
like image 183
Omid Soleiman Avatar answered Oct 03 '22 04:10

Omid Soleiman


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.
like image 29
M4N Avatar answered Oct 03 '22 03:10

M4N


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.

like image 40
tobsen Avatar answered Oct 03 '22 05:10

tobsen