View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.time.batch;
17  
18  import java.util.List;
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.TKUtils;
24  import org.kuali.hr.time.util.TkConstants;
25  
26  /**
27   * Runs on each worker node, schedules jobs to run on the thread pool.
28   */
29  public class BatchJobEntryPoller extends Thread  {
30      private static final Logger LOG = Logger.getLogger(BatchJobEntryPoller.class);
31  
32      TkBatchManager manager;
33      int secondsToSleep;
34      int startupSleep;
35      String ipAddress;
36  
37      public BatchJobEntryPoller(TkBatchManager manager, int sleepSeconds, int startupSleep) {
38          this.manager = manager;
39          this.secondsToSleep = sleepSeconds;
40          this.startupSleep = startupSleep;
41          this.ipAddress = TKUtils.getIPNumber();
42      }
43  
44      @Override
45      public void run() {
46  
47          try {
48              Thread.sleep(1000 * startupSleep);
49          } catch (Exception e) {
50              LOG.error(e);
51          }
52  
53          while(true) {
54              LOG.debug("Looking for BatchJobEntries to run on '" + this.ipAddress + "'");
55              try {
56                  //Find any jobs for this ip address that have a status of S
57                  List<BatchJobEntry> entries = TkServiceLocator.getBatchJobEntryService().getBatchJobEntries(ipAddress, TkConstants.BATCH_JOB_ENTRY_STATUS.SCHEDULED);
58  
59                  //Add jobs to the manager
60                  for (BatchJobEntry entry : entries) {
61                  	BatchJobEntryRunnable batchJobEntryRunnable = getRunnable(entry);
62                  	if (batchJobEntryRunnable != null) {
63                  		manager.pool.submit(batchJobEntryRunnable);
64                  	}
65                  }
66                  Thread.sleep(1000 * secondsToSleep);
67              } catch (Exception e) {
68                  LOG.error(e);
69              }
70          }
71      }
72  
73      private BatchJobEntryRunnable getRunnable(BatchJobEntry entry) {
74          BatchJobEntryRunnable bjer = null;
75  
76          if (StringUtils.equals(entry.getBatchJobName(), TkConstants.BATCH_JOB_NAMES.APPROVE)) {
77              LOG.debug("Creating EmployeeApprovalBatchJobRunnable.");
78              bjer = new EmployeeApprovalBatchJobRunnable(entry);
79          } else if (StringUtils.equals(entry.getBatchJobName(), TkConstants.BATCH_JOB_NAMES.PAY_PERIOD_END)) {
80              LOG.debug("Creating PayPeriodEndBatchJobRunnable.");
81              bjer = new PayPeriodEndBatchJobRunnable(entry);
82          } else if (StringUtils.equals(entry.getBatchJobName(), TkConstants.BATCH_JOB_NAMES.SUPERVISOR_APPROVAL)) {
83              LOG.debug("Creating SupervisorApprovalBatchJobRunnable.");
84              bjer = new SupervisorApprovalBatchJobRunnable(entry);
85          } else if (StringUtils.equals(entry.getBatchJobName(), TkConstants.BATCH_JOB_NAMES.INITIATE)) {
86              LOG.debug("Creating InitiateBatchJobRunnable.");
87              bjer = new InitiateBatchJobRunnable(entry);
88          } else if(StringUtils.equals(entry.getBatchJobName(), TkConstants.BATCH_JOB_NAMES.BATCH_APPROVE_MISSED_PUNCH)) {
89              LOG.debug("BatchApproveMissedPunchJobRunnable is not run on a regular basis, skipping...");
90          } else {
91              LOG.warn("Unknown BatchJobEntryRunnable found in BatchJobEntry table. Unable to create Runnable.");
92          }
93  
94          return bjer;
95      }
96  
97  }