P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package foundation.schedule;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;
 
import foundation.action.ActionContext;
import foundation.action.SingletonActionProvider;
import foundation.dao.DataReader;
import foundation.dao.DataWriter;
import foundation.server.config.Configer;
import foundation.util.Util;
 
 
public class ScheduleCenter extends SingletonActionProvider {
    
    public static boolean Test = false;
    public static boolean Terminated = false;
    protected static Logger logger;
    private static ScheduleCenter instance;
    private static Scheduler scheduler;
    private ScheduleBucket scheduleBucket;
    
    static {
        logger = LogManager.getLogger(ScheduleCenter.class);
        
        try {
            String config = Configer.getPath_Config() + "schedule.properties";
            SchedulerFactory factory = new StdSchedulerFactory(config);
            scheduler = factory.getScheduler();    
        } 
        catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public static synchronized ScheduleCenter getInstance() {
        if (instance == null) {
            instance = new ScheduleCenter();
        }
        
        return instance;
    }
    
    @Override
    protected void publishMethod() {
        //1. 启动定时任务
        addMethod("startup");
        
        //2. 停止定时任务
        addMethod("shutdown");
        
        //3. 暂停定时任务
        addMethod("pause");
        
        //4. 获取定时任务列表
        addMethod("getJobs");
        
        //5.根据JobName执行一次定时任务
        addMethod("executeJob");
        
        //6.保存Schedual
        addMethod("reloadSchedual");
        
        //7.根据JobName停止定时任务
        addMethod("pauseOneJob");
        
        //8.新增定时任务
        addMethod("appendOneJob");
        
    }
 
    public void startup() {
        if (scheduler == null) {
            return;
        }
        
        try {
            Terminated = false;
            
            if (scheduler.isStarted()) {
                return;
            }
            
            scheduler.start();
        } 
        catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
        }
    }
 
    public void shutdown(ActionContext context) {
        if (scheduler == null) {
            return;
        }
        
        try {
            Terminated = true;
            scheduler.shutdown();
        }
        catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
        }
    }
    
    public void pause(ActionContext context) {
        if (scheduler == null) {
            return;
        }
        
        try {
            Terminated = false;
            
            if (!scheduler.isStarted()) {
                return;
            }
            
            scheduler.pauseAll();
        } 
        catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
        }
    }
    
    public void getJobs(ActionContext context) {
        DataWriter dataWriter = context.getDataWriter();
        dataWriter.addValue(scheduleBucket);
    }
    
    public void loadJobs(ScheduleBucket bucket) {
        if (scheduler == null) {
            return;
        }
        
        try {
            //1. 如果已经启动,需要先关闭
            if (scheduler.isStarted()) {
                scheduler.shutdown();
            }
            
            //2. 如果已经加载,需要先清空
            scheduler.clear();
 
            //3. 保存 Bucket,将 Schedule Job 添加到 Quartz 中
            this.scheduleBucket = bucket;
            
            for (ScheduleBatch batch: bucket) {
                for (ScheduleJob job: batch) {
                    appendOneJob(job);
                }
            }
        } 
        catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
        }
    }
 
     public void reloadSchedual(ActionContext context) throws Exception {
        ScheduleLoader.load();
    }
    
    private void appendOneJob(ScheduleJob job) throws Exception {
        String runTime = job.getRunTime();
        
        if (runTime == null) {
            return;
        }
        
        JobKey jobKey = job.getJobKey();
        JobDetail jobDetail = JobBuilder.newJob(ScheduleJobAgent.class).withIdentity(jobKey).build();
        JobDataMap dataMap = jobDetail.getJobDataMap();
        dataMap.put(job.getClass().getSimpleName(), job);
        
        TriggerKey triggerKey = job.getTriggerKey();
        TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
        triggerBuilder.withIdentity(triggerKey);
        triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(runTime));
        CronTrigger trigger = (CronTrigger)triggerBuilder.build();
 
        if (!scheduler.checkExists(jobKey)) {
            scheduler.scheduleJob(jobDetail, trigger);
        }
    }
 
    public void executeJob(ActionContext context) throws JobExecutionException {
        DataReader dataReader = context.getDataReader();
        String scheduleId = dataReader.getString("scheduleId");
        String jobName = dataReader.getString("jobName");
        
        if (Util.isEmpty(scheduleId)) {
            logger.info("data not exists scheduleId, please check params");
        }
        
        ScheduleBatch scheduleBatch = scheduleBucket.get(scheduleId);
        
        if (Util.isEmpty(jobName)) {
            logger.info("data not exists jobName, will execute all job which scheduleId is " + scheduleId);
            
            for (ScheduleJob scheduleJob : scheduleBatch) {
                scheduleJob.execute();
            }
            
            return ;
        }
        
        for (ScheduleJob scheduleJob : scheduleBatch) {
            if (jobName.equalsIgnoreCase(scheduleJob.getId()) ) {
                scheduleJob.execute();
            }
        }
    }
 
     public void pauseOneJob(ActionContext context) throws SchedulerException {
        DataReader dataReader = context.getDataReader();
        String scheduleId = dataReader.getString("scheduleId");
        String jobName = dataReader.getString("jobName");
        
        if (Util.isEmpty(scheduleId)) {
            logger.info("data not exists scheduleId, please check params");
        }
        
        ScheduleBatch scheduleBatch = scheduleBucket.get(scheduleId);
        
        if (Util.isEmpty(jobName)) {
            logger.info("data not exists jobName, will pause all job which scheduleId is " + scheduleId);
            
            for (ScheduleJob scheduleJob : scheduleBatch) {
                JobKey jobKey = scheduleJob.getJobKey();
                scheduler.pauseJob(jobKey);
            }
            
            return ;
        }
        
        for (ScheduleJob scheduleJob : scheduleBatch) {
            if (jobName.equalsIgnoreCase(scheduleJob.getId()) ) {
                JobKey jobKey = scheduleJob.getJobKey();
                scheduler.pauseJob(jobKey);
                logger.info("pause scheduleId:{} ,jobName:{} ", scheduleId, jobName);
            }
            
        }
    }
 
}