-
Notifications
You must be signed in to change notification settings - Fork 0
/
OmsMDCBatchJobScheduler.java
109 lines (88 loc) · 4.2 KB
/
OmsMDCBatchJobScheduler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* ==========================================================================
* Filename: OmsMDCBatchJobScheduler.java
* =============================================================================
* <p>
* =============================================================================
* <p>
* NOTICE
* Confidential, unpublished property of United Parcel Service.
* Use and distribution limited solely to authorized personnel.
* <p>
* The use, disclosutoredre, reproduction, modification, transfer, or
* transmittal of this work for any purpose in any form or by
* any means without the written permission of United Parcel
* Service is strictly prohibited.
* <p>
* Copyright (c) 2016-2017, United Parcel Service of America, Inc.
* All Rights Reserved.
* <p>
* =============================================================================
* Author/Architect - OMS@ups.com
* Date Create – 11-03-2017
* =============================================================================
*/
package com.ups.ops.oms.batch.mdc.task;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.ups.ops.oms.batch.mdc.constants.OmsMDCBatchConstants;
/**
* The Class OmsMDCBatchScheduler.
*/
@Component
public class OmsMDCBatchJobScheduler {
private static Logger log = LoggerFactory.getLogger(OmsMDCBatchJobScheduler.class);
/** The job launcher. */
@Autowired
SimpleJobLauncher jobLauncher;
/** The MDC data processing job. */
@Autowired
Job mdcDataProcessingJob;
/**
* Schedule OMS MDC job.
*
* @throws JobExecutionAlreadyRunningException the job execution already running exception
* @throws JobRestartException the job restart exception
* @throws JobInstanceAlreadyCompleteException the job instance already complete exception
* @throws JobParametersInvalidException the job parameters invalid exception
* @throws InterruptedException
*/
@Scheduled(cron = "${oms.mdc.cron.frequency}", zone="America/New_York")
public void scheduleOMSMdcJob() throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException, InterruptedException {
log.info("OMS MDC SERVICE: Job Started at: {}", new Date());
resetMDCParameters();
JobExecution execution = runOMDCBatchProcess();
while(OmsMDCBatchConstants.RETRY_COUNT <= OmsMDCBatchConstants.MAX_RETRY
&& execution.getStatus().equals(BatchStatus.FAILED) ) {
log.info("MDC Batch Process Retry#" + OmsMDCBatchConstants.RETRY_COUNT);
OmsMDCBatchConstants.RETRY_COUNT=OmsMDCBatchConstants.RETRY_COUNT+1;
execution = runOMDCBatchProcess();
}
log.info("OMS MDC SERVICE: Job finished with status: {}", execution.getStatus());
}
private JobExecution runOMDCBatchProcess() throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException {
JobParameters param = new JobParametersBuilder().addString("JobID", String.valueOf(System.currentTimeMillis()))
.toJobParameters();
JobExecution execution = jobLauncher.run(mdcDataProcessingJob, param);
return execution;
}
private void resetMDCParameters() {
OmsMDCBatchConstants.MDC_RETRY_COMPLETE = Boolean.FALSE;
OmsMDCBatchConstants.RETRY_COUNT = 1;
}
}