dongyukun
9 天以前 5f653cd83425db29ccf514e9fd966a90c7eeab32
提交 | 用户 | 时间
e7c126 1 //package com.xxl.job.admin.core.schedule;
H 2 //
3 //import com.xxl.job.admin.core.conf.XxlJobAdminConfig;
4 //import com.xxl.job.admin.core.jobbean.RemoteHttpJobBean;
5 //import com.xxl.job.admin.core.model.XxlJobInfo;
6 //import com.xxl.job.admin.core.thread.JobFailMonitorHelper;
7 //import com.xxl.job.admin.core.thread.JobRegistryMonitorHelper;
8 //import com.xxl.job.admin.core.thread.JobTriggerPoolHelper;
9 //import com.xxl.job.admin.core.util.I18nUtil;
10 //import com.xxl.job.core.biz.AdminBiz;
11 //import com.xxl.job.core.biz.ExecutorBiz;
12 //import com.xxl.job.core.enums.ExecutorBlockStrategyEnum;
13 //import com.xxl.rpc.remoting.invoker.XxlRpcInvokerFactory;
14 //import com.xxl.rpc.remoting.invoker.call.CallType;
15 //import com.xxl.rpc.remoting.invoker.reference.XxlRpcReferenceBean;
16 //import com.xxl.rpc.remoting.invoker.route.LoadBalance;
17 //import com.xxl.rpc.remoting.net.NetEnum;
18 //import com.xxl.rpc.remoting.net.impl.servlet.server.ServletServerHandler;
19 //import com.xxl.rpc.remoting.provider.XxlRpcProviderFactory;
20 //import com.xxl.rpc.serialize.Serializer;
21 //import org.quartz.*;
22 //import org.quartz.Trigger.TriggerState;
23 //import org.quartz.impl.triggers.CronTriggerImpl;
24 //import org.slf4j.Logger;
25 //import org.slf4j.LoggerFactory;
26 //import org.springframework.util.Assert;
27 //
28 //import javax.servlet.ServletException;
29 //import javax.servlet.http.HttpServletRequest;
30 //import javax.servlet.http.HttpServletResponse;
31 //import java.io.IOException;
32 //import java.util.Date;
33 //import java.util.concurrent.ConcurrentHashMap;
34 //
35 ///**
36 // * base quartz scheduler util
37 // * @author xuxueli 2015-12-19 16:13:53
38 // */
39 //public final class XxlJobDynamicScheduler {
40 //    private static final Logger logger = LoggerFactory.getLogger(XxlJobDynamicScheduler_old.class);
41 //
42 //    // ---------------------- param ----------------------
43 //
44 //    // scheduler
45 //    private static Scheduler scheduler;
46 //    public void setScheduler(Scheduler scheduler) {
47 //        XxlJobDynamicScheduler_old.scheduler = scheduler;
48 //    }
49 //
50 //
51 //    // ---------------------- init + destroy ----------------------
52 //    public void start() throws Exception {
53 //        // valid
54 //        Assert.notNull(scheduler, "quartz scheduler is null");
55 //
56 //        // init i18n
57 //        initI18n();
58 //
59 //        // admin registry monitor run
60 //        JobRegistryMonitorHelper.getInstance().start();
61 //
62 //        // admin monitor run
63 //        JobFailMonitorHelper.getInstance().start();
64 //
65 //        // admin-server
66 //        initRpcProvider();
67 //
68 //        logger.info(">>>>>>>>> init xxl-job admin success.");
69 //    }
70 //
71 //
72 //    public void destroy() throws Exception {
73 //        // admin trigger pool stop
74 //        JobTriggerPoolHelper.toStop();
75 //
76 //        // admin registry stop
77 //        JobRegistryMonitorHelper.getInstance().toStop();
78 //
79 //        // admin monitor stop
80 //        JobFailMonitorHelper.getInstance().toStop();
81 //
82 //        // admin-server
83 //        stopRpcProvider();
84 //    }
85 //
86 //
87 //    // ---------------------- I18n ----------------------
88 //
89 //    private void initI18n(){
90 //        for (ExecutorBlockStrategyEnum item:ExecutorBlockStrategyEnum.values()) {
91 //            item.setTitle(I18nUtil.getString("jobconf_block_".concat(item.name())));
92 //        }
93 //    }
94 //
95 //
96 //    // ---------------------- admin rpc provider (no server version) ----------------------
97 //    private static ServletServerHandler servletServerHandler;
98 //    private void initRpcProvider(){
99 //        // init
100 //        XxlRpcProviderFactory xxlRpcProviderFactory = new XxlRpcProviderFactory();
101 //        xxlRpcProviderFactory.initConfig(
102 //                NetEnum.NETTY_HTTP,
103 //                Serializer.SerializeEnum.HESSIAN.getSerializer(),
104 //                null,
105 //                0,
106 //                XxlJobAdminConfig.getAdminConfig().getAccessToken(),
107 //                null,
108 //                null);
109 //
110 //        // add services
111 //        xxlRpcProviderFactory.addService(AdminBiz.class.getName(), null, XxlJobAdminConfig.getAdminConfig().getAdminBiz());
112 //
113 //        // servlet handler
114 //        servletServerHandler = new ServletServerHandler(xxlRpcProviderFactory);
115 //    }
116 //    private void stopRpcProvider() throws Exception {
117 //        XxlRpcInvokerFactory.getInstance().stop();
118 //    }
119 //    public static void invokeAdminService(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
120 //        servletServerHandler.handle(null, request, response);
121 //    }
122 //
123 //
124 //    // ---------------------- executor-client ----------------------
125 //    private static ConcurrentHashMap<String, ExecutorBiz> executorBizRepository = new ConcurrentHashMap<String, ExecutorBiz>();
126 //    public static ExecutorBiz getExecutorBiz(String address) throws Exception {
127 //        // valid
128 //        if (address==null || address.trim().length()==0) {
129 //            return null;
130 //        }
131 //
132 //        // load-cache
133 //        address = address.trim();
134 //        ExecutorBiz executorBiz = executorBizRepository.get(address);
135 //        if (executorBiz != null) {
136 //            return executorBiz;
137 //        }
138 //
139 //        // set-cache
140 //        executorBiz = (ExecutorBiz) new XxlRpcReferenceBean(
141 //                NetEnum.NETTY_HTTP,
142 //                Serializer.SerializeEnum.HESSIAN.getSerializer(),
143 //                CallType.SYNC,
144 //                LoadBalance.ROUND,
145 //                ExecutorBiz.class,
146 //                null,
147 //                5000,
148 //                address,
149 //                XxlJobAdminConfig.getAdminConfig().getAccessToken(),
150 //                null,
151 //                null).getObject();
152 //
153 //        executorBizRepository.put(address, executorBiz);
154 //        return executorBiz;
155 //    }
156 //
157 //
158 //    // ---------------------- schedule util ----------------------
159 //
160 //    /**
161 //     * fill job info
162 //     *
163 //     * @param jobInfo
164 //     */
165 //    public static void fillJobInfo(XxlJobInfo jobInfo) {
166 //
167 //        String name = String.valueOf(jobInfo.getId());
168 //
169 //        // trigger key
170 //        TriggerKey triggerKey = TriggerKey.triggerKey(name);
171 //        try {
172 //
173 //            // trigger cron
174 //            Trigger trigger = scheduler.getTrigger(triggerKey);
175 //            if (trigger!=null && trigger instanceof CronTriggerImpl) {
176 //                String cronExpression = ((CronTriggerImpl) trigger).getCronExpression();
177 //                jobInfo.setJobCron(cronExpression);
178 //            }
179 //
180 //            // trigger state
181 //            TriggerState triggerState = scheduler.getTriggerState(triggerKey);
182 //            if (triggerState!=null) {
183 //                jobInfo.setJobStatus(triggerState.name());
184 //            }
185 //
186 //            //JobKey jobKey = new JobKey(jobInfo.getJobName(), String.valueOf(jobInfo.getJobGroup()));
187 //            //JobDetail jobDetail = scheduler.getJobDetail(jobKey);
188 //            //String jobClass = jobDetail.getJobClass().getName();
189 //
190 //        } catch (SchedulerException e) {
191 //            logger.error(e.getMessage(), e);
192 //        }
193 //    }
194 //
195 //
196 //    /**
197 //     * add trigger + job
198 //     *
199 //     * @param jobName
200 //     * @param cronExpression
201 //     * @return
202 //     * @throws SchedulerException
203 //     */
204 //    public static boolean addJob(String jobName, String cronExpression) throws SchedulerException {
205 //        // 1、job key
206 //        TriggerKey triggerKey = TriggerKey.triggerKey(jobName);
207 //        JobKey jobKey = new JobKey(jobName);
208 //
209 //        // 2、valid
210 //        if (scheduler.checkExists(triggerKey)) {
211 //            return true;    // PASS
212 //        }
213 //
214 //        // 3、corn trigger
215 //        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression).withMisfireHandlingInstructionDoNothing();   // withMisfireHandlingInstructionDoNothing 忽略掉调度终止过程中忽略的调度
216 //        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(triggerKey).withSchedule(cronScheduleBuilder).build();
217 //
218 //        // 4、job detail
219 //        Class<? extends Job> jobClass_ = RemoteHttpJobBean.class;   // Class.forName(jobInfo.getJobClass());
220 //        JobDetail jobDetail = JobBuilder.newJob(jobClass_).withIdentity(jobKey).build();
221 //
222 //        /*if (jobInfo.getJobData()!=null) {
223 //            JobDataMap jobDataMap = jobDetail.getJobDataMap();
224 //            jobDataMap.putAll(JacksonUtil.readValue(jobInfo.getJobData(), Map.class));
225 //            // JobExecutionContext context.getMergedJobDataMap().get("mailGuid");
226 //        }*/
227 //
228 //        // 5、schedule job
229 //        Date date = scheduler.scheduleJob(jobDetail, cronTrigger);
230 //
231 //        logger.info(">>>>>>>>>>> addJob success(quartz), jobDetail:{}, cronTrigger:{}, date:{}", jobDetail, cronTrigger, date);
232 //        return true;
233 //    }
234 //
235 //
236 //    /**
237 //     * remove trigger + job
238 //     *
239 //     * @param jobName
240 //     * @return
241 //     * @throws SchedulerException
242 //     */
243 //    public static boolean removeJob(String jobName) throws SchedulerException {
244 //
245 //        JobKey jobKey = new JobKey(jobName);
246 //        scheduler.deleteJob(jobKey);
247 //
248 //        /*TriggerKey triggerKey = TriggerKey.triggerKey(jobName);
249 //        if (scheduler.checkExists(triggerKey)) {
250 //            scheduler.unscheduleJob(triggerKey);    // trigger + job
251 //        }*/
252 //
253 //        logger.info(">>>>>>>>>>> removeJob success(quartz), jobKey:{}", jobKey);
254 //        return true;
255 //    }
256 //
257 //
258 //    /**
259 //     * updateJobCron
260 //     *
261 //     * @param jobName
262 //     * @param cronExpression
263 //     * @return
264 //     * @throws SchedulerException
265 //     */
266 //    public static boolean updateJobCron(String jobName, String cronExpression) throws SchedulerException {
267 //
268 //        // 1、job key
269 //        TriggerKey triggerKey = TriggerKey.triggerKey(jobName);
270 //
271 //        // 2、valid
272 //        if (!scheduler.checkExists(triggerKey)) {
273 //            return true;    // PASS
274 //        }
275 //
276 //        CronTrigger oldTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
277 //
278 //        // 3、avoid repeat cron
279 //        String oldCron = oldTrigger.getCronExpression();
280 //        if (oldCron.equals(cronExpression)){
281 //            return true;    // PASS
282 //        }
283 //
284 //        // 4、new cron trigger
285 //        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression).withMisfireHandlingInstructionDoNothing();
286 //        oldTrigger = oldTrigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(cronScheduleBuilder).build();
287 //
288 //        // 5、rescheduleJob
289 //        scheduler.rescheduleJob(triggerKey, oldTrigger);
290 //
291 //        /*
292 //        JobKey jobKey = new JobKey(jobName);
293 //
294 //        // old job detail
295 //        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
296 //
297 //        // new trigger
298 //        HashSet<Trigger> triggerSet = new HashSet<Trigger>();
299 //        triggerSet.add(cronTrigger);
300 //        // cover trigger of job detail
301 //        scheduler.scheduleJob(jobDetail, triggerSet, true);*/
302 //
303 //        logger.info(">>>>>>>>>>> resumeJob success, JobName:{}", jobName);
304 //        return true;
305 //    }
306 //
307 //
308 //    /**
309 //     * pause
310 //     *
311 //     * @param jobName
312 //     * @return
313 //     * @throws SchedulerException
314 //     */
315 //    /*public static boolean pauseJob(String jobName) throws SchedulerException {
316 //
317 //        TriggerKey triggerKey = TriggerKey.triggerKey(jobName);
318 //
319 //        boolean result = false;
320 //        if (scheduler.checkExists(triggerKey)) {
321 //            scheduler.pauseTrigger(triggerKey);
322 //            result =  true;
323 //        }
324 //
325 //        logger.info(">>>>>>>>>>> pauseJob {}, triggerKey:{}", (result?"success":"fail"),triggerKey);
326 //        return result;
327 //    }*/
328 //
329 //
330 //    /**
331 //     * resume
332 //     *
333 //     * @param jobName
334 //     * @return
335 //     * @throws SchedulerException
336 //     */
337 //    /*public static boolean resumeJob(String jobName) throws SchedulerException {
338 //
339 //        TriggerKey triggerKey = TriggerKey.triggerKey(jobName);
340 //
341 //        boolean result = false;
342 //        if (scheduler.checkExists(triggerKey)) {
343 //            scheduler.resumeTrigger(triggerKey);
344 //            result = true;
345 //        }
346 //
347 //        logger.info(">>>>>>>>>>> resumeJob {}, triggerKey:{}", (result?"success":"fail"), triggerKey);
348 //        return result;
349 //    }*/
350 //
351 //
352 //    /**
353 //     * run
354 //     *
355 //     * @param jobName
356 //     * @return
357 //     * @throws SchedulerException
358 //     */
359 //    /*public static boolean triggerJob(String jobName) throws SchedulerException {
360 //        // TriggerKey : name + group
361 //        JobKey jobKey = new JobKey(jobName);
362 //        TriggerKey triggerKey = TriggerKey.triggerKey(jobName);
363 //
364 //        boolean result = false;
365 //        if (scheduler.checkExists(triggerKey)) {
366 //            scheduler.triggerJob(jobKey);
367 //            result = true;
368 //            logger.info(">>>>>>>>>>> runJob success, jobKey:{}", jobKey);
369 //        } else {
370 //            logger.info(">>>>>>>>>>> runJob fail, jobKey:{}", jobKey);
371 //        }
372 //        return result;
373 //    }*/
374 //
375 //
376 //    /**
377 //     * finaAllJobList
378 //     *
379 //     * @return
380 //     *//*
381 //    @Deprecated
382 //    public static List<Map<String, Object>> finaAllJobList(){
383 //        List<Map<String, Object>> jobList = new ArrayList<Map<String,Object>>();
384 //
385 //        try {
386 //            if (scheduler.getJobGroupNames()==null || scheduler.getJobGroupNames().size()==0) {
387 //                return null;
388 //            }
389 //            String groupName = scheduler.getJobGroupNames().get(0);
390 //            Set<JobKey> jobKeys = scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName));
391 //            if (jobKeys!=null && jobKeys.size()>0) {
392 //                for (JobKey jobKey : jobKeys) {
393 //                    TriggerKey triggerKey = TriggerKey.triggerKey(jobKey.getName(), Scheduler.DEFAULT_GROUP);
394 //                    Trigger trigger = scheduler.getTrigger(triggerKey);
395 //                    JobDetail jobDetail = scheduler.getJobDetail(jobKey);
396 //                    TriggerState triggerState = scheduler.getTriggerState(triggerKey);
397 //                    Map<String, Object> jobMap = new HashMap<String, Object>();
398 //                    jobMap.put("TriggerKey", triggerKey);
399 //                    jobMap.put("Trigger", trigger);
400 //                    jobMap.put("JobDetail", jobDetail);
401 //                    jobMap.put("TriggerState", triggerState);
402 //                    jobList.add(jobMap);
403 //                }
404 //            }
405 //
406 //        } catch (SchedulerException e) {
407 //            logger.error(e.getMessage(), e);
408 //            return null;
409 //        }
410 //        return jobList;
411 //    }*/
412 //
413 //}