2025-03-28 09:49:56 +08:00

123 lines
4.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HZQR.Common;
using JobApplication.Job;
using Quartz;
using Quartz.Impl;
namespace JobApplication
{
public class QuartzService
{
IScheduler scheduler = null;
/// <summary>
/// 定时调度业务
/// </summary>
public QuartzService()
{
//1.创建作业调度池(Scheduler)
scheduler = StdSchedulerFactory.GetDefaultScheduler();
//2.创建一个具体的作业即job (具体的job需要单独在一个文件中执行)
var job = JobBuilder.Create<SysncEndAccount>().Build();//同步结账单
//3.创建并配置一个触发器即trigger 3s执行一次
int interval = 3;//3小时执行一次匹配下面的 WithIntervalInHours
var trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.StartAt(DateBuilder.FutureDate(1, IntervalUnit.Second)) // get the next even-hour (minutes and seconds zero ("00:00"))
.WithSimpleSchedule(x => x
.WithIntervalInHours(interval)
.RepeatForever())
// note that in this example, 'forJob(..)' is not called
// - which is valid if the trigger is passed to the scheduler along with the job
.Build();
//移动支付流水按自然日统计,一天一次就够了
var jobFixedRevenue = JobBuilder.Create<FixedRevenueByDay>().Build();
//var triggerFixedRevenue = TriggerBuilder.Create()
// .WithIdentity("trigger2", "group1")
// .StartAt(DateBuilder.DateOf(10, 0, 0))//每天10点开始执行
// .WithCalendarIntervalSchedule(x => x.WithIntervalInDays(1))//每天执行一次
// .EndAt(DateBuilder.DateOf(10, 30, 0))
// .Build();
var triggerFixedRevenue = TriggerBuilder.Create()
.WithIdentity("trigger2", "group1")
.StartAt(DateBuilder.DateOf(10, 0, 0))//每天10点开始执行
.WithSimpleSchedule(x => x
.WithIntervalInHours(24)
.RepeatForever())
.Build();
//移动支付流水按账期统计,一天一次就够了
var jobRevenueByStatement = JobBuilder.Create<FixedRevenueByStatement>().Build();
var triggerRevenueByStatement = TriggerBuilder.Create()
.WithIdentity("trigger3", "group1")
.StartAt(DateBuilder.DateOf(10, 0, 0))//每天10点开始执行
.WithSimpleSchedule(x => x
.WithIntervalInHours(24)
.RepeatForever())
.Build();
//4.将job和trigger加入到作业调度池中
scheduler.ScheduleJob(job, trigger);//同步结账单
scheduler.ScheduleJob(jobFixedRevenue, triggerFixedRevenue);//固化日营收数据
scheduler.ScheduleJob(jobRevenueByStatement, triggerRevenueByStatement);//固化结账日期营收数据
}
/// <summary>
/// 开启任务
/// </summary>
public void Start()
{
scheduler.Start();
}
/// <summary>
/// 关闭任务
/// </summary>
public void Stop()
{
//true表示该Sheduler关闭之前需要等现在所有正在运行的工作完成才能关闭
//false表示直接关闭
scheduler.Shutdown(true);
}
/// <summary>
/// 暂停调度
/// </summary>
public void Pause()
{
scheduler.PauseAll();
}
/// <summary>
/// 继续调度
/// </summary>
public void Continue()
{
scheduler.ResumeAll();
}
}
public class TestJob : IJob
{
public string title;
void IJob.Execute(IJobExecutionContext context)
{
//Console.WriteLine("Hellow JOB");
LogUtil.WriteLog("Hellow JOB" + DateTime.Now.ToString());
}
}
public class TestJob2 : IJob
{
public string title;
void IJob.Execute(IJobExecutionContext context)
{
//Console.WriteLine("Hellow JOB");
LogUtil.WriteLog("Hellow JOB2" + DateTime.Now.ToString());
}
}
}