Quartz.Net和队列应用demo

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Threading;
 4 
 5 namespace ConsoleApplication1
 6 {
 7     public static class Class1
 8     {
 9         static Queue<string> MsgQueue = new Queue<string>();
10         //创建一个没有其他用途的对象作为锁,微软官方推荐做法
11         private static Object thisLock = new Object();
12 
13         static Class1()
14         {
15             ThreadPool.QueueUserWorkItem(q =>
16             {
17                 while (true)
18                 {
19                     lock (thisLock)
20                     {
21                         if (MsgQueue.Count > 0)
22                         {
23                             string msg = MsgQueue.Dequeue();
24                             //把内容记录起来之类的操作...
25                         }
26                         else
27                         {
28                             //队列中没有东西,就让线程休息下
29                             Thread.Sleep(3000);
30                         }
31                     }
32                 }
33             });
34         }
35 
36         public static void Insert(string msg)
37         {
38             lock (thisLock)
39             {
40                 MsgQueue.Enqueue(msg);
41             }
42         }
43     }
44 }
队列demo

 

Quartz.Net和队列结合控制台demo

 1 using Quartz;
 2 using Quartz.Impl;
 3 
 4 namespace ConsoleApplication1
 5 {
 6     class Program
 7     {
 8         static void Main(string[] args)
 9         {
10             for (int i = 1; i <= 10; i++)
11             {
12                 TestJob.Insert($"str{i} ");
13             }
14 
15             //计划者
16             IScheduler sched = StdSchedulerFactory.GetDefaultScheduler();
17             //作业
18             IJobDetail job1 = new JobDetailImpl("Job1", "JobGroup1", typeof(TestJob));
19             //触发器
20             ITrigger trigger1 = TriggerBuilder.Create()
21                 .WithIdentity("Trigger1", "TriggerGroup1")
22                 .StartNow() //现在开始
23                 .WithSimpleSchedule(x => x
24                     .WithIntervalInSeconds(5) //5秒一次
25                     .RepeatForever()) //不断重复
26                 .Build();
27 
28             ////存值
29             //job1.JobDataMap.Add("key1", "value1");
30 
31             sched.ScheduleJob(job1, trigger1);
32             sched.Start();
         Console.Read();
33 } 34 } 35 }

 

 1 using Quartz;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Threading;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     public class TestJob : IJob //想成为作业就要实现此接口
 9     {
10         static Queue<string> MsgQueue = new Queue<string>();
11         private static Object thisLock = new Object();
12 
13         public static void Insert(string msg)
14         {
15             lock (thisLock)
16             {
17                 MsgQueue.Enqueue(msg);
18             }
19         }
20 
21         public void Execute(IJobExecutionContext context)
22         {
23             lock (thisLock)
24             {
25                 if (MsgQueue.Count > 0)
26                 {
27                     string msg = MsgQueue.Dequeue();
28                     Console.WriteLine(msg + DateTime.Now.ToString());
29                 }
30                 else
31                 {
32                     //队列中没有东西,就让线程休息下
33                     Thread.Sleep(3000);
34                 }
35             }
36 
37             ////取值
38             //JobDataMap dataMap = context.JobDetail.JobDataMap;
39             //string content = dataMap.GetString("key1");
40             //Console.WriteLine("作业执行,jobSays:" + content);
41         }
42     }
43 }

 

转载于:https://www.cnblogs.com/shousiji/p/7602594.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值