-
Notifications
You must be signed in to change notification settings - Fork 1
/
SpringQuartzSchedulerFactory.java
168 lines (151 loc) · 6.47 KB
/
SpringQuartzSchedulerFactory.java
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Copyright 2011-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.appng.application.scheduler.quartz;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import javax.servlet.ServletContext;
import org.appng.api.Environment;
import org.appng.api.Platform;
import org.appng.api.ScheduledJob;
import org.appng.api.Scope;
import org.appng.api.model.Properties;
import org.appng.api.model.Site;
import org.appng.api.support.environment.DefaultEnvironment;
import org.appng.application.scheduler.Constants;
import org.quartz.InterruptableJob;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.UnableToInterruptJobException;
import org.quartz.spi.JobFactory;
import org.quartz.spi.OperableTrigger;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.web.context.WebApplicationContext;
import lombok.extern.slf4j.Slf4j;
/**
* A {@link JobFactory} that creates Quartz {@link Job}s that are build from {@link ScheduledJob}s.
*
* @author Matthias Müller
* @author Claus Stümke
*/
@Slf4j
public class SpringQuartzSchedulerFactory extends AdaptableJobFactory {
@Autowired
private ApplicationContext applicationContext;
private AtomicInteger id = new AtomicInteger(1);
@Override
protected Job createJobInstance(TriggerFiredBundle bundle) throws Exception {
final JobDetail jobDetail = bundle.getJobDetail();
final JobKey jobKey = jobDetail.getKey();
OperableTrigger trigger = bundle.getTrigger();
JobDataMap triggerJobData = trigger.getJobDataMap();
log.debug("creating job instance for job {} with jobdata {} and trigger jobdata {}.", jobKey,
jobDetail.getJobDataMap().getWrappedMap(), triggerJobData.getWrappedMap());
org.appng.core.model.ApplicationContext appngAppContext = (org.appng.core.model.ApplicationContext) applicationContext;
final Site site = appngAppContext.getSite();
WebApplicationContext parentContext = (WebApplicationContext) appngAppContext.getParent();
ServletContext servletContext = parentContext.getServletContext();
final Environment environment = DefaultEnvironment.get(servletContext);
final String eventId = hashCode() + "-" + id.getAndIncrement();
if (jobDetail.getJobDataMap().getBoolean(Constants.JOB_HARD_INTERRUPTABLE)) {
return getInterruptableJob(jobDetail, jobKey, site, environment, eventId, triggerJobData);
} else {
return getJob(jobDetail, jobKey, site, environment, eventId, triggerJobData);
}
}
/**
* Returns a regular {@link Job} which cannot be interrupted
*/
private Job getJob(final JobDetail jobDetail, final JobKey jobKey, final Site site, final Environment environment,
final String eventId, final Map<String, Object> triggerJobData) {
return new Job() {
public void execute(JobExecutionContext context) throws JobExecutionException {
RunJobEvent runJobEvent = new RunJobEvent(eventId, jobKey, site.getName(), triggerJobData);
try {
runJobEvent.perform(environment, site);
context.setResult(runJobEvent.getJobResult());
} catch (Exception e) {
context.setResult(runJobEvent.getJobResult());
throw new JobExecutionException(e);
}
sendRunOnceEvent(jobDetail, jobKey, site, runJobEvent);
}
};
}
/**
* Returns an {@link InterruptableJob} that is is being interrupted by quartz when shutting down a {@link Scheduler}
* , e.g. on site reload. This is done by using a separate thread, which is interrupted when
* {@link InterruptableJob#interrupt()} is being called. It does not care about open resources, so use
* {@value #JOBDATA_HARD_INTERRUPTABLE}{@code =true} only for {@link ScheduledJob}s that can safely be stopped by
* interrupting its thread.
*/
private Job getInterruptableJob(final JobDetail jobDetail, final JobKey jobKey, final Site site,
final Environment environment, final String eventId, final Map<String, Object> triggerJobData) {
Properties platformProps = environment.getAttribute(Scope.PLATFORM, Platform.Environment.PLATFORM_CONFIG);
// this is a 'secret' platform-property
final Integer waitTime = platformProps.getInteger("interruptableWaitTime", 5000);
return new InterruptableJob() {
private Thread thread;
public void execute(JobExecutionContext context) throws JobExecutionException {
final RunJobEvent runJobEvent = new RunJobEvent(eventId, jobKey, site.getName(), triggerJobData);
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
runJobEvent.perform(environment, site);
context.setResult(runJobEvent.getJobResult());
} catch (Exception e) {
context.setResult(runJobEvent.getJobResult());
log.error("Caught Exception on job execution: ", e);
}
sendRunOnceEvent(jobDetail, jobKey, site, runJobEvent);
}
};
// run the job in a separate thread so it can be interrupted.
thread = new Thread(runnable, jobKey.getName());
thread.start();
boolean keepGoing = true;
while (keepGoing) {
try {
Thread.sleep(waitTime);
} catch (InterruptedException e) {
log.info("Thread " + thread.getName() + " was interrupted while sleeping");
}
keepGoing = (thread.isAlive() && !thread.isInterrupted());
}
}
public void interrupt() throws UnableToInterruptJobException {
thread.interrupt();
}
};
}
private void sendRunOnceEvent(final JobDetail jobDetail, final JobKey jobKey, final Site site,
RunJobEvent runJobEvent) {
if (jobDetail.getJobDataMap().getBoolean(Constants.JOB_RUN_ONCE)) {
log.info("job {} has option '{}' set, no event will be send.", jobKey, Constants.JOB_RUN_ONCE);
} else {
site.sendEvent(runJobEvent);
log.info("sending {}", runJobEvent);
}
}
}