Java Code Examples for org.springframework.batch.item.ExecutionContext#put()

The following examples show how to use org.springframework.batch.item.ExecutionContext#put() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JobConfiguration.java    From spring-cloud-task with Apache License 2.0 6 votes vote down vote up
@Bean
public Partitioner partitioner() {
	return new Partitioner() {
		@Override
		public Map<String, ExecutionContext> partition(int gridSize) {

			Map<String, ExecutionContext> partitions = new HashMap<>(gridSize);

			for (int i = 0; i < GRID_SIZE; i++) {
				ExecutionContext context1 = new ExecutionContext();
				context1.put("partitionNumber", i);

				partitions.put("partition" + i, context1);
			}

			return partitions;
		}
	};
}
 
Example 2
Source File: CogstackJobPartitioner.java    From CogStack-Pipeline with Apache License 2.0 5 votes vote down vote up
private void populateContextMapWithPartitionCountLimit(ScheduledPartitionParams params, Map<String, ExecutionContext> result) {
    long partitionCount = (params.getMaxId() -params.getMinId()+1);
    logger.info("There are fewer or equal new records than the grid size. Expect " + partitionCount+
            " partitions this execution") ;
    for(long i = 0;i<(partitionCount);i++) {
        ExecutionContext value = new ExecutionContext();
        result.put("partition" + (i + 1L), value);
        value.putLong("minValue", (params.getMinId()+i) );
        value.putLong("maxValue", (params.getMinId()+i) );
        value.put("min_time_stamp", params.getMinTimeStamp().toString());
        value.put("max_time_stamp", params.getMaxTimeStamp().toString());
    }
}
 
Example 3
Source File: CogstackJobPartitioner.java    From CogStack-Pipeline with Apache License 2.0 5 votes vote down vote up
private ExecutionContext getNewExecutionContext(ScheduledPartitionParams params, long start, long end) {
    ExecutionContext value = new ExecutionContext();
    value.putLong("minValue", start);
    value.putLong("maxValue", end);
    value.put("min_time_stamp", params.getMinTimeStamp().toString());
    value.put("max_time_stamp", params.getMaxTimeStamp().toString());
    return value;
}
 
Example 4
Source File: ExecutionContextDeserializer.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
@Override
public ExecutionContext deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
		throws IOException, JsonProcessingException {
	ObjectCodec oc = jsonParser.getCodec();
	JsonNode node = oc.readTree(jsonParser);

	final ExecutionContext executionContext = new ExecutionContext();
	final boolean dirty = node.get("dirty").asBoolean();

	for (JsonNode valueNode : node.get("values")) {

		final JsonNode nodeValue = valueNode.fields().next().getValue();
		final String nodeKey = valueNode.fields().next().getKey();

		if (nodeValue.isNumber() && !nodeValue.isFloatingPointNumber() && nodeValue.canConvertToInt()) {
			executionContext.putInt(nodeKey, nodeValue.asInt());
		}
		else if (nodeValue.isNumber() && !nodeValue.isFloatingPointNumber() && nodeValue.canConvertToLong()) {
			executionContext.putLong(nodeKey, nodeValue.asLong());
		}
		else if (nodeValue.isFloatingPointNumber()) {
			executionContext.putDouble(nodeKey, nodeValue.asDouble());
		}
		else if (nodeValue.isBoolean()) {
			executionContext.putString(nodeKey, String.valueOf(nodeValue.asBoolean()));
		}
		else if (nodeValue.isTextual()) {
			executionContext.putString(nodeKey, nodeValue.asText());
		}
		else {
			executionContext.put(nodeKey, nodeValue.toString());
		}
	}

	if (!dirty && executionContext.isDirty()) {
		executionContext.clearDirtyFlag();
	}

	return executionContext;
}
 
Example 5
Source File: StepExecutionJacksonMixInTests.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
private StepExecution getStepExecution() {
	JobExecution jobExecution = new JobExecution(1L, null, "hi");
	final StepExecution stepExecution = new StepExecution("step1", jobExecution);
	jobExecution.createStepExecution("step1");
	final ExecutionContext executionContext = stepExecution.getExecutionContext();

	executionContext.putInt("counter", 1234);
	executionContext.putDouble("myDouble", 1.123456d);
	executionContext.putLong("Josh", 4444444444L);
	executionContext.putString("awesomeString", "Yep");
	executionContext.put("hello", "world");
	executionContext.put("counter2", 9999);

	return stepExecution;
}
 
