Example usage for org.springframework.batch.core JobExecution isRunning

List of usage examples for org.springframework.batch.core JobExecution isRunning

Introduction

In this page you can find the example usage for org.springframework.batch.core JobExecution isRunning.

Prototype

public boolean isRunning() 

Source Link

Document

Test if this JobExecution indicates that it is running.

Usage

From source file:admin.service.SimpleJobService.java

/**
 * Stop all the active jobs and wait for them (up to a time out) to finish
 * processing.//from   ww  w .j av a 2 s.  c  om
 */
@Override
public void destroy() throws Exception {

    Exception firstException = null;

    for (JobExecution jobExecution : activeExecutions) {
        try {
            if (jobExecution.isRunning()) {
                stop(jobExecution.getId());
            }
        } catch (JobExecutionNotRunningException e) {
            logger.info("JobExecution is not running so it cannot be stopped");
        } catch (Exception e) {
            logger.error("Unexpected exception stopping JobExecution", e);
            if (firstException == null) {
                firstException = e;
            }
        }
    }

    int count = 0;
    int maxCount = (shutdownTimeout + 1000) / 1000;
    while (!activeExecutions.isEmpty() && ++count < maxCount) {
        logger.error("Waiting for " + activeExecutions.size() + " active executions to complete");
        removeInactiveExecutions();
        Thread.sleep(1000L);
    }

    if (firstException != null) {
        throw firstException;
    }

}

From source file:org.trpr.platform.batch.impl.spring.admin.SimpleJobService.java

/**
 * Interface method implementation/*from   w w  w .  jav a2 s . c  om*/
 * @see org.springframework.batch.admin.service.JobService#stop(java.lang.Long)
 */
public JobExecution stop(Long jobExecutionId)
        throws NoSuchJobExecutionException, JobExecutionNotRunningException {
    JobExecution jobExecution = getJobExecution(jobExecutionId);
    if (!jobExecution.isRunning()) {
        throw new JobExecutionNotRunningException(
                "JobExecution is not running and therefore cannot be stopped");
    }
    LOGGER.info("Stopping job execution: " + jobExecution);
    jobExecution.stop();
    jobRepository.update(jobExecution);
    return jobExecution;
}

From source file:org.trpr.platform.batch.impl.spring.admin.SimpleJobService.java

/**
 * Interface method implementation/*ww w  . j a va  2  s . c  o  m*/
 * @see org.springframework.batch.admin.service.JobService#launch(java.lang.String, org.springframework.batch.core.JobParameters)
 */
public JobExecution launch(String jobName, JobParameters jobParameters)
        throws NoSuchJobException, JobExecutionAlreadyRunningException, JobRestartException,
        JobInstanceAlreadyCompleteException, JobParametersInvalidException {

    Job job = this.jobRegistry.getJob(jobName);
    JobExecution lastJobExecution = this.jobRepository.getLastJobExecution(jobName, jobParameters);
    boolean restart = false;
    if (lastJobExecution != null) {
        BatchStatus status = lastJobExecution.getStatus();
        if (status.isUnsuccessful() && status != BatchStatus.ABANDONED) {
            restart = true;
        }
    }
    if (job.getJobParametersIncrementer() != null && !restart) {
        jobParameters = job.getJobParametersIncrementer().getNext(jobParameters);
    }
    JobExecution jobExecution = this.jobLauncher.run(job, jobParameters);
    if (jobExecution.isRunning()) {
        this.activeExecutions.add(jobExecution);
    }
    return jobExecution;
}

From source file:org.trpr.platform.batch.impl.spring.admin.SimpleJobService.java

/**
 * Interface method implementation/*from  ww w. j a  v a  2 s.  c  o m*/
 * @see org.springframework.batch.admin.service.JobService#restart(java.lang.Long)
 */
public JobExecution restart(Long jobExecutionId)
        throws NoSuchJobExecutionException, JobExecutionAlreadyRunningException, JobRestartException,
        JobInstanceAlreadyCompleteException, NoSuchJobException, JobParametersInvalidException {
    JobExecution target = getJobExecution(jobExecutionId);
    JobInstance lastInstance = target.getJobInstance();
    Job job = this.jobRegistry.getJob(lastInstance.getJobName());
    JobExecution jobExecution = this.jobLauncher.run(job, lastInstance.getJobParameters());
    if (jobExecution.isRunning()) {
        this.activeExecutions.add(jobExecution);
    }
    return jobExecution;
}

From source file:org.trpr.platform.batch.impl.spring.admin.SimpleJobService.java

/**
 * Interface method implementation.Stops all the active jobs and wait for them (up to a time out) to finish
 * processing./*ww  w. jav a 2  s  . c  o  m*/
 * @see org.springframework.beans.factory.DisposableBean#destroy()
 */
