ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

记录Quarter的基本使用

2019-12-16 13:04:06  阅读:502  来源: 互联网

标签:基本 CancellationToken Task 记录 System scheduler using Quarter public


原文:记录Quarter的基本使用

  1. using Quartz;
  2. using Quartz.Impl;
  3. using Quartz.Impl.Matchers;
  4. using Quartz.Logging;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.Specialized;
  8. using System.Data.Entity.Migrations;
  9. using System.Linq;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Transactions;
  13. using System.Web;
  14. using WebApplication3.Models;
  15. using WebApplication3.Service;
  16. namespace WebApplication3
  17. {
  18. public static class QuartzHelper
  19. {
  20. public static async Task RunProgramRunExample()
  21. {
  22. try
  23. {
  24. // Grab the Scheduler instance from the Factory
  25. NameValueCollection props = new NameValueCollection
  26. {
  27. { "quartz.serializer.type", "binary" }
  28. };
  29. StdSchedulerFactory factory = new StdSchedulerFactory(props);
  30. IScheduler scheduler = await factory.GetScheduler();
  31. // and start it off
  32. await scheduler.Start();
  33. // define the job and tie it to our HelloJob class
  34. IJobDetail job = JobBuilder.Create<HelloJob>()
  35. .WithIdentity("job1", "group1")
  36. .Build();
  37. // Trigger the job to run now, and then repeat every 10 seconds
  38. ITrigger trigger = TriggerBuilder.Create()
  39. .WithIdentity("trigger1", "group1")
  40. //将在以后的整点触发
  41. .StartAt(DateBuilder.EvenHourDate(null))
  42. //.StartAt(DateBuilder.FutureDate(1, IntervalUnit.Day))
  43. //.StartNow()
  44. //.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(19, 36))//每天几点几分触发
  45. .WithSimpleSchedule(x => x
  46. .WithIntervalInHours(1)//每1小时触发
  47. //.WithIntervalInMinutes(1)//每分钟
  48. //.WithIntervalInSeconds(10)
  49. .RepeatForever())
  50. .Build();
  51. ///监听任务
  52. //scheduler.ListenerManager.AddJobListener(new MyJobListener(), KeyMatcher<JobKey>.KeyEquals(new JobKey("job1", "group1")));
  53. scheduler.ListenerManager.AddJobListener(new MyJobListener(), GroupMatcher<JobKey>.AnyGroup());
  54. // Tell quartz to schedule the job using our trigger
  55. await scheduler.ScheduleJob(job, trigger);
  56. // and last shut down the scheduler when you are ready to close your program
  57. //await scheduler.Shutdown();
  58. }
  59. catch (SchedulerException se)
  60. {
  61. Console.WriteLine(se);
  62. }
  63. }
  64. }
  65. public class MyJobListener : IJobListener
  66. {
  67. public string Name
  68. {
  69. get
  70. {
  71. return "监听任务";
  72. }
  73. }
  74. public Task JobToBeExecuted(IJobExecutionContext context, CancellationToken cancellationToken = default(CancellationToken))
  75. {
  76. return Task.Run(() =>
  77. {
  78. JobToBeExecuted();
  79. });
  80. }
  81. public Task JobExecutionVetoed(IJobExecutionContext context, CancellationToken cancellationToken = default(CancellationToken))
  82. {
  83. return Task.Run(() =>
  84. {
  85. JobExecutionVetoed();
  86. });
  87. }
  88. public Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default(CancellationToken))
  89. {
  90. return Task.Run(() =>
  91. {
  92. JobWasExecuted();
  93. });
  94. }
  95. /// <summary>
  96. /// 执行前
  97. /// </summary>
  98. public void JobToBeExecuted()
  99. {
  100. }
  101. /// <summary>
  102. /// 触发失效
  103. /// </summary>
  104. public void JobExecutionVetoed()
  105. {
  106. string path4 = System.AppDomain.CurrentDomain.BaseDirectory;
  107. new Common.CommonFun().WriteLog("位置:QuartzHelper,定时器触发失效!", "autoUpdate");
  108. }
  109. /// <summary>
  110. /// 触发完成
  111. /// </summary>
  112. public void JobWasExecuted()
  113. {
  114. }
  115. }
  116. /// <summary>
  117. /// 定时任务
  118. /// </summary>
  119. public class HelloJob : IJob
  120. {
  121. public async Task Execute(IJobExecutionContext context)
  122. {
  123. await Task.Run(() =>
  124. {
  125. });
  126. }
  127. }
  128. }

记录Quartz的基本使用

看月亮的向月葵 发布了7 篇原创文章 · 获赞 2 · 访问量 6323 私信 关注

标签:基本,CancellationToken,Task,记录,System,scheduler,using,Quarter,public
来源: https://www.cnblogs.com/lonelyxmas/p/12048544.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有