1 package org.kuali.ole.deliver.service;
2
3 import org.apache.log4j.Logger;
4 import org.kuali.ole.deliver.bo.OleBatchJobBo;
5 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
6 import org.kuali.rice.krad.service.KRADServiceLocator;
7 import org.quartz.CronExpression;
8 import org.quartz.JobDetail;
9 import org.quartz.impl.StdScheduler;
10 import org.springframework.scheduling.quartz.CronTriggerBean;
11
12 import java.text.ParseException;
13 import java.util.Date;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17
18
19
20
21
22
23
24
25 public class OleBatchJobService {
26
27 private static final Logger LOG = Logger.getLogger(OleBatchJobService.class);
28
29 public OleBatchJobService() {
30 addAvailableJobsToScheduler();
31 }
32
33
34
35
36 public void addAvailableJobsToScheduler() {
37
38 Map<String, String> batchMap = new HashMap<String, String>();
39 batchMap.put("jobEnableStatus", "true");
40 List<OleBatchJobBo> oleDeliverBatchJobBoList = (List<OleBatchJobBo>) KRADServiceLocator.getBusinessObjectService().findMatching(OleBatchJobBo.class, batchMap);
41 StdScheduler scheduler = (StdScheduler) GlobalResourceLoader.getService("scheduler");
42
43 if (oleDeliverBatchJobBoList.size() > 0) {
44 for (OleBatchJobBo oleDeliverBatchJobBo : oleDeliverBatchJobBoList) {
45
46 try {
47 JobDetail jobDetail = (JobDetail) GlobalResourceLoader.getService(oleDeliverBatchJobBo.getJobTriggerName());
48 CronTriggerBean cronTriggerBean = new CronTriggerBean();
49 cronTriggerBean.setName(oleDeliverBatchJobBo.getJobTriggerName() + "Trigger");
50 cronTriggerBean.setCronExpression(oleDeliverBatchJobBo.getJobCronExpression());
51 cronTriggerBean.setJobName(jobDetail.getName());
52 cronTriggerBean.setJobGroup(jobDetail.getGroup());
53 cronTriggerBean.setJobDetail(jobDetail);
54 if(null != getNextValidTimeToRunJobFromCronExpression(oleDeliverBatchJobBo.getJobCronExpression())){
55 try {
56 scheduler.scheduleJob(jobDetail, cronTriggerBean);
57 if (LOG.isInfoEnabled()){
58 LOG.info(jobDetail.getName() + " job is scheduled");
59 }
60 } catch (Exception e) {
61 scheduler.rescheduleJob(oleDeliverBatchJobBo.getJobTriggerName(), jobDetail.getName(), cronTriggerBean);
62 }
63 }
64 } catch (Exception e) {
65 LOG.error(e.getMessage() + " : Unable to schedule the job", e);
66
67 }
68 }
69 }
70
71 }
72
73 public static Date getNextValidTimeToRunJobFromCronExpression(String cronExpression){
74 Date date = null;
75 CronExpression expression;
76 try {
77 expression = new CronExpression(cronExpression);
78 date = expression.getNextValidTimeAfter(new Date());
79 } catch (ParseException e) {
80 e.printStackTrace();
81 }
82 return date;
83 }
84
85 }