zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    子线程导致 Windows 服务停止的情况(Topshelf 结合 Quartz.NET)
    ASP.NET Web API 2 使用 DelegatingHandler(委托处理程序)实现签名认证
    ASP.NET Web API 2 使用 AuthorizationFilter(授权过滤器)实现 Basic 认证
    聚合函数查询语句
    SQL SERVER数据库常用命令
    Easyui-datebox日期控件增加清空按钮
    用sql语句查出来字段里包含某个字符串的所有记录
    String 转化成java.sql.Date和java.sql.Time(转载)
    常见的 HTML 事件
    JavaScript 变量中给数值加引号的问题
  • 原文地址:https://www.cnblogs.com/shousiji/p/7602594.html
Copyright © 2011-2022 走看看