/** * 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; }
/** * @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; }
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); }
JobExecution curJobExecution = null; if(StepSynchronizationManager.getContext() != null) { curJobExecution = StepSynchronizationManager.getContext().getStepExecution().getJobExecution();
private javax.batch.runtime.context.StepContext getCurrent() { org.springframework.batch.core.StepExecution curStepExecution = null; if(StepSynchronizationManager.getContext() != null) { curStepExecution = StepSynchronizationManager.getContext().getStepExecution(); } if(curStepExecution == null) { throw new FactoryBeanNotInitializedException("A StepExecution is required"); } StepContext context = contextHolder.get(); // If the current context applies to the current step, use it if(context != null && context.getStepExecutionId() == curStepExecution.getId()) { return context; } Properties stepProperties = batchPropertyContext.getStepProperties(curStepExecution.getStepName()); if(stepProperties != null) { context = new JsrStepContext(curStepExecution, stepProperties); } else { context = new JsrStepContext(curStepExecution, new Properties()); } contextHolder.set(context); return context; }
@Override public Message<?> preSend(Message<?> message, MessageChannel channel) { StepContext context = StepSynchronizationManager.getContext(); if (context == null) { return message; } return MessageBuilder.fromMessage(message).setHeader(STEP_EXECUTION, context.getStepExecution()).build(); }
public String process(String item) throws Exception { StepContext context = StepSynchronizationManager.getContext(); assertTrue(context != null && context.getStepExecution() != null); return item + item; }; };
@Test public void testGetContext() { assertNull(StepSynchronizationManager.getContext()); StepSynchronizationManager.register(stepExecution); assertNotNull(StepSynchronizationManager.getContext()); }
@Test public void testRegisterNull() { assertNull(StepSynchronizationManager.getContext()); StepSynchronizationManager.register(null); assertNull(StepSynchronizationManager.getContext()); }
@Test public void testDefaultStepContext() throws Exception { TestContext testContext = getTestContext(new Object()); listener.prepareTestInstance(testContext); listener.beforeTestMethod(testContext); StepContext context = StepSynchronizationManager.getContext(); assertNotNull(context); listener.afterTestMethod(testContext); assertNull(StepSynchronizationManager.getContext()); }
@Before @After public void start() { while (StepSynchronizationManager.getContext() != null) { StepSynchronizationManager.close(); } }
@Test public void testGetContextWithBatchProperties() { StepContext context = StepSynchronizationManager.getContext(); assertNull(context); StepSynchronizationManager.register(stepExecution, propertyContext); context = StepSynchronizationManager.getContext(); assertNotNull(context); assertEquals(stepExecution, context.getStepExecution()); }
@Override public StepContext call() throws Exception { try { StepSynchronizationManager.register(stepExecution); StepContext context = StepSynchronizationManager.getContext(); context.setAttribute("foo", "bar"); return context; } finally { StepSynchronizationManager.close(); } } });
@Test public void testClose() throws Exception { final List<String> list = new ArrayList<>(); StepContext context = StepSynchronizationManager.register(stepExecution); context.registerDestructionCallback("foo", new Runnable() { @Override public void run() { list.add("foo"); } }); StepSynchronizationManager.close(); assertNull(StepSynchronizationManager.getContext()); assertEquals(0, list.size()); }
@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 void testJobParameters(Object target) throws Exception { TestContext testContext = getTestContext(target); listener.prepareTestInstance(testContext); try { listener.beforeTestMethod(testContext); StepContext context = StepSynchronizationManager.getContext(); assertNotNull(context); assertEquals("spam", context.getJobParameters().get("foo")); } finally { listener.afterTestMethod(testContext); } assertNull(StepSynchronizationManager.getContext()); }
private void testExecutionContext(Object target) throws Exception { TestContext testContext = getTestContext(target); listener.prepareTestInstance(testContext); try { listener.beforeTestMethod(testContext); StepContext context = StepSynchronizationManager.getContext(); assertNotNull(context); assertEquals("bar", context.getStepExecutionContext().get("foo")); } finally { listener.afterTestMethod(testContext); } assertNull(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()); }
@Override public void execute(StepExecution stepExecution) throws JobInterruptedException { context = StepSynchronizationManager.getContext(); setContextFromCollaborator(); stepExecution.getExecutionContext().put("foo", "changed but it shouldn't affect the collaborator"); setContextFromCollaborator(); }
@Test public void testScopeCreation() throws Exception { vanilla.execute(new StepExecution("foo", new JobExecution(11L), 12L)); assertNotNull(TestStep.getContext()); assertNull(StepSynchronizationManager.getContext()); }