1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.batch;
17
18 import java.util.ArrayList;
19 import java.util.Date;
20 import java.util.List;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.log4j.Logger;
24 import org.kuali.hr.time.calendar.CalendarEntries;
25 import org.kuali.hr.time.service.base.TkServiceLocator;
26 import org.kuali.hr.time.util.TKUtils;
27 import org.kuali.hr.time.util.TkConstants;
28
29 public class BatchJobManagerThread extends Thread {
30 private static final Logger LOG = Logger.getLogger(BatchJobManagerThread.class);
31
32 int startupSleep = 120;
33
34 int numOfDaysToPoll = 30;
35 int secondsToSleep = 120;
36
37 public BatchJobManagerThread(int secondsToSleep, int numberOfDaysToPoll, int startupSleep) {
38 this.numOfDaysToPoll = numberOfDaysToPoll;
39 this.secondsToSleep = secondsToSleep;
40 this.startupSleep = startupSleep;
41 }
42
43 @Override
44 public void run() {
45
46 try {
47 Thread.sleep(1000 * startupSleep);
48 } catch (Exception e) {
49 LOG.error(e);
50 }
51
52 while (true) {
53 Date asOfDate = TKUtils.getCurrentDate();
54 List<CalendarEntries> payCalendarEntries = TkServiceLocator.getCalendarEntriesService().getCurrentCalendarEntryNeedsScheduled(numOfDaysToPoll, asOfDate);
55
56 LOG.info("Scanning for batch jobs to run: ("+asOfDate.toString()+")");
57
58 List<BatchJob> jobsToRun = new ArrayList<BatchJob>();
59 for(CalendarEntries payCalendarEntry: payCalendarEntries){
60
61 List<BatchJob> batchJobs = TkServiceLocator.getBatchJobService().getBatchJobs(payCalendarEntry.getHrCalendarEntriesId());
62
63
64
65
66 if ((payCalendarEntry.getBatchInitiateDate() != null) && (!jobPresentInJobsList(batchJobs, TkConstants.BATCH_JOB_NAMES.INITIATE)) ) {
67 BatchJob job = new InitiateBatchJob(payCalendarEntry.getHrCalendarEntriesId());
68 TkServiceLocator.getBatchJobService().saveBatchJob(job);
69 batchJobs.add(job);
70 jobsToRun.add(job);
71 }
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 }
91
92 for (BatchJob job : jobsToRun) {
93 job.runJob();
94 }
95
96 try {
97 Thread.sleep(1000 * secondsToSleep);
98 } catch (Exception e) {
99 LOG.error(e);
100 }
101 }
102 }
103
104 private boolean jobPresentInJobsList(List<BatchJob> batchJobs, String batchJobName) {
105 for (BatchJob batchJob : batchJobs) {
106 if (StringUtils.equals(batchJob.getBatchJobName(), batchJobName)) {
107 return true;
108 }
109 }
110
111 return false;
112 }
113
114
115
116 }