quartz.net 执行后台任务

...

https://www.cnblogs.com/zhangweizhong/category/771057.html

https://www.cnblogs.com/lanxiaoke/category/973331.html

宿主在控制台程序中

using System;
using System.Collections.Specialized;
using System.IO;
using System.Threading.Tasks;
using Quartz;
using Quartz.Impl;
using Ace;
using Microsoft.Extensions.Configuration;
using Ace.Application.CS;

namespace CS.QuartzJob
{
public class Program
{
private static void Main(string[] args)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("config.json", true, true)
.Build();
Globals.Configuration = configuration;

RunProgram().GetAwaiter().GetResult();

Console.WriteLine("Press any key to close the application");
Console.Read();
}

private static async Task RunProgram()
{
try
{
// Grab the Scheduler instance from the Factory
NameValueCollection props = new NameValueCollection
{
{ "quartz.serializer.type", "binary" }
};
StdSchedulerFactory factory = new StdSchedulerFactory(props);
IScheduler scheduler = await factory.GetScheduler();
await scheduler.Start();

var cron1 = Globals.Configuration["JobCron:Job1"];
// 项目完成状态
IJobDetail job = JobBuilder.Create<ProjectDoneJob>()
.WithIdentity("job1", "group1")
.Build();
ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create()
.WithIdentity("trigger1")
.WithCronSchedule(cron1)//cron触发器
.ForJob("job1", "group1")
.Build();
await scheduler.ScheduleJob(job, trigger);

}
catch (SchedulerException se)
{
await Console.Error.WriteLineAsync(se.ToString());
}
}
}

public class ProjectDoneJob : IJob
{
public async Task Execute(IJobExecutionContext context)
{

await Console.Out.WriteLineAsync( DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}
}
}

 

config.json,设置始终复制

{

"JobCron": {
"Job1": "0 0/1 * * * ?" //1分钟
}

}

posted @ 2019-02-27 19:49  胡椒面  阅读(609)  评论(0编辑  收藏  举报