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