您的位置:首页 > 其它

.NETCore 知识点记录-QuartzCore 定时任务

2017-12-03 18:31 302 查看
1.定时任务框架:

1.Pomelo.AspNetCore.TimedJob

2.Quartzcore

第二种已经实现 基于 官网 最新 3.0Bate版本

//核心代码:  参照官网 :

官网地址:  https://www.quartz-scheduler.net/documentation/quartz-3.x/quick-start.html

需要注意的地方:

publicclassHelloJob:IJob{publicTaskExecute(IJobExecutionContextcontext){awaitConsole.Out.WriteLineAsync("Greetings from HelloJob!");}}


官网在介绍 HelloJob的时候

在EF里面  await  是要 配合 async 来使用的  (写成下面这样就好)

publicclassHelloJob:IJob{public asyncTaskExecute(IJobExecutionContextcontext){awaitConsole.Out.WriteLineAsync("Greetings from HelloJob!");}}


namespace CCDN.XXXX
{
class Program
{
static void Main(string[] args)
{
//LogProvider.SetCurrentLogProvider(new ConsoleLogProvider());
RunProgram().GetAwaiter().GetResult();
}

private static async Task RunProgram()
{
int i = 0;
DateTime myStartTime = Convert.ToDateTime("2016-12-08");
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();

// 定义一个 Job
IJobDetail job = JobBuilder.Create<ExportWXDataJob>()
.WithIdentity("job1", "group1")
.Build();
ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create()
.WithIdentity("trigger1") // 给任务一个名字
.StartAt(myStartTime) // 设置任务开始时间
.ForJob("job1", "group1") //给任务指定一个分组
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(20)  //循环的时间
.RepeatForever())
.Build();

// 等待执行任务
await scheduler.ScheduleJob(job, trigger);

// some sleep to show what's happening
//await Task.Delay(TimeSpan.FromMilliseconds(2000));

// and last shut down the scheduler when you are ready to close your program
//await scheduler.Shutdown();
i++;
Console.ReadLine();
Console.WriteLine(DateTime.Now.ToString()+"这是第"+i+"次执行任务");
}
catch (SchedulerException se)
{
await Console.Error.WriteLineAsync(se.ToString());
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: