参考了:c# .net framework 4.5.2 , Quartz.NET 3.0.7 - runliuv - 博客园 (cnblogs.com)
https://www.cnblogs.com/personblog/p/11277527.html,
自己要轮询的任务:
- using log4net;
- using Quartz;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Web;
-
-
- namespace WzsrmyyApi.QuartzTask
- {
- [DisallowConcurrentExecution]
- public class ReportJob : IJob
- {
-
- public Task Execute(IJobExecutionContext context)
- {
-
- return Task.Run(() =>
- {
-
- foo();
-
- });
-
- }
-
- public static readonly ILog logger = LogManager.GetLogger("SendMsgJob");
- public void foo()
- {
- BigDataSupervisionBll bll = new BigDataSupervisionBll();
- BigDataSupervisionOtherBll otherBll = new BigDataSupervisionOtherBll();
- try
- {
- var dic = bll.SplitCount(otherBll.GetChargesRecordOneDay());
- int dictNum = 0; string type = "charges_record";
- for (int i = dictNum; i < dic.Count; i++)
- {
- string data = dic[dictNum];
- var res = bll.PostData(type, data);
-
- logger.Info("日志");
- }
- }
- catch (Exception ex)
- {
- //日志
- logger.Info("error " + ex);
- }
-
- }
-
-
- }
- }
轮询的任务的建立
- using Quartz;
- using Quartz.Impl;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
-
- namespace WzsrmyyApi.QuartzTask
- {
- public class ReportJobScheduler
- {
- 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
() - .WithIdentity(jobName, groupName)
- .Build();
-
- // 创建触发器,
- ITrigger trigger = TriggerBuilder.Create()
- .WithIdentity(triggerName, groupName)
- .StartNow()
- .WithCronSchedule("0 0 22 ? * * ")//每天晚上十点
- .Build();
- //.WithSimpleSchedule(x => x.WithIntervalInSeconds(10).RepeatForever()//每10s执行一次
-
- // 加入到作业调度池中
- await scheduler.ScheduleJob(job, trigger);
-
- // 开始运行
- await scheduler.Start();
- }
- catch (Exception ex)
- {
- //日志
- }
-
- }
-
- }
- }
最后在程序启动的时候进行注册:
- protected void Application_Start()
- {
- log4net.Config.XmlConfigurator.Configure();//系统日志配置
-
- QuartzTask.ReportJobScheduler.Start();//
-
- AreaRegistration.RegisterAllAreas();
- GlobalConfiguration.Configure(WebApiConfig.Register);
- FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
- RouteConfig.RegisterRoutes(RouteTable.Routes);
- BundleConfig.RegisterBundles(BundleTable.Bundles);
- }
以上就是简单的代码使用 。Quartz.net的使用可以通过两种方式调用:一是通过代码的方式,而是通过配置文件的方式;
详细些的可以参考:Quartz.Net详解-CSDN博客