org.springframework.batch.core.scope.context.ChunkContext Java Examples

The following examples show how to use org.springframework.batch.core.scope.context.ChunkContext. 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: HealthCheckTasklet.java    From pinpoint with Apache License 2.0 7 votes vote down vote up
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
    List<String> urlList = generatedFlinkManagerServerApi();

    if (urlList.isEmpty()) {
        return RepeatStatus.FINISHED;
    }

    Map<String, Boolean> jobExecuteStatus = createjobExecuteStatus();

    for (String url : urlList) {
        try {
            ResponseEntity<Map> responseEntity = this.restTemplate.exchange(url, HttpMethod.GET, null, Map.class);

            if (responseEntity.getStatusCode() != HttpStatus.OK) {
                continue;
            }

            checkJobExecuteStatus(responseEntity, jobExecuteStatus);
        } catch (Exception e) {
            logger.error("fail call api to flink server.", e);
        }
    }

    List<String> notExecuteJobList = new ArrayList<>(3);
    for (Map.Entry<String, Boolean> entry : jobExecuteStatus.entrySet()) {
        if (entry.getValue().equals(Boolean.FALSE)) {
            notExecuteJobList.add(entry.getKey());
        }
    }

    if (notExecuteJobList.size() > 0) {
        String exceptionMessage = String.format("job fail : %s", notExecuteJobList);
        throw new Exception(exceptionMessage);
    }

    return RepeatStatus.FINISHED;
}
 
Example #2
Source File: TaskLauncherTaskletTests.java    From composed-task-runner with Apache License 2.0 7 votes vote down vote up
@Test
@DirtiesContext
public void testTaskLauncherTasklet() throws Exception{
	createCompleteTaskExecution(0);
	TaskLauncherTasklet taskLauncherTasklet =
			getTaskExecutionTasklet();
	ChunkContext chunkContext = chunkContext();
	mockReturnValForTaskExecution(1L);
	execute(taskLauncherTasklet, null, chunkContext);
	assertEquals(1L, chunkContext.getStepContext()
			.getStepExecution().getExecutionContext()
			.get("task-execution-id"));

	mockReturnValForTaskExecution(2L);
	chunkContext = chunkContext();
	createCompleteTaskExecution(0);
	taskLauncherTasklet = getTaskExecutionTasklet();
	execute(taskLauncherTasklet, null, chunkContext);
	assertEquals(2L, chunkContext.getStepContext()
			.getStepExecution().getExecutionContext()
			.get("task-execution-id"));
}
 
Example #3
Source File: TaskLauncherTaskletTests.java    From spring-cloud-dataflow with Apache License 2.0 6 votes vote down vote up
@Test
@DirtiesContext
public void testInvalidTaskName() {
	final String ERROR_MESSAGE =
			"Could not find task definition named " + TASK_NAME;
	VndErrors errors = new VndErrors("message", ERROR_MESSAGE, new Link("ref"));
	Mockito.doThrow(new DataFlowClientException(errors))
			.when(this.taskOperations)
			.launch(ArgumentMatchers.anyString(),
					ArgumentMatchers.any(),
					ArgumentMatchers.any(), ArgumentMatchers.any());
	TaskLauncherTasklet taskLauncherTasklet = getTaskExecutionTasklet();
	ChunkContext chunkContext = chunkContext();
	Throwable exception = assertThrows(DataFlowClientException.class,
			() -> taskLauncherTasklet.execute(null, chunkContext));
	Assertions.assertThat(exception.getMessage()).isEqualTo(ERROR_MESSAGE);
}
 
Example #4
Source File: TaskLauncherTaskletTests.java    From spring-cloud-dataflow with Apache License 2.0 6 votes vote down vote up
@Test
@DirtiesContext
public void testTaskLauncherTaskletWithTaskExecutionIdWithPreviousParentID() throws Exception{

	TaskLauncherTasklet taskLauncherTasklet = prepTaskLauncherTests();
	TaskProperties taskProperties = new TaskProperties();
	taskProperties.setExecutionid(88L);
	mockReturnValForTaskExecution(2L);
	ChunkContext chunkContext = chunkContext();
	createCompleteTaskExecution(0);
	chunkContext.getStepContext()
			.getStepExecution().getExecutionContext().put("task-arguments", new ArrayList<String>());
	((List)chunkContext.getStepContext()
			.getStepExecution().getExecutionContext()
			.get("task-arguments")).add("--spring.cloud.task.parent-execution-id=84");
	taskLauncherTasklet = getTaskExecutionTasklet(taskProperties);
	taskLauncherTasklet.setArguments(null);
	execute(taskLauncherTasklet, null, chunkContext);
	assertEquals(2L, chunkContext.getStepContext()
			.getStepExecution().getExecutionContext()
			.get("task-execution-id"));
	assertEquals("--spring.cloud.task.parent-execution-id=88", ((List)chunkContext.getStepContext()
			.getStepExecution().getExecutionContext()
			.get("task-arguments")).get(0));
}
 
