user2018311
user2018311

Reputation: 105

Spring Batch pause and then continue

I'm writing a job that will read from an excel file, x number of rows and then I'd like it to pause for an hour before it continues with the next x number of rows. How do I do this?

I have a job.xml file which contains the following. The subscriptionDiscoverer fetches the file and pass it over to the processor. The subscriptionWriter should write another file when the processor is done.

<job id="subscriptionJob" xmlns="http://www.springframework.org/schema/batch" incrementer="jobParamsIncrementer">
    <validator ref="jobParamsValidator"/>
    <step id="readFile">
        <tasklet>
            <chunk reader="subscriptionDiscoverer" processor="subscriptionProcessor" writer="subscriptionWriter" commit-interval="1"  />
        </tasklet>
    </step>
</job>

Is there some kind of timer I could use or is it some kind of flow structure? It's a large file of about 160000 rows that should be processed.

I hope someone has a solution they would like to share. Thank you!

Upvotes: 1

Views: 6530

Answers (1)

Kenston Choi
Kenston Choi

Reputation: 2942

I'm thinking of two possible approaches for you to start with:

  1. Stop the job, and restart again (after an hour) at the last position. You can start by taking a look on how to change the BatchStatus to notify your intent to stop the job. See http://docs.spring.io/spring-batch/2.0.x/cases/pause.html or look at how Spring Batch Admin implements its way of communicating the PAUSE flag (http://docs.spring.io/spring-batch-admin/reference/reference.xhtml). You may need to implement some persistence to store the position (row number) for the job to know where to start processing again. You can use a scheduler as well to restart the job.

-or-

  1. Add a ChunkListener and implement the following in afterChunk(ChunkContext context): Check if x number of rows has been read so far, and if yes, implement your pause mechanism (e.g., a simple Thread.sleep or look for more consistent way of pausing the step). To check for the number of rows read, you may use StepExecution.getReadCount() from ChunkContext.getStepContext().StepExecution().

Do note that afterChunk is called outside the transaction as indicated in the javadoc:

Callback after the chunk is executed, outside the transaction.

Upvotes: 2

Related Questions