QuratznetTest.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using Microsoft.Extensions.Caching.Memory;
  2. using Quartz;
  3. using Quartz.Impl;
  4. using System;
  5. using System.Diagnostics;
  6. using System.Threading.Tasks;
  7. namespace rediswebapiTest
  8. {
  9. public class QuratznetTest
  10. {
  11. public QuratznetTest()
  12. {
  13. StartAsync();
  14. }
  15. public async void StartAsync()
  16. {
  17. StdSchedulerFactory factory = new StdSchedulerFactory();
  18. // get a scheduler
  19. IScheduler scheduler = await factory.GetScheduler();
  20. await scheduler.Start();
  21. // define the job and tie it to our HelloJob class
  22. Guid guid = Guid.NewGuid();
  23. IJobDetail job = JobBuilder.Create<HelloJob>()
  24. .WithIdentity(guid.ToString())
  25. .Build();
  26. guid = Guid.NewGuid();
  27. // Trigger the job to run now, and then every 40 seconds
  28. ITrigger trigger = TriggerBuilder.Create()
  29. .WithIdentity(guid.ToString())
  30. .StartNow()
  31. .WithSimpleSchedule(x => x
  32. .WithIntervalInSeconds(1)
  33. .RepeatForever())
  34. .Build();
  35. await scheduler.ScheduleJob(job, trigger);
  36. }
  37. }
  38. public class HelloJob : IJob
  39. {
  40. private IMemoryCache _cache;
  41. public HelloJob(IMemoryCache cache)
  42. {
  43. _cache = cache;
  44. }
  45. public async Task Execute(IJobExecutionContext context)
  46. {
  47. //_cache.TryGetValue();
  48. Debug.WriteLine(DateTime.Now);
  49. }
  50. }
  51. }