QuartzLTSProxyAgent.java
7.79 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package com.github.ltsopensource.spring.quartz;
import com.github.ltsopensource.core.commons.utils.CollectionUtils;
import com.github.ltsopensource.core.commons.utils.QuietUtils;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.core.factory.NamedThreadFactory;
import com.github.ltsopensource.core.json.JSON;
import com.github.ltsopensource.core.logger.Logger;
import com.github.ltsopensource.core.logger.LoggerFactory;
import com.github.ltsopensource.jobclient.JobClient;
import com.github.ltsopensource.jobclient.JobClientBuilder;
import com.github.ltsopensource.core.properties.JobClientProperties;
import com.github.ltsopensource.jobclient.domain.Response;
import com.github.ltsopensource.tasktracker.TaskTracker;
import com.github.ltsopensource.tasktracker.TaskTrackerBuilder;
import com.github.ltsopensource.tasktracker.runner.JobRunner;
import com.github.ltsopensource.tasktracker.runner.RunnerFactory;
import org.quartz.impl.triggers.CronTriggerImpl;
import org.quartz.impl.triggers.SimpleTriggerImpl;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* @author Robert HG (254963746@qq.com) on 3/16/16.
*/
class QuartzLTSProxyAgent {
private static final Logger LOGGER = LoggerFactory.getLogger(QuartzLTSProxyAgent.class);
private QuartzLTSConfig quartzLTSConfig;
private List<QuartzJobContext> quartzJobContexts = new CopyOnWriteArrayList<QuartzJobContext>();
private AtomicBoolean ready = new AtomicBoolean(false);
public QuartzLTSProxyAgent(QuartzLTSConfig quartzLTSConfig) {
this.quartzLTSConfig = quartzLTSConfig;
}
// 开始代理
public void startProxy(List<QuartzJobContext> cronJobs) {
if (CollectionUtils.isEmpty(cronJobs)) {
return;
}
quartzJobContexts.addAll(cronJobs);
if (!ready.compareAndSet(false, true)) {
return;
}
new NamedThreadFactory(QuartzLTSProxyAgent.class.getSimpleName() + "_LazyStart").newThread(new Runnable() {
@Override
public void run() {
try {
// 3S之后启动 JobClient和TaskTracker, 为了防止有多个SchedulerFactoryBean, 从而这个方法被调用多次
QuietUtils.sleep(3000);
startProxy0();
} catch (Throwable t) {
LOGGER.error("Error on start " + QuartzLTSProxyAgent.class.getSimpleName(), t);
}
}
}).start();
}
private void startProxy0() {
// 1. 先启动 TaskTracker
startTaskTracker();
// 2. 启动JobClient并提交任务
JobClient jobClient = startJobClient();
// 3. 提交任务
submitJobs(jobClient);
}
private void startTaskTracker() {
TaskTracker taskTracker = TaskTrackerBuilder.buildByProperties(quartzLTSConfig.getTaskTrackerProperties());
taskTracker.setWorkThreads(quartzJobContexts.size());
taskTracker.setJobRunnerClass(QuartzJobRunnerDispatcher.class);
final QuartzJobRunnerDispatcher jobRunnerDispatcher = new QuartzJobRunnerDispatcher(quartzJobContexts);
taskTracker.setRunnerFactory(new RunnerFactory() {
@Override
public JobRunner newRunner() {
return jobRunnerDispatcher;
}
});
taskTracker.start();
}
private JobClient startJobClient() {
JobClientProperties jobClientProperties = quartzLTSConfig.getJobClientProperties();
jobClientProperties.setUseRetryClient(false);
JobClient jobClient = JobClientBuilder.buildByProperties(jobClientProperties);
jobClient.start();
return jobClient;
}
private void submitJobs(JobClient jobClient) {
List<Job> jobs = new ArrayList<Job>(quartzJobContexts.size());
for (QuartzJobContext quartzJobContext : quartzJobContexts) {
if (QuartzJobType.CRON == quartzJobContext.getType()) {
jobs.add(buildCronJob(quartzJobContext));
} else if (QuartzJobType.SIMPLE_REPEAT == quartzJobContext.getType()) {
jobs.add(buildSimpleJob(quartzJobContext));
}
}
LOGGER.info("=============LTS=========== Submit start");
submitJobs0(jobClient, jobs);
LOGGER.info("=============LTS=========== Submit end");
}
private Job buildCronJob(QuartzJobContext quartzJobContext) {
CronTriggerImpl cronTrigger = (CronTriggerImpl) quartzJobContext.getTrigger();
String cronExpression = cronTrigger.getCronExpression();
String description = cronTrigger.getDescription();
int priority = cronTrigger.getPriority();
String name = quartzJobContext.getName();
Job job = new Job();
job.setTaskId(name);
job.setPriority(priority);
job.setCronExpression(cronExpression);
job.setSubmitNodeGroup(quartzLTSConfig.getJobClientProperties().getNodeGroup());
job.setTaskTrackerNodeGroup(quartzLTSConfig.getTaskTrackerProperties().getNodeGroup());
job.setParam("description", description);
setJobProp(job);
return job;
}
private Job buildSimpleJob(QuartzJobContext quartzJobContext) {
SimpleTriggerImpl simpleTrigger = (SimpleTriggerImpl) quartzJobContext.getTrigger();
String description = simpleTrigger.getDescription();
int priority = simpleTrigger.getPriority();
String name = quartzJobContext.getName();
int repeatCount = simpleTrigger.getRepeatCount();
long repeatInterval = simpleTrigger.getRepeatInterval();
Job job = new Job();
job.setTaskId(name);
job.setTriggerDate(simpleTrigger.getNextFireTime());
job.setRepeatCount(repeatCount);
if (repeatCount != 0) {
job.setRepeatInterval(repeatInterval);
}
job.setPriority(priority);
job.setSubmitNodeGroup(quartzLTSConfig.getJobClientProperties().getNodeGroup());
job.setTaskTrackerNodeGroup(quartzLTSConfig.getTaskTrackerProperties().getNodeGroup());
job.setParam("description", description);
setJobProp(job);
return job;
}
private void setJobProp(Job job) {
QuartzLTSConfig.JobProperties jobProperties = quartzLTSConfig.getJobProperties();
if (jobProperties == null) {
return;
}
if (jobProperties.getMaxRetryTimes() != null) {
job.setMaxRetryTimes(jobProperties.getMaxRetryTimes());
}
if (jobProperties.getNeedFeedback() != null) {
job.setNeedFeedback(jobProperties.getNeedFeedback());
}
if (jobProperties.getRelyOnPrevCycle() != null) {
job.setRelyOnPrevCycle(jobProperties.getRelyOnPrevCycle());
}
if (jobProperties.getReplaceOnExist() != null) {
job.setReplaceOnExist(jobProperties.getReplaceOnExist());
}
}
private void submitJobs0(JobClient jobClient, List<Job> jobs) {
List<Job> failedJobs = null;
try {
Response response = jobClient.submitJob(jobs);
if (!response.isSuccess()) {
LOGGER.warn("Submit Quartz Jobs to LTS failed: {}", JSON.toJSONString(response));
failedJobs = response.getFailedJobs();
}
} catch (Throwable e) {
LOGGER.warn("Submit Quartz Jobs to LTS error", e);
failedJobs = jobs;
}
if (CollectionUtils.isNotEmpty(failedJobs)) {
// 没提交成功要重试 3S 之后重试
LOGGER.info("=============LTS=========== Sleep 3 Seconds and retry");
QuietUtils.sleep(3000);
submitJobs0(jobClient, failedJobs);
return;
}
// 如果成功了, 关闭jobClient
jobClient.stop();
}
}