/** * @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; }
@Override public String toString() { return super.toString() + ", stepExecutionContext=" + getStepExecutionContext() + ", jobExecutionContext=" + getJobExecutionContext() + ", jobParameters=" + getJobParameters(); }
@SuppressWarnings({"rawtypes", "unchecked"}) public Map<String, Object> getPartitionPlan() { Map<String, Object> partitionPlanProperties = new HashMap<>(); if(propertyContext != null) { Map partitionProperties = propertyContext.getStepProperties(getStepName()); partitionPlanProperties = partitionProperties; } return Collections.unmodifiableMap(partitionPlanProperties); }
/** * @see Scope#get(String, ObjectFactory) */ @Override public Object get(String name, ObjectFactory<?> objectFactory) { StepContext context = getContext(); Object scopedObject = context.getAttribute(name); if (scopedObject == null) { synchronized (mutex) { scopedObject = context.getAttribute(name); if (scopedObject == null) { if (logger.isDebugEnabled()) { logger.debug(String.format("Creating object in scope=%s, name=%s", this.getName(), name)); } scopedObject = objectFactory.getObject(); context.setAttribute(name, scopedObject); } } } return scopedObject; }
@Test public void testGetStepExecution() { context = new StepContext(stepExecution); assertNotNull(context.getStepExecution()); }
private String getStepIdentifier() { StepContext stepContext = StepSynchronizationManager.getContext(); StepExecution stepExecution = StepSynchronizationManager.getContext().getStepExecution(); return stepContext.getJobName() + "." + stepExecution.getStepName(); }
protected void bindContext(ChunkContext context) { MDC.put(MDCBatchPropertyKeys.JOB_FLOW_ID, (String) context.getStepContext().getJobParameters().get(JobParameterConstants.KEY_FLOW_ID)); MDC.put(MDCBatchPropertyKeys.JOB_FLOW_NAME, (String) context.getStepContext().getJobParameters().get(JobParameterConstants.KEY_FLOW_NAME)); MDC.put(MDCBatchPropertyKeys.JOB_EXECUTION_ID, String.valueOf(context.getStepContext().getStepExecution().getJobExecutionId())); MDC.put(MDCBatchPropertyKeys.JOB_EXECUTION_DISCRIMINATOR, buildExecutionDiscriminator(MDC.get(MDCBatchPropertyKeys.JOB_EXECUTION_ID), MDC.get(MDCBatchPropertyKeys.JOB_FLOW_NAME))); }
@Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { Map<String, Object> stepExecutionContext = chunkContext.getStepContext().getStepExecutionContext(); if(stepExecutionContext.containsKey("ran")) { System.out.println("This time we'll let it go."); return RepeatStatus.FINISHED; } else { System.out.println("I don't think so..."); chunkContext.getStepContext().getStepExecution().getExecutionContext().put("ran", true); throw new RuntimeException("Not this time..."); } } };
@Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { long count = QueryExecutionUtils.countQuery(query, qef); String k = key != null ? key : chunkContext.getStepContext().getStepName() + ".count"; chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext().put(k, count); return RepeatStatus.FINISHED; }
@Test public void testDestructionCallbackSunnyDay() throws Exception { context.setAttribute("foo", "FOO"); context.registerDestructionCallback("foo", new Runnable() { @Override public void run() { list.add("bar"); } }); context.close(); assertEquals(1, list.size()); assertEquals("bar", list.get(0)); }
@Test public void testGetStepContext() { StepContext stepContext = context.getStepContext(); assertNotNull(stepContext); assertEquals("bar", context.getStepContext().getJobParameters().get("foo")); }
@Test public void testJobParameters() throws Exception { JobParameters jobParameters = new JobParametersBuilder().addString("foo", "bar").toJobParameters(); JobInstance instance = stepExecution.getJobExecution().getJobInstance(); stepExecution = new StepExecution("step", new JobExecution(instance, jobParameters)); context = new StepContext(stepExecution); assertEquals("bar", context.getJobParameters().get("foo")); }
@Test public void testRegisterDestructionCallback() { final List<String> list = new ArrayList<>(); context.setAttribute("foo", "bar"); scope.registerDestructionCallback("foo", new Runnable() { @Override public void run() { list.add("foo"); } }); assertEquals(0, list.size()); // When the context is closed, provided the attribute exists the // callback is called... context.close(); assertEquals(1, list.size()); }
@Override protected StepContext createNewContext(StepExecution execution, BatchPropertyContext propertyContext) { StepContext context; if(propertyContext != null) { context = new StepContext(execution, propertyContext); } else { context = new StepContext(execution); } return context; }
@Test public void testDestructionCallbackMissingAttribute() throws Exception { context.registerDestructionCallback("foo", new Runnable() { @Override public void run() { list.add("bar"); } }); context.close(); // Yes the callback should be called even if the attribute is missing - // for inner beans assertEquals(1, list.size()); }
@Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { Map<String, Object> context = chunkContext.getStepContext().getJobExecutionContext(); if(context.get("restart") != null) { contribution.setExitStatus(new ExitStatus("ES3")); } else { chunkContext.getStepContext().setAttribute("restart", true); contribution.setExitStatus(new ExitStatus("ES4")); } return RepeatStatus.FINISHED; } }
@Test public void testStepExecutionContext() throws Exception { ExecutionContext executionContext = stepExecution.getExecutionContext(); executionContext.put("foo", "bar"); assertEquals("bar", context.getStepExecutionContext().get("foo")); }
@Test(expected = IllegalStateException.class) public void testIllegalContextId() throws Exception { context = new StepContext(new StepExecution("foo", new JobExecution(0L))); context.getId(); }
private void setContextFromCollaborator() { if (context != null) { context.setAttribute("collaborator", collaborator.getName()); context.setAttribute("collaborator.class", collaborator.getClass().toString()); if (collaborator.getParent()!=null) { context.setAttribute("parent", collaborator.getParent().getName()); context.setAttribute("parent.class", collaborator.getParent().getClass().toString()); } } }
@Test public void testRemove() { context.setAttribute("foo", "bar"); scope.remove("foo"); assertFalse(context.hasAttribute("foo")); }