-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathQuartzExample.cs
More file actions
146 lines (122 loc) · 4.92 KB
/
QuartzExample.cs
File metadata and controls
146 lines (122 loc) · 4.92 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Quartz;
using Quartz.Impl;
namespace WindowsService1
{
class QuartzExample
{
private IScheduler sched = null;
public void Start()
{
// construct a scheduler factory
ISchedulerFactory schedFact = new StdSchedulerFactory();
// get a scheduler
sched = schedFact.GetScheduler();
sched.Start();
//{
// IJobDetail job = JobBuilder.Create<Noop_IJob>()
// .WithIdentity("myJob1", "group1")
// .Build();
// // Trigger the job to run now, and then every 40 seconds
// ITrigger trigger = TriggerBuilder.Create()
// .WithIdentity("myTrigger1", "group1")
// .StartNow()
// .WithSimpleSchedule(x => x
// .WithIntervalInSeconds(10)
// .RepeatForever())
// .Build();
// sched.ScheduleJob(job, trigger);
//}
{
IJobDetail job = JobBuilder.Create<WebRequest>()
.WithIdentity("myJob2", "group1")
.Build();
// Trigger the job to run now, and then every 40 seconds
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("myTrigger2", "group1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(10)
.RepeatForever())
.Build();
sched.ScheduleJob(job, trigger);
}
//{
// IJobDetail job = JobBuilder.Create<Noop_BaseJob>()
// .WithIdentity("myJob3", "group1")
// .Build();
// // Trigger the job to run now, and then every 40 seconds
// ITrigger trigger = TriggerBuilder.Create()
// .WithIdentity("myTrigger3", "group1")
// .StartNow()
// .WithSimpleSchedule(x => x
// .WithIntervalInSeconds(10)
// .RepeatForever())
// .Build();
// sched.ScheduleJob(job, trigger);
//}
}
public void Stop()
{
sched.Shutdown();
}
/*
* We suggest a base class like the example below, but this is an example without it.
* This would just specify the reporting transaction name as "Quartz"
*/
class Noop_IJob : IJob
{
public void Execute(IJobExecutionContext context)
{
Debug.WriteLine("Just sleeping for a couple seconds");
System.Threading.Thread.Sleep(2000);
Debug.WriteLine("Sleep over");
}
}
/*
* Making a base class for the job to easily wrap all of them with the profile tracer logic.
*/
class BaseJob : IJob
{
public void Execute(IJobExecutionContext context)
{
//Could name it by the inheriting job class name. This is definitely preferred if you have lots of different uses of the same job
var tracer = StackifyLib.ProfileTracer.CreateAsOperation("Quartz " + this.GetType().Name);
//could name it by the job name in quartz. This is fine if the job only exists once.
//for use cases where you run the same job lots of times for different clients, for example, probably makes more sense to use the job class type so they all report the same.
//var tracer = StackifyLib.ProfileTracer.CreateAsOperation("Quartz " + context.JobDetail.Name);
tracer.Exec(() => ExecuteJob(context));
}
protected virtual void ExecuteJob(IJobExecutionContext context)
{
throw new NotImplementedException("Must implement in the inheriting class");
}
}
class Noop_BaseJob : BaseJob
{
protected override void ExecuteJob(IJobExecutionContext context)
{
Debug.WriteLine("Just sleeping for a couple seconds");
System.Threading.Thread.Sleep(2000);
Debug.WriteLine("Sleep over");
}
}
class WebRequest : BaseJob
{
protected override void ExecuteJob(IJobExecutionContext context)
{
Debug.WriteLine("This example is a quartz job running every once in a while");
using (WebClient wc = new WebClient())
{
wc.DownloadString("https://www.microsoft.com/en-us/");
}
}
}
}
}