本文github链接

https://github.com/sunshuaize/cnBlogDemos/tree/master/Quartz.Net%20%E4%BB%BB%E5%8A%A1%E8%B0%83%E5%BA%A6%E4%B9%8B%E7%AE%80%E5%8D%95%E4%BB%BB%E5%8A%A1(1)/DayWorkClose

下面的代码对应这个项目看

三个对象

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. 项目一:第十四天 1.在realm中动态授权 2.Shiro整合ehcache 缓存realm中授权信息 3.动态展示菜单数据 4.Quartz定时任务调度框架—Spring整合javamail发送邮件 5.基于poi实现分区导出

    1 Shiro整合ehCache缓存授权信息 当需要进行权限校验时候:四种方式url拦截.注解.页面标签.代码级别,当需要验证权限会调用realm中的授权方法   Shiro框架内部整合好缓存管理器, ...

  2. Quartz.Net 任务调度

    基于ASP.NET MVC(C#)和Quartz.Net组件实现的定时执行任务调度 在之前的文章<推荐一个简单.轻量.功能非常强大的C#/ASP.NET定时任务执行管理器组件–FluentSch ...

  3. Spring Quartz实现任务调度

    任务调度 在企业级应用中,经常会制定一些"计划任务",即在某个时间点做某件事情 核心是以时间为关注点,即在一个特定的时间点,系统执行指定的一个操作 任务调度涉及多线程并发.线程池维 ...

  4. Quartz实现任务调度

    一.任务调度概述 在企业级应用中,经常会制定一些"计划任务",即在某个时间点做某件事情,核心是以时间为关注点,即在一个特定的时间点,系统执行指定的一个操作,任务调度涉及多线程并发. ...

  5. quartz.net任务调度:源码及使用文档

    目录: 1.quartz.net任务调度:源码及使用文档 2.quartz.net插件类库封装 前言 前段时间把自己封装quartz.net 类库的过程总结到博客园,有网友想要看一下源码,所以就把源码 ...

  6. 项目ITP(五) spring4.0 整合 Quartz 实现任务调度

    前言 系列文章:[传送门] 项目需求: 二维码推送到一体机上,给学生签到扫描用.然后需要的是 上课前20分钟 ,幸好在帮带我的学长做 p2p 的时候,接触过.自然 quartz 是首选.所以我就配置了 ...

  7. Java&Quartz实现任务调度

    目录 Java&Quartz实现任务调度 1.Quartz的作用 2.预备 3.Quartz核心 3.1.Job接口 3.2.JobDetail类 3.3 JobExecutionContex ...

  8. Quartz.Net任务调度框架

    Quartz.Net是一个开源的任务调度框架,非常强大,能够通过简单的配置帮助我们定时具体的操作. 相对于我们用的线程里面while(true)然后sleep来执行某个操作,应该算的上是高端,大气,上 ...

  9. ASP.NET MVC5 实现基于Quartz.NET任务调度

    工作之余.技术?.记是不可能记住的. 只有写点东西 才能维持得了生活这样子的.好早就像写一篇关于任务调度的文章.终究是太懒了 一.Quartz.NET介绍 Quartz.NET是一个强大.开源.轻量的 ...

随机推荐

  1. CentOS6.8 安装/升级Python2.7.x,并安装最新setuptools、pip、fabric程序总结

    最终靠谱的可借鉴文档: 1.python官网 2.http://lovesoo.org/python-fabric-yuan-cheng-zi-dong-bu-shu-jian-jie.html 3. ...

  2. 自建云存储:Nextcloud vs. ownCloud vs. Seafile

    Self-hosted Cloud Storage: Nextcloud vs. ownCloud vs. Seafile By Ashutosh KS in Hosting. Updated on ...

  3. 多次读取HttpEntity内容

    有时,需要重复读取HttpEntity,直接使用是行不通的,这时需要使用BufferedHttpEntity类将其进行包装一下. public static void main(String[] ar ...

  4. django+nginx+uwsgi_cent0s7.4 部署

    django+nginx+uwsgi_cent0s7.4 部署 几条命令 # 查看是否有 uwsgi 相关的进程 ps -aux|grep "uwsgi" # 杀死有关 uwsgi ...

  5. JavaScript中的编码解码

    1.URI 统一资源标识符(URI)是一个用于标识某一互联网资源名称的字符串.,该种标识允许用户对任何(包括本地和互联网)的资源通过特定的协议进行交互操作.Web上可用的每种资源 -HTML文档.图像 ...

  6. keil c51 不能使用:Go to Definition of....的解决方法 STC51

    keil c51 不能使用:Go to Definition of....的解决方法 达到的目标如下图所示: 解决方法为 :在工程栏右键单击进入Manage Components ,然后点确定,前提是 ...

  7. 【HDOJ6609】Find the answer(线段树)

    题意:给定一个n个正整数的数列,第i项为w[i],对于每个i,你要从[1,i-1]中选择一些变成0,使得变化后[1,i]的总和小于m,每次询问最少要变几个 n<=2e5,m<=1e9,1& ...

  8. Distinctive Character

    Distinctive Character Sol bfs寻找最优解. 考虑一开始把他给的状态加进队列里,这些状态的答案都是0. 每次枚举不同的一位拓展,同时要保证这个新状态没有出现过. 效率O(2^ ...

  9. [CSP-S模拟测试]:Tourist Attractions(简单图论+bitset)

    题目描述 在美丽的比特镇一共有$n$个景区,编号依次为$1$到$n$,它们之间通过若干条双向道路连接.$Byteasar$慕名来到了比特镇旅游,不过由于昂贵的门票费,他只能负担起$4$个景区的门票费. ...

  10. sql:CallableStatement执行存储过程

    /** * 使用CablleStatement调用存储过程 * @author APPle * */ public class Demo1 { /** * 调用带有输入参数的存储过程 * CALL p ...