Spring Batch

◇◆丶佛笑我妖孽 提交于 2020-02-27 02:54:37

pom

<spring-boot.version>2.2.2.RELEASE</spring-boot.version>  
  
<dependency>    
    <groupId>org.springframework.boot</groupId>    
<artifactId>spring-boot-starter-batch</artifactId> </dependency>  

yml

spring:   
  batch:  
 job: enabled: false #是否自动执行定义的Job,默认是  
 initialize-schema: never

本人将write,read,process...等bean定义在java中。把job定义在xml中,这样看的比较清楚。特别是有使用flow,或job之前相互引用时。
自定义配置 如果你想修改JobRepository,jobExplorer 等其他组件时,可以实现BatchConfigurer接口并注册至容器中。系统默认是使用BasicBatchConfigurer作为Spring Batch 的默认配置,你也可以继承它来覆盖要重写的组件

java

@Configuration
@EnableBatchProcessing
@ImportResource("classpath:batch/spring-batch.xml")
public class SpringBatchConfigurer {

    @Autowired
    private ListableJobLocator jobRegistry;
    @Autowired
    private JobLauncher        jobLauncher;
    @Autowired
    private JobRepository      jobRepository;
    @Autowired
    private JobExplorer        jobExplorer;

    @Bean
    public JobOperator jobOperator() {
        SimpleJobOperator jobOperator = new SimpleJobOperator();
        jobOperator.setJobExplorer(this.jobExplorer);
        jobOperator.setJobRepository(this.jobRepository);
        jobOperator.setJobLauncher(this.jobLauncher);
        jobOperator.setJobRegistry(this.jobRegistry);
        return jobOperator;
    }

    /**
     * 异步批处理执行者
     *
     * @author Canaan
     * @date 2020/2/20 9:08
     */
    @Bean
    @Qualifier("async")
    public JobLauncher asyncJobLauncher() throws Exception {
        SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
        jobLauncher.setJobRepository(this.jobRepository);
        jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
        jobLauncher.afterPropertiesSet();
        return jobLauncher;
    }

}
  

spring-batch.xml

<?xml version="1.0" encoding="UTF-8"?>  
<beans:beans xmlns="http://www.springframework.org/schema/batch"
 xmlns:beans="http://www.springframework.org/schema/beans"  
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  xsi:schemaLocation="  
 http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd 
 http://www.springframework.org/schema/batch https://www.springframework.org/schema/batch/spring-batch.xsd">  
 <!--对账批处理作业-->  
 <job id="reconciliationJob" job-repository="jobRepository" restartable="true"> 
	 <step id="step1"> 
		 <tasklet transaction-manager="transactionManager"> 
			 <chunk reader="reconciliationReader" 
				writer="reconciliationWriter" 
				processor="reconciliationProcessor" 
				commit-interval="2"> 
			 </chunk> 
		 </tasklet> 
	 </step> 
</job>  
</beans:beans>  

Component

@Configuration
public class ReconciliationBatchConfig {

    @Autowired
    private SqlSessionFactory       sqlSessionFactory;

    @Bean
    @StepScope
    public MyBatisPagingItemReader<SendHistoryDO> reconciliationReader(@Value("#{jobParameters['date']}") Long date) {
        MyBatisPagingItemReader<SendHistoryDO> myBatisPagingItemReader = new MyBatisPagingItemReader<>();
        myBatisPagingItemReader.setSqlSessionFactory(sqlSessionFactory);
        myBatisPagingItemReader.setPageSize(1000);
        myBatisPagingItemReader.setQueryId("com.xyasia.sms.dao.SendHistoryDao.selectByCriteria");
        Map<String, Object> params = new HashMap<>();
        params.put("createDate_eq", Objects.requireNonNull(date));
        myBatisPagingItemReader.setParameterValues(params);
        return myBatisPagingItemReader;
    }

}  

Test

  
@SpringBootTest(classes = SMSManagerApplication.class)  
@ExtendWith(SpringExtension.class)  
public class BatchJobTest {  
  
 @Autowired 
private JobLauncher jobLauncher;
@Autowired 
@Qualifier("reconciliationJob")
private Job         reconciliationJob;  
	
 @Test 
	void reconciliationJobTest() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException { 
	 JobParameters jobParameters = new JobParametersBuilder()
		 .addLong("date", LocalDate.now().toDate().getTime()) 
		 .toJobParameters();  
	 
 JobExecution jobExecution = this.jobLauncher.run(reconciliationJob, jobParameters); 
	 System.out.println(jobExecution);
 }  
}  
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!