Example #5
Source File: TaskLauncherTaskletTests.java    From spring-cloud-dataflow with Apache License 2.0 6 votes vote down vote up
@Test
@DirtiesContext
public void testTaskLauncherTaskletWithTaskExecutionId() throws Exception{
	TaskLauncherTasklet taskLauncherTasklet = prepTaskLauncherTests();

	TaskProperties taskProperties = new TaskProperties();
	taskProperties.setExecutionid(88L);
	mockReturnValForTaskExecution(2L);
	ChunkContext chunkContext = chunkContext();
	createCompleteTaskExecution(0);
	taskLauncherTasklet = getTaskExecutionTasklet(taskProperties);
	taskLauncherTasklet.setArguments(null);
	execute(taskLauncherTasklet, null, chunkContext);
	assertEquals(2L, chunkContext.getStepContext()
			.getStepExecution().getExecutionContext()
			.get("task-execution-id"));
	assertEquals("--spring.cloud.task.parent-execution-id=88", ((List)chunkContext.getStepContext()
			.getStepExecution().getExecutionContext()
			.get("task-arguments")).get(0));
}
 
Example #6
Source File: TaskLauncherTaskletTests.java    From spring-cloud-dataflow with Apache License 2.0 6 votes vote down vote up
@Test
@DirtiesContext
public void testTaskLauncherTasklet() throws Exception{
	createCompleteTaskExecution(0);
	TaskLauncherTasklet taskLauncherTasklet =
			getTaskExecutionTasklet();
	ChunkContext chunkContext = chunkContext();
	mockReturnValForTaskExecution(1L);
	execute(taskLauncherTasklet, null, chunkContext);
	assertEquals(1L, chunkContext.getStepContext()
			.getStepExecution().getExecutionContext()
			.get("task-execution-id"));

	mockReturnValForTaskExecution(2L);
	chunkContext = chunkContext();
	createCompleteTaskExecution(0);
	taskLauncherTasklet = getTaskExecutionTasklet();
	execute(taskLauncherTasklet, null, chunkContext);
	assertEquals(2L, chunkContext.getStepContext()
			.getStepExecution().getExecutionContext()
			.get("task-execution-id"));
}
 
Example #7
Source File: ImportUserTasklet.java    From spring-boot-doma2-sample with Apache License 2.0 6 votes vote down vote up
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws IOException {
    val status = super.execute(contribution, chunkContext);

    val context = BatchContextHolder.getContext();
    val errors = getErrors(context);

    if (isNotEmpty(errors)) {
        errors.forEach(e -> {
            val sourceName = e.getSourceName();
            val position = e.getPosition();
            val errorMessage = e.getErrorMessage();
            log.error("エラーがあります。ファイル名={}, 行数={}, エラーメッセージ={}", sourceName, position, errorMessage);
        });

        throw new ValidationException("取り込むファイルに不正な行が含まれています。");
    }

    return status;
}
 
Example #8
Source File: TaskLauncherTaskletTests.java    From composed-task-runner with Apache License 2.0 6 votes vote down vote up
@Test
@DirtiesContext
public void testInvalidTaskName() {
	final String ERROR_MESSAGE =
			"Could not find task definition named " + TASK_NAME;
	VndErrors errors = new VndErrors("message", ERROR_MESSAGE, new Link("ref"));
	Mockito.doThrow(new DataFlowClientException(errors))
			.when(this.taskOperations)
			.launch(ArgumentMatchers.anyString(),
					ArgumentMatchers.any(),
					ArgumentMatchers.any(), ArgumentMatchers.any());
	TaskLauncherTasklet taskLauncherTasklet = getTaskExecutionTasklet();
	ChunkContext chunkContext = chunkContext();
	Throwable exception = assertThrows(DataFlowClientException.class,
			() -> taskLauncherTasklet.execute(null, chunkContext));
	Assertions.assertThat(exception.getMessage()).isEqualTo(ERROR_MESSAGE);
}
 
Example #9
Source File: CleanupInactiveAgentsTasklet.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
    try {
        adminService.removeInactiveAgents(durationDays);
        return RepeatStatus.FINISHED;
    } catch (Exception e) {
        logger.warn("Failed to execute. message:{}", e.getMessage(), e);
        throw e;
    }
}
 
Example #10
Source File: EventListenerTests.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Test
public void EventEmittingChunkExecutionListenerAfterChunk() {
	final String CHUNK_MESSAGE = "After Chunk Processing";
	ChunkContext chunkContext = getChunkContext();
	this.eventEmittingChunkListener.afterChunk(chunkContext);
	assertThat(this.queueChannel.getQueueSize()).isEqualTo(1);
	Message msg = this.queueChannel.receive();
	assertThat(msg.getPayload()).isEqualTo(CHUNK_MESSAGE);
}
 
