zoukankan      html  css  js  c++  java
  • asp.net任务调度之Quartz.net

    如果要在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"nulltypeof(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"nulltypeof(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
        }

    }
  • 相关阅读:
    (Delphi) Using the Disk Cache 使用磁盘缓存
    当电视沦为“情怀”,5G能不能拯救它?(zz)
    何为优秀的机器学习特征 zz
    BP神经网络算法推导及代码实现笔记zz
    偏差(Bias)和方差(Variance)——机器学习中的模型选择zz
    关于管理,你可能一直有 3 个误解zz
    读《赋能》有感zz
    Concept Drift(概念漂移)
    第四范式涂威威:AutoML技术现状与未来展望
    韩家炜在数据挖掘上开辟的「小路」是什么
  • 原文地址:https://www.cnblogs.com/zyip/p/1680923.html
Copyright © 2011-2022 走看看