org.springframework.batch.core.StepContribution Java Examples

The following examples show how to use org.springframework.batch.core.StepContribution. 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: JobConfiguration.java    From tutorials with MIT License 6 votes vote down vote up
@Bean
public Job job2() {
    return jobBuilderFactory.get("job2")
        .start(stepBuilderFactory.get("job2step1")
            .tasklet(new Tasklet() {
                @Override
                public RepeatStatus execute(
                    StepContribution contribution,
                    ChunkContext chunkContext)
                    throws Exception {
                    LOGGER
                        .info("This job is from Baeldung");
                    return RepeatStatus.FINISHED;
                }
            })
            .build())
        .build();
}
 
Example #3
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 #4
Source File: JobConfiguration.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 #5
Source File: TaskBatchExecutionListenerTests.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) throws Exception {
					System.out.println("Executed");
					return RepeatStatus.FINISHED;
				}
			}).build()).build();
}
 
Example #6
Source File: TaskBatchExecutionListenerTests.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) throws Exception {
					System.out.println("Executed");
					return RepeatStatus.FINISHED;
				}
			}).build()).build();
}
 
Example #7
Source File: TaskBatchExecutionListenerTests.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Bean
public FactoryBean<Job> job() {
	return new FactoryBean<Job>() {
		@Override
		public Job getObject() throws Exception {
			return JobFactoryBeanConfiguration.this.jobBuilderFactory.get("job")
					.start(JobFactoryBeanConfiguration.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())
					.build();
		}

		@Override
		public Class<?> getObjectType() {
			return Job.class;
		}

		@Override
		public boolean isSingleton() {
			return true;
		}
	};
}
 
Example #8
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 #9
Source File: TaskBatchExecutionListenerTests.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 {
					System.out.println("Executed job1");
					return RepeatStatus.FINISHED;
				}
			}).build()).build();
}
 
Example #10
Source File: TaskBatchExecutionListenerTests.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 {
					System.out.println("Executed job2");
					return RepeatStatus.FINISHED;
				}
			}).build()).build();
}
 
Example #11
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 #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: 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 #17
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 #18
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 #19
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 #20
Source File: LinesProcessor.java    From tutorials with MIT License 5 votes vote down vote up
@Override
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
    for (Line line : lines) {
        long age = ChronoUnit.YEARS.between(line.getDob(), LocalDate.now());
        logger.debug("Calculated age " + age + " for line " + line.toString());
        line.setAge(age);
    }
    return RepeatStatus.FINISHED;
}
 
Example #21
Source File: LinesReader.java    From tutorials with MIT License 5 votes vote down vote up
@Override
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
    Line line = fu.readLine();
    while (line != null) {
        lines.add(line);
        logger.debug("Read line: " + line.toString());
        line = fu.readLine();
    }
    return RepeatStatus.FINISHED;
}
 
Example #22
Source File: LinesWriter.java    From tutorials with MIT License 5 votes vote down vote up
@Override
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
    for (Line line : lines) {
        fu.writeLine(line);
        logger.debug("Wrote line " + line.toString());
    }
    return RepeatStatus.FINISHED;
}
 
Example #23
Source File: JobConfiguration.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public Job job() {
    return jobBuilderFactory.get("job").start(stepBuilderFactory.get("jobStep1").tasklet(new Tasklet() {
        @Override
        public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
            logger.info("Job was run");
            return RepeatStatus.FINISHED;
        }
    }).build()).build();
}
 
Example #24
Source File: JobConfiguration.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public Step step1() {
    return this.stepBuilderFactory.get("job1step1")
        .tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(
                StepContribution contribution,
                ChunkContext chunkContext)
                throws Exception {
                LOGGER.info("Tasklet has run");
                return RepeatStatus.FINISHED;
            }
        }).build();
}
 
Example #25
Source File: BaseTasklet.java    From seed with Apache License 2.0 5 votes vote down vote up
@Override
final public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
	this.stepContribution = stepContribution;
	this.chunkContext = chunkContext;
	// 画面传入参数取得
	Map<String, Object> jobParameters = chunkContext.getStepContext().getJobParameters();
	this.doExec(jobParameters);
	return RepeatStatus.FINISHED;
}
 
Example #26
Source File: SimpleBatchApplication.java    From Software-Architecture-with-Spring-5.0 with MIT License 5 votes vote down vote up
@Bean
public Step ourBatchStep() {
	return stepBuilderFactory.get("stepPackPub1")
			.tasklet(new Tasklet() {
				public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
					return null;
				}
			})
			.build();
}
 
Example #27
Source File: PropertyResolverConsumerTasklet.java    From spring-batch-rest with Apache License 2.0 5 votes vote down vote up
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
    propertyResolverConsumer.accept(new PropertySourcesPropertyResolver(propertySources(
            stepExecution.getJobExecution().getJobConfigurationName(),
            stepExecution.getJobParameters().toProperties(), environment)));
    return FINISHED;
}
 
Example #28
Source File: FileDeletingTasklet.java    From spring-batch with MIT License 5 votes vote down vote up
@Override
public RepeatStatus execute(StepContribution stepContribution,
    ChunkContext chunkContext) {
  try (Stream<Path> walk =
      Files.walk(Paths.get(directory.getFile().getPath()))) {
    walk.filter(Files::isRegularFile).map(Path::toFile)
        .forEach(File::delete);
  } catch (IOException e) {
    LOGGER.error("error deleting files", e);
    throw new UnexpectedJobExecutionException(
        "unable to delete files");
  }

  return RepeatStatus.FINISHED;
}
 
Example #29
Source File: TaskLauncherTaskletTests.java    From composed-task-runner 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: ComposedRunnerVisitorConfiguration.java    From composed-task-runner 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();
}