Example #11
Source File: UnzipTasklet.java    From spring-batch-performance-tuning with Apache License 2.0 5 votes vote down vote up
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
	try {
		ZipFile zipFile = new ZipFile(file);
		zipFile.extractAll(destination);
	} catch (Exception e) {
		throw new RuntimeException("Failed to unzip file: " + file, e);
	}

	return RepeatStatus.FINISHED;
}
 
Example #12
Source File: TaskJobLauncherCommandLineRunnerTests.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Bean
public Job jobFail() {
	return this.jobBuilderFactory.get("jobA")
			.start(this.stepBuilderFactory.get("step1").tasklet(new Tasklet() {
				@Override
				public RepeatStatus execute(StepContribution contribution,
						ChunkContext chunkContext) throws Exception {
					System.out.println("Executed");
					throw new IllegalStateException("WHOOPS");
				}
			}).build()).build();
}
 
Example #13
Source File: TaskJobLauncherCommandLineRunnerTests.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Bean
public Job jobFun() {
	return this.jobBuilderFactory.get("jobSucceed").start(
			this.stepBuilderFactory.get("step1Succeed").tasklet(new Tasklet() {
				@Override
				public RepeatStatus execute(StepContribution contribution,
						ChunkContext chunkContext) {
					System.out.println("Executed");
					return RepeatStatus.FINISHED;
				}
			}).build()).build();
}
 
Example #14
Source File: CampusCourseCreatorTasklet.java    From olat with Apache License 2.0 5 votes vote down vote up
@Override
public RepeatStatus execute(StepContribution arg0, ChunkContext arg1) throws Exception {
    LOG.info("Methode execute started...");
    createAllCampusCoursesFromTemplate();
    LOG.info("Methode execute finished.");
    return RepeatStatus.FINISHED;
}
 
Example #15
Source File: HiveInitializerTasklet.java    From Intro-to-Spring-Hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public RepeatStatus execute(StepContribution arg0, ChunkContext arg1)
		throws Exception {
	hive.execute(dropDdl);
	hive.execute(createDdl);
	return null;
}
 
Example #16
Source File: BatchEventsApplication.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Bean
public Step step1() {
	return this.stepBuilderFactory.get("step1")
		.tasklet(new Tasklet() {
			@Override
			public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
				System.out.println("Tasklet has run");
				return RepeatStatus.FINISHED;
			}
		}).build();
}
 
Example #17
Source File: HealthCheckTaskletV2.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
    List<String> urlList = generatedFlinkManagerServerApi();

    if (urlList.isEmpty()) {
        return RepeatStatus.FINISHED;
    }

    Map<String, Boolean> jobExecuteStatus = createjobExecuteStatus();

    for (String url : urlList) {
        try {
            ResponseEntity<Map> responseEntity = this.restTemplate.exchange(url, HttpMethod.GET, null, Map.class);

            if (responseEntity.getStatusCode() != HttpStatus.OK) {
                continue;
            }

            checkJobExecuteStatus(responseEntity, jobExecuteStatus);
        } catch (Exception e) {
            logger.error("fail call api to flink server.", e);
        }
    }

    List<String> notExecuteJobList = new ArrayList<>(3);
    for (Map.Entry<String, Boolean> entry : jobExecuteStatus.entrySet()) {
        if (entry.getValue().equals(Boolean.FALSE)) {
            notExecuteJobList.add(entry.getKey());
        }
    }

    if (notExecuteJobList.size() > 0) {
        String exceptionMessage = String.format("job fail : %s", notExecuteJobList);
        throw new Exception(exceptionMessage);
    }

    return RepeatStatus.FINISHED;
}
 
Example #18
Source File: TaskJobLauncherCommandLineRunnerTests.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Bean
public Job job() {
	return this.jobBuilderFactory.get("job")
			.start(this.stepBuilderFactory.get("step1").tasklet(new Tasklet() {
				@Override
				public RepeatStatus execute(StepContribution contribution,
						ChunkContext chunkContext) {
					System.out.println("Executed");
					return RepeatStatus.FINISHED;
				}
			}).build()).build();
}
 
Example #19
Source File: CleanupTasklet.java    From spring-batch-performance-tuning with Apache License 2.0 5 votes vote down vote up
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
	for (String path : paths) {
		FileUtils.cleanDirectory(new File(path));
	}

	return RepeatStatus.FINISHED;
}
 
