View Javadoc

1   /**
2    * Copyright 2004-2012 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                      manager.pool.submit(getRunnable(entry));
62                  }
63                  Thread.sleep(1000 * secondsToSleep);
64              } catch (Exception e) {
65                  LOG.error(e);
66              }
67          }
68      }
69  
70      private BatchJobEntryRunnable getRunnable(BatchJobEntry entry) {
71          BatchJobEntryRunnable bjer = null;
72  
73          if (StringUtils.equals(entry.getBatchJobName(), TkConstants.BATCH_JOB_NAMES.APPROVE)) {
74              LOG.debug("Creating EmployeeApprovalBatchJobRunnable.");
75              bjer = new EmployeeApprovalBatchJobRunnable(entry);
76          } else if (StringUtils.equals(entry.getBatchJobName(), TkConstants.BATCH_JOB_NAMES.PAY_PERIOD_END)) {
77              LOG.debug("Creating PayPeriodEndBatchJobRunnable.");
78              bjer = new PayPeriodEndBatchJobRunnable(entry);
79          } else if (StringUtils.equals(entry.getBatchJobName(), TkConstants.BATCH_JOB_NAMES.SUPERVISOR_APPROVAL)) {
80              LOG.debug("Creating SupervisorApprovalBatchJobRunnabble.");
81              bjer = new SupervisorApprovalBatchJobRunnable(entry);
82          } else if (StringUtils.equals(entry.getBatchJobName(), TkConstants.BATCH_JOB_NAMES.INITIATE)) {
83              LOG.debug("Creating InitiateBatchJobRunnable.");
84              bjer = new InitiateBatchJobRunnable(entry);
85          } else if(StringUtils.equals(entry.getBatchJobName(), TkConstants.BATCH_JOB_NAMES.BATCH_APPROVE_MISSED_PUNCH)) {
86              LOG.debug("Creating BatchApproveMissedPunchJobRunnable.");
87              bjer = new BatchApproveMissedPunchJobRunnable(entry);
88          } else {
89              LOG.warn("Unknown BatchJobEntryRunnable found in BatchJobEntry table. Unable to create Runnable.");
90          }
91  
92          return bjer;
93      }
94  
95  }