1 package org.apache.onami.scheduler;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static java.lang.String.format;
23 import static java.util.TimeZone.getDefault;
24 import static org.apache.onami.scheduler.Scheduled.DEFAULT;
25 import static org.quartz.CronScheduleBuilder.cronSchedule;
26 import static org.quartz.JobBuilder.newJob;
27 import static org.quartz.JobKey.jobKey;
28 import static org.quartz.TriggerBuilder.newTrigger;
29 import static org.quartz.TriggerKey.triggerKey;
30 import static org.quartz.utils.Key.DEFAULT_GROUP;
31
32 import java.util.TimeZone;
33
34 import javax.inject.Inject;
35
36 import org.quartz.Job;
37 import org.quartz.JobKey;
38 import org.quartz.Scheduler;
39 import org.quartz.Trigger;
40 import org.quartz.TriggerKey;
41
42 import com.google.inject.ProvisionException;
43
44
45
46
47
48 public final class JobSchedulerBuilder {
49
50
51
52
53 private final Class<? extends Job> jobClass;
54
55
56
57
58 private String jobName = DEFAULT;
59
60
61
62
63 private String jobGroup = DEFAULT_GROUP;
64
65
66
67
68
69
70 private boolean requestRecovery = false;
71
72
73
74
75
76 private boolean storeDurably = false;
77
78
79
80
81 private String triggerName = DEFAULT;
82
83
84
85
86 private String triggerGroup = DEFAULT_GROUP;
87
88
89
90
91 private String cronExpression;
92
93
94
95
96
97 private TimeZone timeZone = getDefault();
98
99
100
101
102
103
104 private int priority = 0;
105
106
107
108
109
110
111 private Trigger trigger;
112
113
114
115
116
117
118
119 private boolean updateExistingTrigger = false;
120
121
122
123
124
125
126
127
128 JobSchedulerBuilder( final Class<? extends Job> jobClass )
129 {
130 this.jobClass = jobClass;
131 }
132
133
134
135
136
137
138
139 public JobSchedulerBuilder withJobName( String jobName )
140 {
141 this.jobName = jobName;
142 return this;
143 }
144
145
146
147
148
149
150
151 public JobSchedulerBuilder withJobGroup( String jobGroup )
152 {
153 this.jobGroup = jobGroup;
154 return this;
155 }
156
157
158
159
160
161
162
163
164
165 public JobSchedulerBuilder withRequestRecovery( boolean requestRecovery )
166 {
167 this.requestRecovery = requestRecovery;
168 return this;
169 }
170
171
172
173
174
175
176
177
178 public JobSchedulerBuilder withStoreDurably( boolean storeDurably )
179 {
180 this.storeDurably = storeDurably;
181 return this;
182 }
183
184
185
186
187
188
189
190 public JobSchedulerBuilder withTriggerName( String triggerName )
191 {
192 this.triggerName = triggerName;
193 return this;
194 }
195
196
197
198
199
200
201
202 public JobSchedulerBuilder withTriggerGroup( String triggerGroup )
203 {
204 this.triggerGroup = triggerGroup;
205 return this;
206 }
207
208
209
210
211
212
213
214 public JobSchedulerBuilder withCronExpression( String cronExpression )
215 {
216 this.cronExpression = cronExpression;
217 return this;
218 }
219
220
221
222
223
224
225
226
227
228 public JobSchedulerBuilder withTimeZone( TimeZone timeZone )
229 {
230 this.timeZone = timeZone;
231 return this;
232 }
233
234
235
236
237
238
239
240
241
242 public JobSchedulerBuilder withPriority( int priority )
243 {
244 this.priority = priority;
245 return this;
246 }
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261 public JobSchedulerBuilder withTrigger(Trigger trigger)
262 {
263 this.trigger = trigger;
264 return this;
265 }
266
267
268
269
270
271
272
273
274 public JobSchedulerBuilder updateExistingTrigger()
275 {
276 this.updateExistingTrigger = true;
277 return this;
278 }
279
280
281
282
283
284
285
286
287
288
289 @Inject
290 public void schedule( Scheduler scheduler )
291 throws Exception
292 {
293 if ( cronExpression == null && trigger == null )
294 {
295 throw new ProvisionException( format( "Impossible to schedule Job '%s' without cron expression",
296 jobClass.getName() ) );
297 }
298 if ( cronExpression != null && trigger != null )
299 {
300 throw new ProvisionException( format( "Impossible to schedule Job '%s' with cron expression " +
301 "and an associated Trigger at the same time", jobClass.getName() ) );
302 }
303
304 JobKey jobKey = jobKey( DEFAULT.equals( jobName ) ? jobClass.getName() : jobName, jobGroup );
305 TriggerKey triggerKey = triggerKey( DEFAULT.equals( triggerName ) ? jobClass.getCanonicalName() : triggerName, triggerGroup );
306
307 if ( updateExistingTrigger && scheduler.checkExists( triggerKey ) ) {
308 scheduler.unscheduleJob( triggerKey );
309 }
310
311 scheduler.scheduleJob( newJob( jobClass )
312 .withIdentity( jobKey )
313 .requestRecovery( requestRecovery )
314 .storeDurably( storeDurably ).build(),
315 ( trigger == null ) ?
316 newTrigger()
317 .withIdentity( triggerKey )
318 .withSchedule( cronSchedule( cronExpression )
319 .inTimeZone( timeZone ) )
320 .withPriority( priority )
321 .build()
322 : trigger);
323 }
324
325 }