Example #20
Source File: EventListenerTests.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Test
public void EventEmittingChunkExecutionListenerBeforeChunk() {
	final String CHUNK_MESSAGE = "Before Chunk Processing";
	ChunkContext chunkContext = getChunkContext();
	this.eventEmittingChunkListener.beforeChunk(chunkContext);
	assertThat(this.queueChannel.getQueueSize()).isEqualTo(1);
	Message msg = this.queueChannel.receive();
	assertThat(msg.getPayload()).isEqualTo(CHUNK_MESSAGE);
}
 
Example #21
Source File: JobConfiguration.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Bean
public Job job2() {
	return this.jobBuilderFactory.get("job2")
		.start(this.stepBuilderFactory.get("job2step1")
			.tasklet(new Tasklet() {
				@Override
				public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
					logger.info("Job2 was run");
					return RepeatStatus.FINISHED;
				}
			})
			.build())
		.build();
}
 
Example #22
Source File: JobConfiguration.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Bean
public Job job1() {
	return this.jobBuilderFactory.get("job1")
		.start(this.stepBuilderFactory.get("job1step1")
			.tasklet(new Tasklet() {
				@Override
				public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
					logger.info("Job1 was run");
					return RepeatStatus.FINISHED;
				}
			})
			.build())
		.build();
}
 
Example #23
Source File: JobConfiguration.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Bean
@StepScope
public Tasklet workerTasklet(
	final @Value("#{stepExecutionContext['partitionNumber']}") Integer partitionNumber) {

	return new Tasklet() {
		@Override
		public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
			System.out.println("This tasklet ran partition: " + partitionNumber);

			return RepeatStatus.FINISHED;
		}
	};
}
 
Example #24
Source File: JobSkipConfiguration.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Bean
public Step step1() {
	return this.stepBuilderFactory.get("step1").tasklet(new Tasklet() {
		@Override
		public RepeatStatus execute(StepContribution contribution,
				ChunkContext chunkContext) throws Exception {
			System.out.println("Executed");
			return RepeatStatus.FINISHED;
		}
	}).build();
}
 
Example #25
Source File: ComposedRunnerVisitorConfiguration.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
private Step createTaskletStep(final String taskName) {
	return this.steps.get(taskName)
			.tasklet(new Tasklet() {
				@Override
				public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
					return RepeatStatus.FINISHED;
				}
			})
			.transactionAttribute(getTransactionAttribute())
			.build();
}
 
Example #26
Source File: ComposedRunnerVisitorConfiguration.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
private Step createTaskletStepWithListener(final String taskName,
		StepExecutionListener stepExecutionListener) {
	return this.steps.get(taskName)
			.tasklet(new Tasklet() {
				@Override
				public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
					return RepeatStatus.FINISHED;
				}
			})
			.transactionAttribute(getTransactionAttribute())
			.listener(stepExecutionListener)
			.build();
}
 
Example #27
Source File: TaskLauncherTaskletTests.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
private ChunkContext chunkContext ()
{
	final long JOB_EXECUTION_ID = 123L;
	final String STEP_NAME = "myTestStep";

	JobExecution jobExecution = new JobExecution(JOB_EXECUTION_ID);
	StepExecution stepExecution = new StepExecution(STEP_NAME, jobExecution);
	StepContext stepContext = new StepContext(stepExecution);
	return new ChunkContext(stepContext);
}
 
Example #28
Source File: BatchConfiguration.java    From spring-graalvm-native with Apache License 2.0 5 votes vote down vote up
@Bean
public Step step1() {
	return this.stepBuilderFactory.get("step1")
			.tasklet(new Tasklet() {
				@Override
				public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
					System.out.println(">> This was run in a Spring Batch app!");

					return RepeatStatus.FINISHED;
				}
			}).build();
}
 
Example #29
Source File: TaskLauncherTaskletTests.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
private RepeatStatus execute(TaskLauncherTasklet taskLauncherTasklet, StepContribution contribution,
		ChunkContext chunkContext)  throws Exception{
	RepeatStatus status = taskLauncherTasklet.execute(contribution, chunkContext);
	if (!status.isContinuable()) {
		throw new IllegalStateException("Expected continuable status for the first execution.");
	}
	return taskLauncherTasklet.execute(contribution, chunkContext);

}
 
Example #30
Source File: TaskLauncherTaskletTests.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
@Test
@DirtiesContext
public void testTaskLauncherTaskletFailure() {
	mockReturnValForTaskExecution(1L);
	TaskLauncherTasklet taskLauncherTasklet = getTaskExecutionTasklet();
	ChunkContext chunkContext = chunkContext();
	createCompleteTaskExecution(1);
	Throwable exception = assertThrows(UnexpectedJobExecutionException.class,
			() -> execute(taskLauncherTasklet, null, chunkContext));
	Assertions.assertThat(exception.getMessage()).isEqualTo("Task returned a non zero exit code.");
}