List of usage examples for org.springframework.batch.core.scope.context StepSynchronizationManager register
public static StepContext register(StepExecution stepExecution)
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//from ww w .j a v a 2 s. 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/*from w w w . j a v a2s. co m*/ 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
@Test public void testSimpleProperty() throws Exception { StepExecution stepExecution = new StepExecution("step", new JobExecution(0L), 123L); ExecutionContext executionContext = stepExecution.getExecutionContext(); executionContext.put("foo", "bar"); StepSynchronizationManager.register(stepExecution); assertEquals("bar", simple.getName()); }
From source file:org.springframework.batch.core.scope.AsyncStepScopeIntegrationTests.java
@Test public void testGetMultipleInMultipleThreads() throws Exception { List<FutureTask<String>> tasks = new ArrayList<FutureTask<String>>(); for (int i = 0; i < 12; i++) { final String value = "foo" + i; final Long id = 123L + i; FutureTask<String> task = new FutureTask<String>(new Callable<String>() { @Override/*from w ww .j a v a2 s . co m*/ public String call() throws Exception { StepExecution stepExecution = new StepExecution(value, new JobExecution(0L), id); ExecutionContext executionContext = stepExecution.getExecutionContext(); executionContext.put("foo", value); StepContext context = StepSynchronizationManager.register(stepExecution); logger.debug("Registered: " + context.getStepExecutionContext()); try { return simple.getName(); } finally { StepSynchronizationManager.close(); } } }); tasks.add(task); taskExecutor.execute(task); } int i = 0; for (FutureTask<String> task : tasks) { assertEquals("foo" + i, task.get()); i++; } }
From source file:org.springframework.batch.core.scope.AsyncStepScopeIntegrationTests.java
@Test public void testGetSameInMultipleThreads() throws Exception { List<FutureTask<String>> tasks = new ArrayList<FutureTask<String>>(); final StepExecution stepExecution = new StepExecution("foo", new JobExecution(0L), 123L); ExecutionContext executionContext = stepExecution.getExecutionContext(); executionContext.put("foo", "foo"); StepSynchronizationManager.register(stepExecution); assertEquals("foo", simple.getName()); for (int i = 0; i < 12; i++) { final String value = "foo" + i; FutureTask<String> task = new FutureTask<String>(new Callable<String>() { @Override// w ww . j ava 2 s.c om public String call() throws Exception { ExecutionContext executionContext = stepExecution.getExecutionContext(); executionContext.put("foo", value); StepContext context = StepSynchronizationManager.register(stepExecution); logger.debug("Registered: " + context.getStepExecutionContext()); try { return simple.getName(); } finally { StepSynchronizationManager.close(); } } }); tasks.add(task); taskExecutor.execute(task); } for (FutureTask<String> task : tasks) { assertEquals("foo", task.get()); } // Don't close the outer scope until all tasks are finished. This should // always be the case if using an AbstractStep StepSynchronizationManager.close(); }
From source file:org.springframework.batch.core.scope.context.StepContextRepeatCallback.java
/** * Manage the {@link StepContext} lifecycle. Business processing should be * delegated to {@link #doInChunkContext(RepeatContext, ChunkContext)}. This * is to ensure that the current thread has a reference to the context, even * if the callback is executed in a pooled thread. Handles the registration * and unregistration of the step context, so clients should not duplicate * those calls.//from ww w .j ava 2 s . c o m * * @see RepeatCallback#doInIteration(RepeatContext) */ @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { // The StepContext has to be the same for all chunks, // otherwise step-scoped beans will be re-initialised for each chunk. StepContext stepContext = StepSynchronizationManager.register(stepExecution); if (logger.isDebugEnabled()) { logger.debug("Preparing chunk execution for StepContext: " + ObjectUtils.identityToString(stepContext)); } ChunkContext chunkContext = attributeQueue.poll(); if (chunkContext == null) { chunkContext = new ChunkContext(stepContext); } try { if (logger.isDebugEnabled()) { logger.debug("Chunk execution starting: queue size=" + attributeQueue.size()); } return doInChunkContext(context, chunkContext); } finally { // Still some stuff to do with the data in this chunk, // pass it back. if (!chunkContext.isComplete()) { attributeQueue.add(chunkContext); } StepSynchronizationManager.close(); } }
From source file:org.springframework.batch.core.scope.StepScopePerformanceTests.java
@Before public void start() throws Exception { int count = doTest("vanilla", "warmup"); logger.info("Item count: " + count); StepSynchronizationManager.close();/* www . ja va 2 s . c o m*/ StepSynchronizationManager.register(new StepExecution("step", new JobExecution(0L), 1L)); }
From source file:org.springframework.batch.core.step.AbstractStep.java
/** * Registers the {@link StepExecution} for property resolution via {@link StepScope} * * @param stepExecution/* w ww . j a v a 2 s .c o m*/ */ protected void doExecutionRegistration(StepExecution stepExecution) { StepSynchronizationManager.register(stepExecution); }