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.sql.Timestamp;
19
20 import org.apache.commons.lang.StringUtils;
21 import org.apache.commons.lang3.ArrayUtils;
22 import org.apache.log4j.Logger;
23 import org.kuali.hr.time.service.base.TkServiceLocator;
24 import org.kuali.hr.time.util.TkConstants;
25 import org.kuali.rice.core.api.config.property.ConfigContext;
26 import org.springframework.transaction.TransactionStatus;
27 import org.springframework.transaction.support.TransactionCallbackWithoutResult;
28
29 public class BatchJob {
30 private Logger LOG = Logger.getLogger(BatchJob.class);
31 int lastPlace = 0;
32
33 private Long tkBatchJobId;
34 private String batchJobName;
35 private String batchJobStatus;
36 private String hrPyCalendarEntryId;
37 private Long timeElapsed = 0L;
38 private Timestamp timestamp;
39 long startTime;
40 long endTime;
41
42 public BatchJob() {
43 this.setBatchJobStatus(TkConstants.BATCH_JOB_ENTRY_STATUS.SCHEDULED);
44 this.setTimestamp(new Timestamp(System.currentTimeMillis()));
45 }
46
47
48
49
50 void doBeforeRun() {
51 LOG.info("Starting batch job: " + this.getBatchJobName() + " for pay calendar entry: " + this.hrPyCalendarEntryId);
52 startTime = System.currentTimeMillis();
53 this.setBatchJobStatus(TkConstants.BATCH_JOB_ENTRY_STATUS.RUNNING);
54 TkServiceLocator.getBatchJobService().saveBatchJob(this);
55 }
56
57 public void runJob() {
58 TkServiceLocator.getTransactionTemplate().execute(new TransactionCallbackWithoutResult() {
59 @Override
60 protected void doInTransactionWithoutResult(TransactionStatus status) {
61 doBeforeRun();
62 doWork();
63 doAfterRun();
64 }
65 });
66 }
67
68 void doWork() {
69 throw new UnsupportedOperationException("You must override this method in a subclass.");
70 }
71
72
73
74
75 void doAfterRun() {
76 endTime = System.currentTimeMillis();
77 long runtime = endTime - startTime;
78 timeElapsed = (runtime > 0) ? runtime / 1000 : 0;
79 this.setBatchJobStatus(TkConstants.BATCH_JOB_ENTRY_STATUS.FINISHED);
80 TkServiceLocator.getBatchJobService().saveBatchJob(this);
81 LOG.info("Batch job '" + this.getBatchJobName() + "' ("+this.getHrPyCalendarEntryId()+") complete after " + timeElapsed + " seconds.");
82 }
83
84 public String getNextIpAddressInCluster() {
85 String nextIpAddressInCluster = "";
86
87 String clusterIps = ConfigContext.getCurrentContextConfig().getProperty("cluster.ips");
88 String[] ips = StringUtils.split(clusterIps, ",");
89 if (ArrayUtils.isNotEmpty(ips)) {
90 if (lastPlace >= ips.length) {
91 lastPlace = 0;
92 }
93 nextIpAddressInCluster = ips[lastPlace++];
94 }
95
96 return nextIpAddressInCluster;
97 }
98
99 protected void populateBatchJobEntry(Object o){
100 throw new UnsupportedOperationException("You must override this method in a subclass.");
101 }
102
103 public Long getTkBatchJobId() {
104 return tkBatchJobId;
105 }
106
107 public void setTkBatchJobId(Long tkBatchJobId) {
108 this.tkBatchJobId = tkBatchJobId;
109 }
110
111 public String getBatchJobName() {
112 return batchJobName;
113 }
114
115 public void setBatchJobName(String batchJobName) {
116 this.batchJobName = batchJobName;
117 }
118
119 public String getBatchJobStatus() {
120 return batchJobStatus;
121 }
122
123 public void setBatchJobStatus(String batchJobStatus) {
124 this.batchJobStatus = batchJobStatus;
125 }
126
127 public String getHrPyCalendarEntryId() {
128 return hrPyCalendarEntryId;
129 }
130
131 public void setHrPyCalendarEntryId(String hrPyCalendarEntryId) {
132 this.hrPyCalendarEntryId = hrPyCalendarEntryId;
133 }
134
135 public Long getTimeElapsed() {
136 return timeElapsed;
137 }
138
139 public void setTimeElapsed(Long timeElapsed) {
140 this.timeElapsed = timeElapsed;
141 }
142
143 public Timestamp getTimestamp() {
144 return timestamp;
145 }
146
147 public void setTimestamp(Timestamp timestamp) {
148 this.timestamp = timestamp;
149 }
150
151 BatchJobEntry createBatchJobEntry(String batchJobName, String ip, String principal, String documentId, String clockLogId) {
152 BatchJobEntry entry = new BatchJobEntry();
153
154 entry.setBatchJobEntryStatus(TkConstants.BATCH_JOB_ENTRY_STATUS.SCHEDULED);
155 entry.setBatchJobName(batchJobName);
156 entry.setIpAddress(ip);
157 entry.setHrPyCalendarEntryId(this.getHrPyCalendarEntryId());
158 entry.setPrincipalId(principal);
159 entry.setTkBatchJobId(this.getTkBatchJobId());
160 entry.setDocumentId(documentId);
161 entry.setClockLogId(clockLogId);
162
163 return entry;
164 }
165
166 }