@Override public StepContext call() throws Exception { try { StepSynchronizationManager.register(stepExecution); StepContext context = StepSynchronizationManager.getContext(); context.setAttribute("foo", "bar"); return context; } finally { StepSynchronizationManager.close(); } } });
@Around("execution(void org.springframework.batch.core.Step+.execute(*)) && target(step) && args(stepExecution)") public void execute(Step step, StepExecution stepExecution) throws JobInterruptedException { StepSynchronizationManager.register(stepExecution); try { step.execute(stepExecution); } finally { StepSynchronizationManager.release(); } }
/** * Get an attribute accessor in the form of a {@link StepContext} that can * be used to store scoped bean instances. * * @return the current step context which we can use as a scope storage * medium */ private StepContext getContext() { StepContext context = StepSynchronizationManager.getContext(); if (context == null) { throw new IllegalStateException("No context holder available for step scope"); } return context; }
@Test public void testGetContext() { assertNull(StepSynchronizationManager.getContext()); StepSynchronizationManager.register(stepExecution); assertNotNull(StepSynchronizationManager.getContext()); }
@Test public void testRelease() { StepContext context = StepSynchronizationManager.register(stepExecution); final List<String> list = new ArrayList<>(); context.registerDestructionCallback("foo", new Runnable() { @Override public void run() { list.add("foo"); } }); // On release we expect the destruction callbacks to be called StepSynchronizationManager.release(); assertNull(StepSynchronizationManager.getContext()); assertEquals(1, list.size()); }
/** * @param testContext the current test context * @throws Exception if there is a problem * @see TestExecutionListener#afterTestMethod(TestContext) */ @Override public void afterTestMethod(TestContext testContext) throws Exception { Method method = TestContext.class.getMethod(HAS_ATTRIBUTE_METHOD_NAME, String.class); Boolean hasAttribute = (Boolean) ReflectionUtils.invokeMethod(method, testContext, STEP_EXECUTION); if (hasAttribute) { StepSynchronizationManager.close(); } }
@Before @After public void start() { while (StepSynchronizationManager.getContext() != null) { StepSynchronizationManager.close(); } }
@Override protected void doExecutionRegistration(StepExecution stepExecution) { StepSynchronizationManager.register(stepExecution, propertyContext); } }
/** * Releases the most recent {@link StepExecution} */ protected void doExecutionRelease() { StepSynchronizationManager.release(); }
/** * @return the current step execution if there is one */ private StepExecution getStepExecution() { StepContext context = StepSynchronizationManager.getContext(); if (context==null) { return null; } StepExecution stepExecution = context.getStepExecution(); return stepExecution; }
@Test public void testRegisterNull() { assertNull(StepSynchronizationManager.getContext()); StepSynchronizationManager.register(null); assertNull(StepSynchronizationManager.getContext()); }
/** * Added to clean up left overs from other tests. * @throws Exception */ @BeforeClass public static void setUpClass() throws Exception { StepSynchronizationManager.close(); }
/** * Registers the {@link StepExecution} for property resolution via {@link StepScope} * * @param stepExecution StepExecution to use when hydrating the StepScoped beans */ protected void doExecutionRegistration(StepExecution stepExecution) { StepSynchronizationManager.register(stepExecution); }
@After public void close() { StepSynchronizationManager.release(); if (context != null) { context.close(); } }
@Override public StepContext call() throws Exception { try { StepSynchronizationManager.register(new StepExecution("step" + count, new JobExecution(count))); StepContext context = factory.getObject(); Thread.sleep(1000L); return context; } catch (Throwable ignore) { return null; }finally { StepSynchronizationManager.close(); } } }));
@Test public void testRegisterTwice() { StepSynchronizationManager.register(stepExecution); StepSynchronizationManager.register(stepExecution); StepSynchronizationManager.close(); // if someone registers you have to assume they are going to close, so // the last thing you want is for the close to remove another context // that someone else has registered assertNotNull(StepSynchronizationManager.getContext()); StepSynchronizationManager.close(); assertNull(StepSynchronizationManager.getContext()); }
private Properties getArtifactProperties(String artifactName) { String originalArtifactName = artifactName; if(originalArtifactName.startsWith(SCOPED_TARGET_BEAN_PREFIX)) { originalArtifactName = artifactName.substring(SCOPED_TARGET_BEAN_PREFIX.length()); } StepContext stepContext = StepSynchronizationManager.getContext(); if (stepContext != null) { return batchPropertyContext.getStepArtifactProperties(stepContext.getStepName(), originalArtifactName); } return batchPropertyContext.getArtifactProperties(originalArtifactName); }