Quartz.Net 任务调度之简单任务(1)
本文github链接
下面的代码对应这个项目看
三个对象
1.IScheduler
2.IJobDetail
3.ITrigger
第一步,选创建一个任务单元(修建火车道)
#region 创建单元 (时间轴/载体)
StdSchedulerFactory factory = new StdSchedulerFactory();
IScheduler scheduler = await factory.GetScheduler();
await scheduler.Start();
#endregion
第二步,创建一个任务(造火车)
#region job
IJobDetail jobDetail = JobBuilder.Create<HelloJob>()
.WithDescription("this is a job")
.WithIdentity("job1", "group1")
.Build();
#endregion
WithIdentity():
第一个参数(job1):给当前任务起个名字(行动代号)
第二个参数(group1):分组,因为这个任务可以是多个,也可以是多组,分类一下
HelloJob.cs
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace DayWorkCloseService.DayWorkClose
{
public class HelloJob : IJob
{
public async Task Execute(IJobExecutionContext context)
{
await Task.Run(() => { Console.WriteLine($@"{"张翼德"}"+DateTime.Now + ""); });
}
}
}
第三步 配置时间策略(确定发车时间)
#region 时间策略
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds()
.WithRepeatCount())
.Build();
#endregion
WithIdentity():同上
startNow():立即开始一次
WithIntervalInSeconds():一秒一次
WithRepeatCount():最大执行10000次
4. 把任务和时间策略承载到任务单元中 (喝壮行酒,准备上路)
await scheduler.ScheduleJob(jobDetail, trigger);
完整的代码 这个是我新建了一个类库,调用不在这里
using DayWorkCloseService.DayWorkClose;
using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace DayWorkCloseService
{
public class DayWorkCloseManager
{ public static async Task Init()
{
try
{
#region 创建单元 (时间轴/载体)
StdSchedulerFactory factory = new StdSchedulerFactory();
IScheduler scheduler = await factory.GetScheduler();
await scheduler.Start();
#endregion #region job
IJobDetail jobDetail = JobBuilder.Create<HelloJob>()
.WithDescription("this is a job")
.WithIdentity("job1", "group1")
.Build();
#endregion #region 时间策略
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds()
.WithRepeatCount())
.Build();
#endregion // Tell quartz to schedule the job using our trigger
await scheduler.ScheduleJob(jobDetail, trigger); }
catch (SchedulerException se)
{
Console.WriteLine(se);
}
}
}
}
准备都做完了,现在开始调用,就一句话,不对,两句
DayWorkCloseManager.Init().GetAwaiter().GetResult(); Console.Read();//这个一定要
done
Quartz.Net 任务调度之简单任务(1)的更多相关文章
- 项目一:第十四天 1.在realm中动态授权 2.Shiro整合ehcache 缓存realm中授权信息 3.动态展示菜单数据 4.Quartz定时任务调度框架—Spring整合javamail发送邮件 5.基于poi实现分区导出
1 Shiro整合ehCache缓存授权信息 当需要进行权限校验时候:四种方式url拦截.注解.页面标签.代码级别,当需要验证权限会调用realm中的授权方法 Shiro框架内部整合好缓存管理器, ...
- Quartz.Net 任务调度
基于ASP.NET MVC(C#)和Quartz.Net组件实现的定时执行任务调度 在之前的文章<推荐一个简单.轻量.功能非常强大的C#/ASP.NET定时任务执行管理器组件–FluentSch ...
- Spring Quartz实现任务调度
任务调度 在企业级应用中,经常会制定一些"计划任务",即在某个时间点做某件事情 核心是以时间为关注点,即在一个特定的时间点,系统执行指定的一个操作 任务调度涉及多线程并发.线程池维 ...
- Quartz实现任务调度
一.任务调度概述 在企业级应用中,经常会制定一些"计划任务",即在某个时间点做某件事情,核心是以时间为关注点,即在一个特定的时间点,系统执行指定的一个操作,任务调度涉及多线程并发. ...
- quartz.net任务调度:源码及使用文档
目录: 1.quartz.net任务调度:源码及使用文档 2.quartz.net插件类库封装 前言 前段时间把自己封装quartz.net 类库的过程总结到博客园,有网友想要看一下源码,所以就把源码 ...
- 项目ITP(五) spring4.0 整合 Quartz 实现任务调度
前言 系列文章:[传送门] 项目需求: 二维码推送到一体机上,给学生签到扫描用.然后需要的是 上课前20分钟 ,幸好在帮带我的学长做 p2p 的时候,接触过.自然 quartz 是首选.所以我就配置了 ...
- Java&Quartz实现任务调度
目录 Java&Quartz实现任务调度 1.Quartz的作用 2.预备 3.Quartz核心 3.1.Job接口 3.2.JobDetail类 3.3 JobExecutionContex ...
- Quartz.Net任务调度框架
Quartz.Net是一个开源的任务调度框架,非常强大,能够通过简单的配置帮助我们定时具体的操作. 相对于我们用的线程里面while(true)然后sleep来执行某个操作,应该算的上是高端,大气,上 ...
- ASP.NET MVC5 实现基于Quartz.NET任务调度
工作之余.技术?.记是不可能记住的. 只有写点东西 才能维持得了生活这样子的.好早就像写一篇关于任务调度的文章.终究是太懒了 一.Quartz.NET介绍 Quartz.NET是一个强大.开源.轻量的 ...
随机推荐
- Python的list中的选取范围
a = [1,2,3,4,5,6,7,8,9,10] a[0:1] = [1] a[0:2] = [1,2] 包含开头,不包含结尾. a [:-1]: 从头一直到最后一个元素a[-1],但不包含最后一 ...
- Spring Transaction Isolation
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11530702.html Reference http://docs.spring.io/spring/ ...
- JAVA代码覆盖率采集与分析方案
原文地址-> http://m.blog.csdn.net/article/details?id=48688763
- JS中字符串的常见属性及方法
1.属性 1.1.length var txt = "abc 123"; console.log(txt.length); 2.方法 2.1.返回字符位置(indexOf()) 该 ...
- HDU 6038 Function —— 2017 Multi-University Training 1
Function Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total ...
- asd的甩锅计划
asd的甩锅计划 时间限制: 1 Sec 内存限制: 128 MB提交: 177 解决: 19[提交][状态] 题目描述 大家对hdu上面的畅通工程系列一定很熟悉了吧.比如如下一段,就是畅通工程里 ...
- phpredis报错信息:protocol error, got 'o' as reply type byte解决方案
今天在前端调用PHP的接口时,有报错信息为:protocol error, got 'o' as reply type byte另外此错误有几率会重现,并不是必现的.十分疑惑,遂百度一下,发现是red ...
- 彻底搞定C指针-函数名与函数指针
函数名与函数指针 一 通常的函数调用 一个通常的函数调用的例子://自行包含头文件 void MyFun(int x); //此处的申明也可写成:void MyFun( int ); int mai ...
- QTP--启动IE浏览器的三种方式
第一种方式 创建浏览器对象模式 如果提示无法创建对象时需要先打开对象. Set ie = CreateObject("InternetExplorer.Application" ...
- 13. Jmeter-定时器
Jmeter-定时器介绍与使用 固定定时器 Uniform Random Timer Precise Throughput Timer Constant Throughput Timer 高斯随机定时 ...