Monday, July 19, 2010

Quartz.net with ASP.NET implementation...

Just now finished my sample scheduled task with quartz.net in my asp.net web application.....


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 System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.IO;
    using Quartz;
    
    /// <summary>
    /// Summary description for SendMailJob
    /// </summary>
    public class SendMailJob : IJob
    {
        public void Execute(JobExecutionContext context)
        {
            WriteToFile();
        }
        private static void WriteToFile()
        {
            StreamWriter SW;
            SW = File.CreateText("C:\\Documents and Settings\\chendur\\Desktop\\samp.txt");
            SW.WriteLine("God is greatest of them all");
            SW.WriteLine("This is second line");
            SW.WriteLine(getIndianStandardTime());
            SW.Close();
        }
        public static DateTime getIndianStandardTime()
        {
            TimeZoneInfo IND_ZONE = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
            return TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, IND_ZONE);
        }
    }
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:
<%@ Application Language="C#" %>
<%@ Import Namespace="Quartz"%>
<%@ Import Namespace="Quartz.Impl" %>

<script runat="server">

    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);
        SimpleTrigger trigger2 = new SimpleTrigger("myTrigger",
                                null,
                                DateTime.UtcNow,
                                null,
                                SimpleTrigger.RepeatIndefinitely,
                                TimeSpan.FromSeconds(60)); 
        //Trigger trigger = TriggerUtils.MakeHourlyTrigger();
        //// start on the next even hour
        //trigger.StartTimeUtc = TriggerUtils.GetEvenHourDate(DateTime.UtcNow);  
        //trigger.Name = "mySendMailTrigger";
        // schedule the job for execution
        sched.ScheduleJob(jobDetail, trigger2);
    }

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.

14 comments:

  1. very nice working code, great dude

    ReplyDelete
  2. You should not lose reference to your StdSchedulerFactory, it should be global. Having a sample job that called SendMailJob is kinda bad example as it doesn't really send email (and Quartz.NET comes prepackaged with a send mail job).

    ReplyDelete
  3. hi this kannadas 8 months exp, Dot Net Developer in VASA Infotech Services Pvt Ltd ,Chennai. Kindly send me the Quertz.net code Example to this mail id:
    kannadas.m@vasaits.com thank you.

    ReplyDelete
  4. Hi Pandiya chendur

    Thanks for the blog It worked fine and I m able to schedule the task
    Thank You very much

    Aman Rohilla

    ReplyDelete
  5. Hi Pandiya,
    I'm working on Quartz.net Ado Job store. Jobs are getting stored into the database. But the Quartz server was unable to schedule jobs presentt in the database. What are the steps I need to follow to make My Quartz Service to fire jobs present in the database.
    Thanks in advance

    ReplyDelete
  6. Hi,

    I have gone through your code. Everything was fine. But How it will execute if the application is not running at that time of scheduling time (at 6 pm).

    ReplyDelete
  7. I paу a visit each ԁay a fеω webѕitеs anԁ information sіteѕ
    to read articlеs or rеvieωs, however this wеbsіtе pгоvіdеs quаlitу based pоstѕ.
    Look into my page ... Instagram-promoter.com

    ReplyDelete
  8. Nice blog here! Also your web site loads up very fast!
    What host are you using? Can I get your affiliate link to your
    host? I wish my site loaded up as quickly as yours lol

    Here is my homepage forex broker

    ReplyDelete
  9. My brother suggested I might like this blog.

    He was totally right. This post truly made my day.
    You can not imagine just how much time I had spent for this info!
    Thanks!

    Feel free to surf to my webpage www.zulutradeonline.com

    ReplyDelete
  10. Do you mind if I quote a few of your articles as long
    as I provide credit and sources back to your website?
    My blog site is in the very same niche as yours and my visitors would really
    benefit from some of the information you present here.
    Please let me know if this ok with you. Thanks!

    Also visit my webpage :: http://gymkhana.iitb.ac.in/

    ReplyDelete
  11. Hello, i feel that i noticed you visited my weblog thus i got here
    to go back the favor?.I'm trying to in finding issues to enhance my site!I guess its ok to make use of some of your ideas!!

    Feel free to visit my homepage get followers

    ReplyDelete
  12. I'm curious to find out what blog platform you are working with? I'm experiencing some small security
    issues with my latest website and I'd like to find something more secure. Do you have any suggestions?

    Review my website; get followers

    ReplyDelete
  13. This is not working can you tell me how do i do this ? Please reply me

    ReplyDelete