• c# .net framework 4.5.2 , Quartz.NET 3.0.7


    参考了:https://www.cnblogs.com/personblog/p/11277527.html

    https://www.jianshu.com/p/b8e7e4deb60a

    .NET MVC 中的示例:

    ReportJob.cs

    using Quartz;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web;
    
    namespace Quartz研究.MyJob.GetWxQdSubMchId
    {
        [DisallowConcurrentExecution]
        public class ReportJob : IJob
        {
    
            public Task Execute(IJobExecutionContext context)
            {
    
                return Task.Run(() =>
                {
    
                    foo();
    
                });
    
            }
    
    
            public void foo()
            {
                try
                {
                    var reportDirectory = string.Format("~/reports/{0}/", DateTime.Now.ToString("yyyy-MM"));
                    reportDirectory = System.Web.Hosting.HostingEnvironment.MapPath(reportDirectory);
                    if (!Directory.Exists(reportDirectory))
                    {
                        Directory.CreateDirectory(reportDirectory);
                    }
                    var dailyReportFullPath = string.Format("{0}report_{1}.log", reportDirectory, DateTime.Now.Day);
                    var logContent = string.Format("{0}==>>{1}{2}", DateTime.Now, "create new log.", Environment.NewLine);
                    File.AppendAllText(dailyReportFullPath, logContent);
    
                }
                catch (Exception ex)
                {
                    //日志
                }
    
            }
    
    
        }
    }

    --

    ReportJobScheduler.cs

    using Quartz;
    using Quartz.Impl;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace Quartz研究.MyJob.GetWxQdSubMchId
    {
        public class ReportJobScheduler
        {
            /// <summary>
            /// 创建计划任务
            /// </summary>
            public static async void Start()
            {
    
                try
                {
                    string thisJob = "ReportJob";
    
                    string groupName = "gp"+ thisJob;
                    string jobName = "job" + thisJob;
                    string triggerName = "trigger" + thisJob;
    
                    // 创建作业调度池
                    ISchedulerFactory factory = new StdSchedulerFactory();
                    IScheduler scheduler = await factory.GetScheduler();
    
                    // 创建作业
                    IJobDetail job = JobBuilder.Create<ReportJob>()
                      .WithIdentity(jobName, groupName)
                      .Build();
    
                    // 创建触发器,每10s执行一次
                    ITrigger trigger = TriggerBuilder.Create()
                      .WithIdentity(triggerName, groupName)
                      .StartNow()
                      .WithSimpleSchedule(x => x
                        .WithIntervalInSeconds(10)
                        .RepeatForever())
                      .Build();
    
                    // 加入到作业调度池中
                    await scheduler.ScheduleJob(job, trigger);
    
                    // 开始运行
                    await scheduler.Start();
                }
                catch (Exception ex)
                {
                    //日志
                }
    
            }
    
        }
    }

    --

    Application_Start()

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Http;
    using System.Web.Mvc;
    using System.Web.Optimization;
    using System.Web.Routing;
    
    namespace Quartz研究
    {
        public class MvcApplication : System.Web.HttpApplication
        {
            protected void Application_Start()
            {
    
                Quartz研究.MyJob.GetWxQdSubMchId.ReportJobScheduler.Start();
    
                AreaRegistration.RegisterAllAreas();
                GlobalConfiguration.Configure(WebApiConfig.Register);
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
            }
        }
    }

    --

  • 相关阅读:
    AnaConda环境下安装librosa包超时
    [浙江大学数据结构]多项式求值,及算法效率问题
    java正则表达式测试用例
    tK Mybatis 通用 Mapper 3.4.6: Example 新增 builder 模式的应用
    Detect image format in Java(使用java探测图片文件格式)
    使用ColumnType注解解决/过滤/转义tk mybatis插入insertSelective、insert语句中遇到sql关键字
    IDEA中关闭sonar代码质量检测
    pip设置安装源
    无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系
    sql 查出一张表中重复的所有记录数据
  • 原文地址:https://www.cnblogs.com/runliuv/p/11976385.html
Copyright © 2020-2023  润新知