Example 6
Source File: ExecutionContextSerializationTests.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerializationOfExecutionContext() throws IOException {
	final ObjectMapper objectMapper = DataFlowTemplate.prepareObjectMapper(new ObjectMapper());

	final ExecutionContext stepExecutionExecutionContext = new ExecutionContext();
	stepExecutionExecutionContext.put("foo", "bar");
	stepExecutionExecutionContext.put("foo2", "bar2");

	final String serializedExecutionContext = objectMapper.writeValueAsString(stepExecutionExecutionContext);
	final String expectedExecutionContext = "{\"dirty\":true,\"empty\":false,\"values\":[{\"foo\":\"bar\"},{\"foo2\":\"bar2\"}]}";
	assertEquals(expectedExecutionContext, serializedExecutionContext);

}
 
Example 7
Source File: JobStepExecutionControllerTests.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
private void createStepExecution(String jobName, String... stepNames) {
	JobInstance instance = jobRepository.createJobInstance(jobName, new JobParameters());
	JobExecution jobExecution = jobRepository.createJobExecution(instance, new JobParameters(), null);
	for (String stepName : stepNames) {
		StepExecution stepExecution = new StepExecution(stepName, jobExecution, 1L);
		stepExecution.setId(null);
		ExecutionContext context = new ExecutionContext();
		context.put("stepval", stepName);
		stepExecution.setExecutionContext(context);
		jobRepository.add(stepExecution);
	}
	TaskExecution taskExecution = dao.createTaskExecution(jobName, new Date(), new ArrayList<String>(), null);
	taskBatchDao.saveRelationship(taskExecution, jobExecution);
}
 
Example 8
Source File: StepExecutionEventTests.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Test
public void testExecutionContext() {
	ExecutionContext executionContext = new ExecutionContext();
	executionContext.put("hello", "world");
	StepExecutionEvent stepExecutionEvent = new StepExecutionEvent(
			getBasicStepExecution());
	assertThat(stepExecutionEvent.getExecutionContext()).isNotNull();
	stepExecutionEvent.setExecutionContext(executionContext);
	assertThat(stepExecutionEvent.getExecutionContext().getString("hello"))
			.isEqualTo("world");
}
 
Example 9
Source File: JobExecutionEventTests.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Test
public void testExecutionContext() {
	ExecutionContext executionContext = new ExecutionContext();
	executionContext.put("hello", "world");
	JobExecutionEvent jobExecutionEvent = new JobExecutionEvent();
	assertThat(jobExecutionEvent.getExecutionContext()).isNotNull();
	jobExecutionEvent.setExecutionContext(executionContext);
	assertThat(jobExecutionEvent.getExecutionContext().getString("hello"))
			.isEqualTo("world");
}
 
Example 10
Source File: TaskLauncherTasklet.java    From composed-task-runner with Apache License 2.0 4 votes vote down vote up
/**
 * Executes the task as specified by the taskName with the associated
 * properties and arguments.
 *
 * @param contribution mutable state to be passed back to update the current step execution
 * @param chunkContext contains the task-execution-id used by the listener.
 * @return Repeat status of FINISHED.
 */
@Override
public RepeatStatus execute(StepContribution contribution,
		ChunkContext chunkContext) {
	if (this.executionId == null) {
		this.timeout = System.currentTimeMillis() +
				this.composedTaskProperties.getMaxWaitTime();
		logger.debug("Wait time for this task to complete is " +
				this.composedTaskProperties.getMaxWaitTime());
		logger.debug("Interval check time for this task to complete is " +
				this.composedTaskProperties.getIntervalTimeBetweenChecks());

		String tmpTaskName = this.taskName.substring(0,
				this.taskName.lastIndexOf('_'));

		List<String> args = this.arguments;

		ExecutionContext stepExecutionContext = chunkContext.getStepContext().getStepExecution().
				getExecutionContext();
		if (stepExecutionContext.containsKey("task-arguments")) {
			args = (List<String>) stepExecutionContext.get("task-arguments");
		}
		if(this.taskProperties.getExecutionid() != null) {
			args.add("--spring.cloud.task.parent-execution-id=" + this.taskProperties.getExecutionid());
		}
		this.executionId = this.taskOperations.launch(tmpTaskName,
				this.properties, args, null);

		stepExecutionContext.put("task-execution-id", executionId);
		stepExecutionContext.put("task-arguments", args);
	}
	else {
		try {
			Thread.sleep(this.composedTaskProperties.getIntervalTimeBetweenChecks());
		}
		catch (InterruptedException e) {
			Thread.currentThread().interrupt();
			throw new IllegalStateException(e.getMessage(), e);
		}

		TaskExecution taskExecution =
				this.taskExplorer.getTaskExecution(this.executionId);
		if (taskExecution != null && taskExecution.getEndTime() != null) {
			if (taskExecution.getExitCode() == null) {
				throw new UnexpectedJobExecutionException("Task returned a null exit code.");
			}
			else if (taskExecution.getExitCode() != 0) {
				throw new UnexpectedJobExecutionException("Task returned a non zero exit code.");
			}
			else {
				return RepeatStatus.FINISHED;
			}
		}
		if (this.composedTaskProperties.getMaxWaitTime() > 0 &&
				System.currentTimeMillis() > timeout) {
			throw new TaskExecutionTimeoutException(String.format(
					"Timeout occurred while processing task with Execution Id %s",
					this.executionId));
		}
	}
	return RepeatStatus.CONTINUABLE;
}
 
