asp.net任務調度之Quartz.net

來源:互聯網
上載者:User

如果要在asp.net中實作類別似windows中計劃任何的功能,你會怎麼做?

您可以在留言裡寫出您的方法,以便我學習和改進自己的程式,謝謝。

以下是我的方法;

首先下載Quartz.net

web.config加入以下兩個片段

代碼

  <configSections>
    <!--Quartz-->
    <section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    <!--End Quartz-->
  </configSections>

 

 

代碼

  <!--Start Quartz-->
  <quartz>
    <add key="quartz.scheduler.instanceName" value="ExampleDefaultQuartzScheduler"/>
    <add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz"/>
    <add key="quartz.threadPool.threadCount" value="10"/>
    <add key="quartz.threadPool.threadPriority" value="2"/>
    <add key="quartz.jobStore.misfireThreshold" value="60000"/>
    <add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz"/>
  </quartz>
  <!--End Quartz-->

 

 

Global.asax加入以下片段

 

代碼

 void Application_Start(object sender, EventArgs e) 
    {
     
        //Sys.Log.Sys.Info("starting sched...");//日誌中計入系統開始時間
        // construct a scheduler factory
        ISchedulerFactory schedFact = new Quartz.Impl.StdSchedulerFactory();
        // get a scheduler
        sched = schedFact.GetScheduler();
        sched.Start();

        // construct job info
        JobDetail jobDetail = new JobDetail("myJob", null, typeof(Task.QuartzJob_Test));
        // fire every hour
        Trigger trigger = TriggerUtils.MakeSecondlyTrigger(60 * 5);
        // start on the next even hour
        trigger.StartTimeUtc = DateTime.UtcNow;
        trigger.Name = "myTrigger";
        sched.ScheduleJob(jobDetail, trigger);
        
        //delay iisPool
        Task.DelayIISThreadPool.url = HttpContext.Current.Request.Url.ToString();
        JobDetail jobDetail2 = new JobDetail("myJob2", null, typeof(Task.DelayIISThreadPool));
        Trigger trigger2 = TriggerUtils.MakeSecondlyTrigger(60 * 10);
        trigger2.StartTimeUtc = DateTime.UtcNow;
        trigger2.Name = "myTrigger2";
        sched.ScheduleJob(jobDetail2, trigger2);
        // End Quartz
    }

    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown
        
        //Quartz
        if (sched != null)
        {
            sched.Shutdown(true);
        }
        //End Quartz
    }

 

 

App_Code裡加入以下兩個檔案

 

代碼

//QuartzJob_Test.cs
using System;
using System.Collections.Generic;
using System.Web;

namespace Task
{
    /// <summary>
    /// Summary description for DelayIISThreadPool
    /// </summary>
    public class DelayIISThreadPool : Quartz.IJob
    {

        #region IJob Members
        public static string url;
        private static System.Net.WebClient wc = new System.Net.WebClient();

        public void Execute(Quartz.JobExecutionContext context)
        {
            if (string.IsNullOrEmpty(url))
                return;
            wc.DownloadString(url);
        }

        #endregion
    }

}

 

 

代碼

//DelayIISThreadPool.cs
using System;
using System.Collections.Generic;
using System.Web;

namespace Task
{
    public class DelayIISThreadPool : Quartz.IJob
    {

        #region IJob Members
        public static string url;
        private static System.Net.WebClient wc = new System.Net.WebClient();

        public void Execute(Quartz.JobExecutionContext context)
        {
            if (string.IsNullOrEmpty(url))
                return;
            wc.DownloadString(url);
        }

        #endregion
    }

}

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.