Example usage for org.springframework.batch.core.scope.context StepSynchronizationManager release

List of usage examples for org.springframework.batch.core.scope.context StepSynchronizationManager release

Introduction

In this page you can find the example usage for org.springframework.batch.core.scope.context StepSynchronizationManager release.

Prototype

public static void release() 

Source Link

Document

A convenient "deep" close operation.

Usage

From source file:org.springframework.batch.core.jsr.launch.JsrJobOperator.java

/**
 * Stops the running job execution if it is currently running.
 *
 * @param executionId the database id for the {@link JobExecution} to be stopped.
 * @throws NoSuchJobExecutionException//w  w w .j  ava2s  . c  o  m
 * @throws JobExecutionNotRunningException
 */
@Override
public void stop(long executionId)
        throws NoSuchJobExecutionException, JobExecutionNotRunningException, JobSecurityException {
    org.springframework.batch.core.JobExecution jobExecution = jobExplorer.getJobExecution(executionId);
    // Indicate the execution should be stopped by setting it's status to
    // 'STOPPING'. It is assumed that
    // the step implementation will check this status at chunk boundaries.
    BatchStatus status = jobExecution.getStatus();
    if (!(status == BatchStatus.STARTED || status == BatchStatus.STARTING)) {
        throw new JobExecutionNotRunningException(
                "JobExecution must be running so that it can be stopped: " + jobExecution);
    }
    jobExecution.setStatus(BatchStatus.STOPPING);
    jobRepository.update(jobExecution);

    try {
        Job job = jobRegistry.getJob(jobExecution.getId());
        if (job instanceof StepLocator) {//can only process as StepLocator is the only way to get the step object
            //get the current stepExecution
            for (org.springframework.batch.core.StepExecution stepExecution : jobExecution
                    .getStepExecutions()) {
                if (stepExecution.getStatus().isRunning()) {
                    try {
                        //have the step execution that's running -> need to 'stop' it
                        Step step = ((StepLocator) job).getStep(stepExecution.getStepName());
                        if (step instanceof TaskletStep) {
                            Tasklet tasklet = ((TaskletStep) step).getTasklet();
                            if (tasklet instanceof StoppableTasklet) {
                                StepSynchronizationManager.register(stepExecution);
                                ((StoppableTasklet) tasklet).stop();
                                StepSynchronizationManager.release();
                            }
                        }
                    } catch (NoSuchStepException e) {
                        logger.warn("Step not found", e);
                    }
                }
            }
        }
    } catch (NoSuchJobException e) {
        logger.warn("Cannot find Job object", e);
    }
}

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

@Override
@Transactional/* w w w.j  a  v  a 2 s .  com*/
public boolean stop(long executionId) throws NoSuchJobExecutionException, JobExecutionNotRunningException {

    JobExecution jobExecution = findExecutionById(executionId);
    // Indicate the execution should be stopped by setting it's status to
    // 'STOPPING'. It is assumed that
    // the step implementation will check this status at chunk boundaries.
    BatchStatus status = jobExecution.getStatus();
    if (!(status == BatchStatus.STARTED || status == BatchStatus.STARTING)) {
        throw new JobExecutionNotRunningException(
                "JobExecution must be running so that it can be stopped: " + jobExecution);
    }
    jobExecution.setStatus(BatchStatus.STOPPING);
    jobRepository.update(jobExecution);

    try {
        Job job = jobRegistry.getJob(jobExecution.getJobInstance().getJobName());
        if (job instanceof StepLocator) {//can only process as StepLocator is the only way to get the step object
            //get the current stepExecution
            for (StepExecution stepExecution : jobExecution.getStepExecutions()) {
                if (stepExecution.getStatus().isRunning()) {
                    try {
                        //have the step execution that's running -> need to 'stop' it
                        Step step = ((StepLocator) job).getStep(stepExecution.getStepName());
                        if (step instanceof TaskletStep) {
                            Tasklet tasklet = ((TaskletStep) step).getTasklet();
                            if (tasklet instanceof StoppableTasklet) {
                                StepSynchronizationManager.register(stepExecution);
                                ((StoppableTasklet) tasklet).stop();
                                StepSynchronizationManager.release();
                            }
                        }
                    } catch (NoSuchStepException e) {
                        logger.warn("Step not found", e);
                    }
                }
            }
        }
    } catch (NoSuchJobException e) {
        logger.warn("Cannot find Job object", e);
    }

    return true;
}

From source file:org.springframework.batch.core.scope.AsyncStepScopeIntegrationTests.java

@Before
public void countBeans() {
    StepSynchronizationManager.release();
    beanCount = beanFactory.getBeanDefinitionCount();
}

From source file:org.springframework.batch.core.step.AbstractStep.java

/**
 * Releases the most recent {@link StepExecution}
 */
protected void doExecutionRelease() {
    StepSynchronizationManager.release();
}