admin 管理员组文章数量: 1184232
本文主要向大家介绍了C#编程之C# 通过 Quartz .NET 实现 schedule job 的处理,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。
在实际项目的开发过程中,会有这样的功能需求:要求创建一些Job定时触发运行,比如进行一些数据的同步。
那么在 .Net Framework 中如何实现这个Timer Job的功能呢?
这里所讲的是借助第三方的组件 Quartz.Net 来实现(源码位置:https://github/quartznet/quartznet)
详细内容请看如下步骤:
1):首先在VS中创建一个Console Application,然后通过NuGet下载Quartz.Net组件并且引用到当前工程中。我们下载的是3.0版本,注:此版本与之前的2.0版本一定的区别。
2):继承 IJob 接口,实现 Excute 方法
public class EricSimpleJob : IJob
{
public Task Execute(IJobExecutionContext context)
{
Console.WriteLine("Hello Eric, Job executed.");
return Task.CompletedTask;
}
}
public class EricAnotherSimpleJob : IJob
{
public Task Execute(IJobExecutionContext context)
{
string filepath = @"C:\timertest.txt";
if (!File.Exists(filepath))
{
using (FileStream fs = File.Create(filepath)) { }
}
using (StreamWriter sw = new StreamWriter(filepath, true))
{
sw.WriteLine(DateTime.Now.ToLongTimeString());
}
return Task.CompletedTask;
}
}
3):完成 IScheduler, IJobDetails 与 ITrigger之间的配置
static async Task TestAsyncJob()
{
var props = new NameValueCollection
{
{ "quartz.serializer.type", "binary" }
};
StdSchedulerFactory schedFact = new StdSchedulerFactory(props);
IScheduler sched = await schedFact.GetScheduler();
await sched.Start();
IJobDetail job = JobBuilder.Create()
.WithIdentity("EricJob", "EricGroup")
.Build();
IJobDetail anotherjob = JobBuilder.Create()
.WithIdentity("EricAnotherJob", "EricGroup")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("EricTrigger", "EricGroup")
.WithSimpleSchedule(x => x.WithIntervalInSeconds(5).RepeatForever())
.Build();
ITrigger anothertrigger = TriggerBuilder.Create()
.WithIdentity("EricAnotherTrigger", "EricGroup")
.WithSimpleSchedule(x => x.WithIntervalInSeconds(5).RepeatForever())
.Build();
await sched.ScheduleJob(job, trigger);
await sched.ScheduleJob(anotherjob, anothertrigger);
}
4):在 Main 方法中完成调用, 由于是异步处理,因此这里用 Console.ReadKey() 完成对主线程的阻塞
static void Main(string[] args)
{
TestAsyncJob();
Console.ReadKey();
}
5):最终的运行结果为,两个Job使屏幕和文件不断输出字符串
更多信息请参考如下链接:
https://wwwblogs/MingQiu/p/8568143.html
6):如果我们想将此注册为Windows Service,在对应Service启动之后自动处理对应Job,请参考如下链接:
http://wwwblogs/mingmingruyuedlut/p/9033159.html
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C#.NET频道!
版权声明:本文标题:quartz c语言,C#编程之C# 通过 Quartz .NET 实现 schedule job 的处理 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1766278669a3450934.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论