-
Notifications
You must be signed in to change notification settings - Fork 1
/
RunJobEvent.java
103 lines (91 loc) · 3.8 KB
/
RunJobEvent.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
/*
* 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 org.appng.api.BusinessException;
import org.appng.api.Environment;
import org.appng.api.InvalidConfigurationException;
import org.appng.api.ScheduledJob;
import org.appng.api.ScheduledJobResult;
import org.appng.api.messaging.Event;
import org.appng.api.model.Application;
import org.appng.api.model.Site;
import org.appng.application.scheduler.Constants;
import org.appng.application.scheduler.model.JobResult;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StopWatch;
public class RunJobEvent extends Event {
private static final long serialVersionUID = 3439557609209638189L;
private static final String SCHEDULER_APPLICATION = "schedulerApplication";
private final String id;
private final JobKey jobKey;
private final Map<String, Object> triggerJobData;
private JobResult jobResult;
public RunJobEvent(String id, JobKey jobKey, String siteName, Map<String, Object> triggerJobData) {
super(siteName);
this.id = id;
this.jobKey = jobKey;
this.triggerJobData = triggerJobData;
}
public JobResult getJobResult() {
return jobResult;
}
public void perform(Environment environment, Site site) throws InvalidConfigurationException, BusinessException {
Logger logger = LoggerFactory.getLogger(RunJobEvent.class);
String appName = null;
try {
String schedulerAppName = site.getProperties().getString(SCHEDULER_APPLICATION, "appng-scheduler");
Application appngScheduler = site.getApplication(schedulerAppName);
Scheduler scheduler = appngScheduler.getBean(Scheduler.class);
JobDetail jobDetail = scheduler.getJobDetail(jobKey);
appName = jobDetail.getJobDataMap().getString(Constants.JOB_ORIGIN);
Application application = site.getApplication(appName);
String beanName = jobDetail.getJobDataMap().getString(Constants.JOB_BEAN_NAME);
ScheduledJob job = (ScheduledJob) application.getBean(beanName);
if (null == job) {
throw new BusinessException("ScheduledJob " + beanName + " not found in application " + appName);
}
job.setJobDataMap(jobDetail.getJobDataMap());
job.getJobDataMap().putAll(triggerJobData);
this.jobResult = new JobResult(new ScheduledJobResult(), appName, site.getName(), jobKey.getName());
StopWatch sw = new StopWatch();
sw.start();
job.execute(site, application);
sw.stop();
this.jobResult.setScheduledJobResult(job.getResult());
Object[] args = new Object[] { jobKey, appName, site.getName(), sw.getTotalTimeMillis() };
logger.debug("executing job {} for application {} in site {} took {}ms", args);
} catch (SchedulerException e) {
throw new BusinessException(e);
} catch (Exception e) {
Object[] args = new Object[] { jobKey, appName, site.getName() };
logger.warn("error while executing job {} for application {} in site {} ", args);
if (e instanceof BusinessException) {
throw (BusinessException) e;
}
throw new BusinessException(e);
}
}
@Override
public String toString() {
return getClass().getName() + "#" + id + "[site: " + getSiteName() + ", job: " + jobKey + "]";
}
}