Waits for batch job to finish. - Java javax.batch.operations

Java examples for javax.batch.operations:JobOperator

Description

Waits for batch job to finish.

Demo Code


import java.util.EnumSet;
import java.util.Set;
import javax.batch.operations.JobOperator;
import javax.batch.runtime.BatchStatus;
import javax.batch.runtime.JobExecution;

public class Main{
    public static final Set<BatchStatus> INCOMPLETE_STATUSES = EnumSet.of(
            BatchStatus.STARTED, BatchStatus.STARTING);
    /**//from w w w  .  j  a  v  a2s.  c  om
     * Waits for job to finish. JSR 352 does not provide a mechanism to 
     * launch jobs in the synchronous mode. As a result, the polling needs
     * to occur
     * 
     * @param jobOperator
     * @param executionId
     * @param pollingWait
     * @param maxTries
     * @return
     */
    public static JobExecution waitForJobToEnd(JobOperator jobOperator,
            final long executionId, final long pollingWait,
            final int maxTries) {

        int countTries = 0;
        JobExecution jobExecution = null;
        do {
            try {
                countTries++;
                Thread.sleep(pollingWait);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            jobExecution = jobOperator.getJobExecution(executionId);
        } while (INCOMPLETE_STATUSES
                .contains(jobExecution.getBatchStatus())
                || (countTries < maxTries));

        return jobExecution;
    }
}

Related Tutorials