org.springframework.batch.repeat.RepeatStatus Java Examples

The following examples show how to use org.springframework.batch.repeat.RepeatStatus. 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: DefaultExceptionJobDemo.java    From SpringAll with MIT License 6 votes vote down vote up
@Bean
public Job defaultExceptionJob() {
    return jobBuilderFactory.get("defaultExceptionJob")
            .start(
                stepBuilderFactory.get("step")
                    .tasklet((stepContribution, chunkContext) -> {
                        // 获取执行上下文
                        ExecutionContext executionContext = chunkContext.getStepContext().getStepExecution().getExecutionContext();
                        if (executionContext.containsKey("success")) {
                            System.out.println("任务执行成功");
                            return RepeatStatus.FINISHED;
                        } else {
                            String errorMessage = "处理任务过程发生异常";
                            System.out.println(errorMessage);
                            executionContext.put("success", true);
                            throw new RuntimeException(errorMessage);
                        }

                    }).build()
            ).build();
}
 
Example #4
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 #5
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 #6
Source File: TaskJobLauncherCommandLineRunner.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
private void monitorJobExecutions() {
	RepeatTemplate template = new RepeatTemplate();

	Date startDate = new Date();

	template.iterate(context -> {

		List<JobExecution> failedJobExecutions = new ArrayList<>();
		RepeatStatus repeatStatus = RepeatStatus.FINISHED;
		for (JobExecution jobExecution : this.jobExecutionList) {
			JobExecution currentJobExecution = this.taskJobExplorer
					.getJobExecution(jobExecution.getId());
			BatchStatus batchStatus = currentJobExecution.getStatus();
			if (batchStatus.isRunning()) {
				repeatStatus = RepeatStatus.CONTINUABLE;
			}
			if (batchStatus.equals(BatchStatus.FAILED)) {
				failedJobExecutions.add(jobExecution);
			}
		}
		Thread.sleep(this.taskBatchProperties.getFailOnJobFailurePollInterval());

		if (repeatStatus.equals(RepeatStatus.FINISHED)
				&& failedJobExecutions.size() > 0) {
			throwJobFailedException(failedJobExecutions);
		}
		return repeatStatus;
	});
}
 
Example #7
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 #8
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 #9
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 #10
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 #11
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 #12
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 #13
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 #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: 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 #16
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 #17
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 #18
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 #19
Source File: PrefixTests.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((contribution, chunkContext) -> {
				System.out.println("Executed");
				return RepeatStatus.FINISHED;
			}).build()).build();
}
 
Example #20
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 #21
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 #22
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 #23
Source File: AddressImportJobConfiguration.java    From spring-batch-lightmin with Apache License 2.0 5 votes vote down vote up
@Bean
@JobScope
public Tasklet deleteFileTasklet(@Value("#{jobParameters['fileSource']}") final String pathToFile) {
    return (contribution, chunkContext) -> {
        final File file = new File(pathToFile);
        final boolean deleted = file.delete();
        final RepeatStatus repeatStatus;
        if (deleted) {
            repeatStatus = RepeatStatus.FINISHED;
        } else {
            repeatStatus = RepeatStatus.CONTINUABLE;
        }
        return repeatStatus;
    };
}
 
Example #24
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 #25
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 #26
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 #27
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 #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: BatchConfiguration.java    From messaging with Apache License 2.0 5 votes vote down vote up
@Bean
Job job(JobBuilderFactory jobBuilderFactory,
 StepBuilderFactory stepBuilderFactory, JdbcTemplate template,
 ItemReader<Contact> fileReader,
 ItemProcessor<Contact, Contact> emailProcessor,
 ItemWriter<Contact> jdbcWriter) {

 Step setup = stepBuilderFactory.get("clean-contact-table")
  .tasklet((contribution, chunkContext) -> {
   template.update("delete from CONTACT");
   return RepeatStatus.FINISHED;
  }).build();

 Step fileToJdbc = stepBuilderFactory.get("file-to-jdbc-fileToJdbc")
  .<Contact, Contact>chunk(5)
  // <1>
  .reader(fileReader).processor(emailProcessor).writer(jdbcWriter)
  .faultTolerant().skip(InvalidEmailException.class)
  // <2>
  .skipPolicy((Throwable t, int skipCount) -> {
   LogFactory.getLog(getClass()).info("skipping ");
   return t.getClass().isAssignableFrom(InvalidEmailException.class);
  }).retry(HttpStatusCodeException.class) // <3>
  .retryLimit(2).build();

 return jobBuilderFactory.get("etl") // <4>
  .start(setup).next(fileToJdbc).build();
}
 
Example #30
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();
}