1

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();}
Philip Wrage
  • 1,505
  • 1
  • 12
  • 23
Dipty S
  • 11
  • 6
  • can you post more code so that I can understand the issue – Saurabh Verma May 04 '20 at 11:33
  • Hi Saurabh,I have added few more code snippets for your understanding.1) Basically i am calling a shell script (Which is creating JSON file) and Step 1 is reading that JSON object and converting it to POJO object to insert into Database. – Dipty S May 04 '20 at 12:08

0 Answers0