Example 11
Source File: TaskLauncherTasklet.java    From spring-cloud-dataflow with Apache License 2.0 4 votes vote down vote up
/**
 * Executes the task as specified by the taskName with the associated
 * properties and arguments.
 *
 * @param contribution mutable state to be passed back to update the current step execution
 * @param chunkContext contains the task-execution-id used by the listener.
 * @return Repeat status of FINISHED.
 */
@Override
public RepeatStatus execute(StepContribution contribution,
		ChunkContext chunkContext) {
	if (this.executionId == null) {
		this.timeout = System.currentTimeMillis() +
				this.composedTaskProperties.getMaxWaitTime();
		logger.debug("Wait time for this task to complete is " +
				this.composedTaskProperties.getMaxWaitTime());
		logger.debug("Interval check time for this task to complete is " +
				this.composedTaskProperties.getIntervalTimeBetweenChecks());

		String tmpTaskName = this.taskName.substring(0,
				this.taskName.lastIndexOf('_'));

		List<String> args = this.arguments;

		ExecutionContext stepExecutionContext = chunkContext.getStepContext().getStepExecution().
				getExecutionContext();
		if (stepExecutionContext.containsKey("task-arguments")) {
			args = (List<String>) stepExecutionContext.get("task-arguments");
		}
		List<String> cleansedArgs = new ArrayList<>();
		if(args != null) {
			for(String argument : args) {
				if(!argument.startsWith("--spring.cloud.task.parent-execution-id=")) {
					cleansedArgs.add(argument);
				}
			}
			args = cleansedArgs;
		}
		if(this.taskProperties.getExecutionid() != null) {
			args.add("--spring.cloud.task.parent-execution-id=" + this.taskProperties.getExecutionid());
		}
		if(StringUtils.hasText(this.composedTaskProperties.getPlatformName())) {
			properties.put("spring.cloud.dataflow.task.platformName", this.composedTaskProperties.getPlatformName());
		}
		this.executionId = this.taskOperations.launch(tmpTaskName,
				this.properties, args, null);

		stepExecutionContext.put("task-execution-id", executionId);
		stepExecutionContext.put("task-arguments", args);
	}
	else {
		try {
			Thread.sleep(this.composedTaskProperties.getIntervalTimeBetweenChecks());
		}
		catch (InterruptedException e) {
			Thread.currentThread().interrupt();
			throw new IllegalStateException(e.getMessage(), e);
		}

		TaskExecution taskExecution =
				this.taskExplorer.getTaskExecution(this.executionId);
		if (taskExecution != null && taskExecution.getEndTime() != null) {
			if (taskExecution.getExitCode() == null) {
				throw new UnexpectedJobExecutionException("Task returned a null exit code.");
			}
			else if (taskExecution.getExitCode() != 0) {
				throw new UnexpectedJobExecutionException("Task returned a non zero exit code.");
			}
			else {
				return RepeatStatus.FINISHED;
			}
		}
		if (this.composedTaskProperties.getMaxWaitTime() > 0 &&
				System.currentTimeMillis() > timeout) {
			throw new TaskExecutionTimeoutException(String.format(
					"Timeout occurred while processing task with Execution Id %s",
					this.executionId));
		}
	}
	return RepeatStatus.CONTINUABLE;
}