springbatch 实例
package com.jeesite.modules.sdbatch.service; import com.jeesite.common.idgen.IdGen; import com.jeesite.modules.medrec.service.MedRecMainService; import com.jeesite.modules.sdcommon.DateUtils; import com.jeesite.modules.sdform.service.SdCourseService; import com.jeesite.modules.util.ProjectUtils; import lombok.SneakyThrows; import org.apache.xmlbeans.impl.common.XPath; import org.junit.Test; import org.springframework.batch.core.*; import org.springframework.batch.core.annotation.BeforeStep; import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; import org.springframework.batch.core.launch.JobLauncher; import org.springframework.batch.core.launch.support.RunIdIncrementer; import org.springframework.batch.core.listener.ExecutionContextPromotionListener; import org.springframework.batch.core.scope.context.ChunkContext; import org.springframework.batch.core.scope.context.StepContext; import org.springframework.batch.core.step.tasklet.Tasklet; import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.repeat.RepeatStatus; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.ArrayList; import java.util.List; /** * TaskletWay 通过匿名内部类调用业务处理方法 */ @Configuration //用于告诉spring,咱们这个类是一个自定义配置类,里面很多bean都需要加载到spring容器里面 public class TaskletWay { @Autowired private StepBuilderFactory stepBuilderFactory; @Autowired private MedRecMainService medRecMainService; @Autowired private SdCourseService sdCourseService; @Autowired private JobBuilderFactory jobBuilderFactory; @Autowired private JobLauncher jobLauncher; /** * * @return */ @Bean public Tasklet diseaseLet() { //lambda 表达式简化匿名内部类 return (StepContribution contribution, ChunkContext chunkContext) -> { sdCourseService.diseaseScript(); return RepeatStatus.FINISHED; }; } @Bean public Tasklet PIPLet() { //lambda 表达式简化匿名内部类 return (StepContribution contribution, ChunkContext chunkContext) -> { sdCourseService.specialScript("PIP"); return RepeatStatus.FINISHED; }; } @Bean public Tasklet DVTLet() { //lambda 表达式简化匿名内部类 return (StepContribution contribution, ChunkContext chunkContext) -> { sdCourseService.specialScript("DVT"); return RepeatStatus.FINISHED; }; } @Bean public Tasklet VTELet() { //lambda 表达式简化匿名内部类 return (StepContribution contribution, ChunkContext chunkContext) -> { sdCourseService.specialScript("VTE"); return RepeatStatus.FINISHED; }; } @Bean public Tasklet fieldLet() { //lambda 表达式简化匿名内部类 return (StepContribution contribution, ChunkContext chunkContext) -> { sdCourseService.fieldScript(); return RepeatStatus.FINISHED; }; } @Bean public Tasklet formLet() { //lambda 表达式简化匿名内部类 return (StepContribution contribution, ChunkContext chunkContext) -> { sdCourseService.formScript(); return RepeatStatus.FINISHED; }; } @Bean public Step diseaseStep() { return stepBuilderFactory .get("diseaseStep").tasklet(diseaseLet()) .build(); } @Bean public Step PIPStep() { return stepBuilderFactory .get("PIPStep").tasklet(PIPLet()) .build(); } @Bean public Step DVTStep() { return stepBuilderFactory .get("DVTStep").tasklet(DVTLet()) .build(); } @Bean public Step VTEStep() { return stepBuilderFactory .get("VTEStep").tasklet(VTELet()) .build(); } @Bean public Step fieldStep() { return stepBuilderFactory .get("fieldStep").tasklet(fieldLet()) .build(); } @Bean public Step formStep() { return stepBuilderFactory .get("formStep").tasklet(formLet()) .build(); } /** * 先确定 * 然后处理 * 然后调用 * 下来进行 */ @SneakyThrows @Bean public void deterSdBatch() { Job job = jobBuilderFactory.get("diseaseBatch") .incrementer(new RunIdIncrementer()) .flow(diseaseStep()) .next(DVTStep()) .next(PIPStep()) .next(VTEStep()) .next(formStep()) .next(fieldStep()) .end() .build(); JobParameters jobParameters = new JobParametersBuilder() .addString("batch_no", DateUtils.getDate()) //batch参数1 .addString("time", DateUtils.getTime()) //batch参数2 .toJobParameters(); jobLauncher.run(job, jobParameters); } /** * 获取 * * @return */ public Tasklet eleRecordLet() { return (StepContribution contribution, ChunkContext chunkContext) -> { ExecutionContext executionContext = contribution.getStepExecution().getJobExecution().getExecutionContext(); //进行向下一步骤传参 List<String> ids = sdCourseService.eleRecord(); executionContext.put("inpatientIds", ids); // 调用接口 // sdCourseService.; //返回list<String> return RepeatStatus.FINISHED; }; } /** * 调用生成 * * @return */ @Bean public Tasklet modelEntityLet() { return (StepContribution contribution, ChunkContext chunkContext) -> { ExecutionContext executionContext = contribution.getStepExecution().getJobExecution().getExecutionContext(); List<String> ids = (List<String>) executionContext.get("inpatientIds"); //获取上个步骤传来的参数 if(ids != null){ sdCourseService.modelAPI(ids); } return RepeatStatus.FINISHED; }; } @Bean public Step eleRecordStep() { return stepBuilderFactory .get("eleRecordStep") .tasklet(eleRecordLet()) .build(); } @Bean public Step modelEntityStep() { return stepBuilderFactory .get("modelEntityStep").tasklet(modelEntityLet()) .build(); } @SneakyThrows @Bean public void entityBatch() { Job job = jobBuilderFactory.get("entityBatch") .incrementer(new RunIdIncrementer()) .flow(eleRecordStep()) .next(modelEntityStep()) .end() .build(); JobParameters jobParameters = new JobParametersBuilder() .addString("batch_no", DateUtils.getDate()) .addString("time", DateUtils.getTime()) .toJobParameters(); jobLauncher.run(job, jobParameters); } }
先创建 let 然后创建step 过程是通过step来进行串接的,let是主要调用逻辑代码和实现参数传递的
小白技术分享