Java Code Examples for org.springframework.batch.core.StepContribution

The following examples show how to use org.springframework.batch.core.StepContribution. These examples are extracted from open source projects. 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 Project: pinpoint   Source File: HealthCheckTasklet.java    License: 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
@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 3
Source Project: tutorials   Source File: JobConfiguration.java    License: 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 4
Source Project: spring-graalvm-native   Source File: BatchConfiguration.java    License: 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 5
@Bean
public Step ourBatchStep() {
	return stepBuilderFactory.get("stepPackPub1")
			.tasklet(new Tasklet() {
				public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
					return null;
				}
			})
			.build();
}
 
Example 6
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
    propertyResolverConsumer.accept(new PropertySourcesPropertyResolver(propertySources(
            stepExecution.getJobExecution().getJobConfigurationName(),
            stepExecution.getJobParameters().toProperties(), environment)));
    return FINISHED;
}
 
Example 7
Source Project: spring-batch   Source File: FileDeletingTasklet.java    License: 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 8
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 9
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 10
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 11
Source Project: seed   Source File: BaseTasklet.java    License: 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 12
Source Project: seed   Source File: StepService0005.java    License: Apache License 2.0 5 votes vote down vote up
private Tasklet nextBizDateTasklet(){
    return new Tasklet() {
        @Override
        public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
            //入参[email protected]ef3fc[readCount=0,writeCount=0,filterCount=0,parentSkipCount=0,readSkipCount=0,writeSkipCount=0,processSkipCount=0,exitStatus=exitCode=EXECUTING;exitDescription=],chunkCo[email protected]2313a648[stepContext=SynchronizedAttributeAccessor: [], stepExecutionContext={batch.taskletType=com.jadyer.seed.controller.batch.javaconfig.StepService0005$1, batch.stepType=org.springframework.batch.core.step.tasklet.TaskletStep}, jobExecutionContext={}, jobParameters={time=1536145118798},complete=false,attributes={}],chunkContext.getStepC[email protected]3441dec5[stepExecution=StepExecution: id=23, version=1, name=step0005, status=STARTED, exitStatus=EXECUTING, readCount=0, filterCount=0, writeCount=0 readSkipCount=0, writeSkipCount=0, processSkipCount=0, commitCount=0, rollbackCount=0, exitDescription=,callbacks={},propertyContext=<null>,support=org.sprin[email protected]0]
            LogUtil.getLogger().info("入参contribution={},chunkContext={},chunkContext.getStepContext={}", ReflectionToStringBuilder.toString(contribution), ReflectionToStringBuilder.toString(chunkContext), ReflectionToStringBuilder.toString(chunkContext.getStepContext()));
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return RepeatStatus.FINISHED;
        }
    };
}
 
Example 13
Source Project: seed   Source File: Tasklet301.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
    File dataFile = new File("D:\\data\\seedboot-batch-result.txt");
    for(Person obj : personRepository.findAll()){
        StringBuilder sb = new StringBuilder();
        sb.append(obj.getAge()).append("|").append(obj.getRealName()).append("\n");
        FileUtils.writeStringToFile(dataFile, sb.toString(), StandardCharsets.UTF_8, true);
    }
    return RepeatStatus.FINISHED;
}
 
Example 14
Source Project: journaldev   Source File: BatchConfiguration.java    License: MIT License 5 votes vote down vote up
@Bean
public Step step1() {
  return stepBuilderFactory.get("step1")
      .tasklet(new Tasklet() {
        public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
          return null;
        }
      })
      .build();
}
 
Example 15
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 16
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 17
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 18
Source Project: spring-cloud-task   Source File: BatchEventsApplication.java    License: 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 19
Source Project: spring-cloud-task   Source File: JobConfiguration.java    License: 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 20
Source Project: spring-cloud-task   Source File: JobConfiguration.java    License: 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 21
Source Project: spring-cloud-task   Source File: JobConfiguration.java    License: 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 Project: spring-cloud-task   Source File: JobSkipConfiguration.java    License: 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 23
Source Project: spring-cloud-task   Source File: JobConfiguration.java    License: 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 24
@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 25
@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 26
@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 27
@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 28
@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 29
@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 30
@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();
}