C#调用Quartz实例代码
C#调用Quartz实例代码博客分类:
.netquartzCC++C#HTML 需求描述
:首页有一部分数据需要在某个时间段内每隔一分钟从数据库读出,首页的访问量太大,实时从数据库取很耗数据库资源,我的解决方案是:在首页通过几个 include 小html文件,这里只需要用Quartz每隔一分钟从数据库读出这些数据并生成HTML代码.
代码如下:
C#代码
using Quartz.Listener; using Quartz; using System.Collections;
using Model; using System.Collections.Generic;
using System.Text; using System.IO; using Quartz.Impl;
using System;
namespace DataBLL
{
public class CustomJob : IJob
{ public void Execute(JobExecutionContext context)
{ try { ProcesIndex.IndexDDE();// LogHelper.WriteLog(DateTime.Now.ToLongTimeString() + "执行更新成功"); }
catch (Exception e) { LogHelper.WriteLog(DateTime.Now.ToLongTimeString()+"执行更新首页数据失败!\n"+e.Message); } } }
public class MyTriggerRunner { public virtual void Run()
{ //获取一个调度工厂 ISchedulerFactory sf = new StdSchedulerFactory(); IScheduler sched = sf.GetScheduler(); //System.DateTime ts = TriggerUtils.GetNextGivenSecondDate(null, 15);
JobDetail job = new JobDetail("job1", "group1", typeof(CustomJob)); //SimpleTrigger trigger = new SimpleTrigger("trigger1", "group1");
CronTrigger trigger = new CronTrigger("trigger1", "group1", "job1", "group1"); ////设定启动时间 //trigger.StartTime = ts; ////设置每隔10秒运行 //trigger.RepeatInterval = 10000; ////设置总共运行次数 //trigger.RepeatCount = 100; //表达式表示:每周一至周五早上九点至下午三点之间每分钟执行一次 trigger.CronExpressionString = "0 0/1 9-15 * * ?"; sched.AddJob(job, true);
DateTime ft = sched.ScheduleJob( trigger);
//启动作业调度任务 sched.Start(); } } } } 还需要最重要的一步,在main方法里调用 MyTriggerRunner.run();