public void destroy() throws Exception {
    Exception firstException = null;
    for (JobExecution jobExecution : activeExecutions) {
        try {
            if (jobExecution.isRunning()) {
                stop(jobExecution.getId());
            }
        } catch (JobExecutionNotRunningException e) {
            LOGGER.info("JobExecution is not running so it cannot be stopped");
        } catch (Exception e) {
            LOGGER.error("Unexpected exception stopping JobExecution", e);
            if (firstException == null) {
                firstException = e;
            }
        }
    }
    int count = 0;
    int maxCount = (shutdownTimeout + 1000) / 1000;
    while (!activeExecutions.isEmpty() && ++count < maxCount) {
        LOGGER.error("Waiting for " + activeExecutions.size() + " active executions to complete");
        removeInactiveExecutions();
        Thread.sleep(1000L);
    }
    if (firstException != null) {
        throw firstException;
    }
}

From source file:org.trpr.platform.batch.impl.spring.admin.SimpleJobService.java

/**
 * Interface method implementation// ww w .jav a  2 s .c o m
 * @see org.springframework.batch.admin.service.JobService#stopAll()
 */
public int stopAll() {
    List<JobExecution> allExecutions = new LinkedList<JobExecution>();
    for (String jobName : this.jobRegistry.getJobNames()) {
        for (JobInstance jobInstance : this.jobExplorer.getJobInstances(jobName, 0, Integer.MAX_VALUE)) {
            for (JobExecution jobExecution : this.jobExplorer.getJobExecutions(jobInstance)) {
                if (jobExecution.isRunning()) {
                    allExecutions.add(jobExecution);
                }
            }
        }
    }
    for (JobExecution jobExecution : allExecutions) {
        jobExecution.stop();
        this.jobRepository.update(jobExecution);
    }
    return allExecutions.size();
}

From source file:admin.service.SimpleJobService.java

/**
 * Check all the active executions and see if they are still actually
 * running. Remove the ones that have completed.
 *//*from w  w  w.ja va  2 s  .co  m*/
@Scheduled(fixedDelay = 60000)
public void removeInactiveExecutions() {

    for (Iterator<JobExecution> iterator = activeExecutions.iterator(); iterator.hasNext();) {
        JobExecution jobExecution = iterator.next();
        try {
            jobExecution = getJobExecution(jobExecution.getId());
        } catch (NoSuchJobExecutionException e) {
            logger.error("Unexpected exception loading JobExecution", e);
        }
        if (!jobExecution.isRunning()) {
            iterator.remove();
        }
    }

}

From source file:org.trpr.platform.batch.impl.spring.admin.SimpleJobService.java

/**
 * Check all the active executions and see if they are still actually
 * running. Remove the ones that have completed.
 *///from www  . j  ava  2 s. c  o  m
@Scheduled(fixedDelay = 60000)
public void removeInactiveExecutions() {
    for (Iterator<JobExecution> iterator = activeExecutions.iterator(); iterator.hasNext();) {
        JobExecution jobExecution = iterator.next();
        try {
            jobExecution = getJobExecution(jobExecution.getId());
        } catch (NoSuchJobExecutionException e) {
            LOGGER.error("Unexpected exception loading JobExecution", e);
        }
        if (!jobExecution.isRunning()) {
            iterator.remove();
        }
    }
}

From source file:cn.cuizuoli.appranking.batch.tasklet.AppRankingJobTest.java

@Test
public void execute() {
    JobExecution jobExecution = null;
    try {/* w  w w . j a  v  a2s  . c  om*/
        JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
        Job job = jobRegistry.getJob("appRankingJob");
        JobParameters jobParameters = jobParametersBuilder
                .addString("date", DateTime.now().toString("yyyyMMddHHmmss")).toJobParameters();
        jobExecution = jobLauncher.run(job, jobParameters);
    } catch (NoSuchJobException e) {
        e.printStackTrace();
    } catch (JobExecutionAlreadyRunningException e) {
        e.printStackTrace();
    } catch (JobRestartException e) {
        e.printStackTrace();
    } catch (JobInstanceAlreadyCompleteException e) {
        e.printStackTrace();
    } catch (JobParametersInvalidException e) {
        e.printStackTrace();
    } finally {
        while (jobExecution.isRunning()) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        }
    }
}

From source file:org.springframework.batch.core.launch.support.CommandLineJobRunner.java

private List<JobExecution> getRunningJobExecutions(String jobIdentifier) {
    List<JobExecution> jobExecutions = getJobExecutionsWithStatusGreaterThan(jobIdentifier,
            BatchStatus.COMPLETED);//  w  ww  . ja  v  a  2s . c o  m
    if (jobExecutions.isEmpty()) {
        return null;
    }
    List<JobExecution> result = new ArrayList<JobExecution>();
    for (JobExecution jobExecution : jobExecutions) {
        if (jobExecution.isRunning()) {
            result.add(jobExecution);
        }
    }
    return result.isEmpty() ? null : result;
}