I have a method that i have written inside constructor.
When i schedule a Spring batch Job That method is not getting called in specific time interval.
Only it get called at the time of Spring Batch Application startup.
Can anyone suggest any logic how i can schedule a Spring Batch
application that will restart my application in a specific time interval.
private int nextULPIndex;
private List<Data> finalULPData;
public JsonItemReaderFromULProspector() {
ULProspectorAPICall.runShellScript();
logger.info("API call to ULProspector successfully executed inside JsonItemReaderFromULProspector");
logger.info("Inside Constructor of JsonItemReaderFromULProspector");
initialize();
}
private void initialize() {
List<Data> ulpData = ReadJSONFromULP.getJsonObject();
logger.info("initialize() Started");
finalULPData = Collections.unmodifiableList(ulpData);
nextULPIndex = 0;
}
finalULPData is called inside run() method of the ItemReader class
I can't call the initialize() inside run() because run() method is getting called repeatedly based on chunk size.
public class JsonItemReaderFromULProspector implements ItemReader<Data> {
@Override
public Data read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
Data nextULPData = null;
logger.info("read() method JsonItemReaderFromULProspector class :Start");
if (nextULPIndex < finalULPData.size()) {
nextULPData = finalULPData.get(nextULPIndex);
nextULPIndex++;
}
return nextULPData;
}
}
This class is called from BatchConfig class like below.
@StepScope
@Lazy
ItemReader<Data> reader() {
return new JsonItemReaderFromULProspector();
}
@Bean
@Lazy
public Step step1() throws Exception {
return stepBuilderFactory.get("step1").<Data, ULPData>chunk(10).reader(reader()).processor(ulProspectorDataProcessor()).writer(ulpDataWriter.jsonFileDatabaseItemWriter()).build();
}
@Bean
public Job job() throws Exception {
return this.jobBuilderFactory.get("job").incrementer(new RunIdIncrementer())
.start(callUlPApiStep())
.next(deleteRecordsFromTableStep())
.next(step1()).build();}