博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
任务调度~Quartz.net实现简单的任务调试
阅读量:4456 次
发布时间:2019-06-08

本文共 3887 字,大约阅读时间需要 12 分钟。

任务调度类似于sqlserver中的作业,即按周期性执行某个程序,代码段,或者某种服务,在JAVA环境中出现了Quartz,它可以简单的实现任务的调试,而像lucene一样,它会有对于的.net版本,Quartz.net,今天我们来做一个简单的实验,其时很简单的实验:

环境:.net4.0+mvc3

功能:每1分钟去向一个文件里写日志(当然,如果你要调用某个服务,只要让它实现IJob接口即可。

所需要的程序集

首先在WEB.Config的configuration节点里做一些必要的配置

1   
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 18
19
20 21
22
23
24 25
26
27

在global.asax.cs里添加调用和取消调用的代码

1         protected void Application_Start()2         {3            WriteLogScheduler.Instance.Start();4         }5 6         protected void Application_End(object sender, EventArgs e)7         {8             WriteLogScheduler.Instance.Stop();9         }

所要调用的任务,它需要实现IJob接口

1     ///  2     /// 要调度的功能模块 3     ///  4     public class WriteLogJob : IJob 5     { 6         public void Execute(IJobExecutionContext context) 7         { 8             string fileLogPath = AppDomain.CurrentDomain.BaseDirectory; 9             string fileLogName = "TestQuartz_" + DateTime.Now.ToLongDateString() + "_log.txt";10             FileInfo finfo = new FileInfo(fileLogPath + fileLogName);11             using (FileStream fs = finfo.OpenWrite())12             {13                 //根据上面创建的文件流创建写数据流 14                 StreamWriter strwriter = new StreamWriter(fs);15                 //设置写数据流的起始位置为文件流的末尾 16                 strwriter.BaseStream.Seek(0, SeekOrigin.End);17                 //写入相关记录信息18                 strwriter.WriteLine("发生时间: " + DateTime.Now.ToString());19                 strwriter.WriteLine("---------------------------------------------");20                 strwriter.WriteLine();21                 //清空缓冲区内容,并把缓冲区内容写入基础流 22                 strwriter.Flush();23                 strwriter.Close();24                 fs.Close();25             }26         }27 28     }

添加调用任务的代码,Quartz服务核心代码

1  public class WriteLogScheduler 2     { 3  4         static ISchedulerFactory _sf = new StdSchedulerFactory(); 5         static IScheduler _sched = _sf.GetScheduler(); 6         static WriteLogScheduler _instance = null; 7         static object lockObj = new object(); 8  9         /// 10         /// 线程安全的单例对象11         /// 12         public static WriteLogScheduler Instance13         {14             get15             {16                 if (_instance == null)17                 {18                     lock (lockObj)19                     {20                         if (_instance == null)21                         {22                             _instance = new WriteLogScheduler();23                         }24                     }25                 }26                 return _instance;27             }28         }29 30         public void Start()31         {32             ILog log = LogManager.GetLogger(typeof(WriteLogScheduler));33             DateTimeOffset runTime = DateBuilder.EvenMinuteDate(DateTimeOffset.UtcNow);34             // define the job and tie it to our HelloJob class35             IJobDetail job = JobBuilder.Create
()36 .WithIdentity("job1", "group1")37 .Build();38 // Trigger the job to run on the next round minute39 ITrigger trigger = TriggerBuilder.Create()40 .WithIdentity("trigger1", "group1")41 .StartAt(runTime)42 .Build();43 // Tell quartz to schedule the job using our trigger44 _sched.ScheduleJob(job, trigger);45 _sched.Start();46 }47 public void Stop()48 {49 _sched.Shutdown(true);50 }51 52 }

运行程序,即可看到结果了,下一讲,我将把XML配置信息加进来,以减少程序的松耦性。

转载于:https://www.cnblogs.com/waiwai1015/p/4744880.html

你可能感兴趣的文章
bootstrap-table 分页
查看>>
使用本机IP调试web项目
查看>>
【Java面试题】58 char型变量中能不能存贮一个中文汉字?为什么?
查看>>
C++ Primer 第六章 函数
查看>>
交互设计算法基础(3) - Quick Sort
查看>>
Ubuntu各种软件的安装
查看>>
2019春第四次课程设计实验报告
查看>>
智能社的邀请码
查看>>
算法与分析 统计数字问题和整数因子分解问题?
查看>>
变量提升
查看>>
Thinkphp3.2多站点共用S方法缓存
查看>>
高精度(✚▬✖)法,↓↓↓
查看>>
谜题88:原生类型的处理
查看>>
boost::asio::detail::epoll_reactor::start_op的崩溃问题
查看>>
一句话题解
查看>>
第四次作业-树
查看>>
[转]调试技巧
查看>>
js判断手机访问网站自动跳转到手机版
查看>>
一个 tiny Lisp 语言解释器的实现
查看>>
【翻译】如何给tomcat配置memcached-session-manager
查看>>