spring cloud task + spring batch+ spring boot 調(diào)度批處理應(yīng)用

1、spring boot啟動(dòng)類加注解@EnableTask 、 @EnableBatchProcessing。
2、基于注解@Configuration 的配置類中,定義Job和step,二者需要加@Bean注解,注入到spring 的容器中
3、eclipse啟動(dòng)時(shí),在springboot主類啟動(dòng)參數(shù)中加 spring.batch.job.names=jobname , spring.cloud.task.name=taskname。 其中,spring.batch.job.names為指定需要執(zhí)行的job,job在多次執(zhí)行過程中不變,spring.cloud.task.name為執(zhí)行本次任務(wù)的命名,未配置可重復(fù)執(zhí)行屬性時(shí),task命名不變時(shí),只允許執(zhí)行一次。多次執(zhí)行需要修改該屬性名?;蛘咔謇韘pring batch / spring cloud task提供的數(shù)據(jù)表,測試階段可進(jìn)行這種騷操作,正式代碼需要參考官網(wǎng)文檔進(jìn)行實(shí)際的處理

mybatis + spring batch 鏈接數(shù)據(jù)庫配置,參考:mybatis-spring

springboot啟動(dòng)類 Application

@EnableBatchProcessing
@EnableTask
@SpringBootApplication
public class Application {
    public static void mian(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
  • @EnableBatchProcessing,該注解在應(yīng)用中只需要出現(xiàn)一次,就能提供JobRepository開箱即用的功能。單獨(dú)使用該注解,job的啟動(dòng)可通過restful接口調(diào)用方式啟動(dòng)job?;蛘呙蟹绞絾?dòng),具體使用參考spring batch官方文檔。
  • @EnableTask,該注解在應(yīng)用中只需要出現(xiàn)一次,就能提供TaskRepository的功能。
  • @EnableBatchProcessing、@EnableTask同時(shí)使用,job可通過任務(wù)調(diào)度方式啟動(dòng)。依賴spring cloud task的調(diào)度,指定job執(zhí)行。

基于Configuration的配置類TestConfiguration

定義了job=testjob。

@Lazy
@Configuration
public class TestConfiguration {
    
    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    
    @Autowired
    private StepBuildFactory stepBuildFactory;
    
    @Autowired
    private SqlSessionFactory sqlSessionFactory


    //@Bean注解不能少,需要在spring啟動(dòng)時(shí),將構(gòu)建的命名為testjob的Job注入到容器中,以便啟動(dòng)時(shí)能被調(diào)用到
    @Bean 
    public Job testJob() throws Exception {
    
        return jobBuilderFactory.get("testjob")  //定義job,指定job命名為testjob, 定義多個(gè)job時(shí),可在啟動(dòng)時(shí)通過spring.batch.job.names=testjob,指定待啟動(dòng)job
                .start(testStep())
                .build();
    
    }
    
    //@Bean注解不能少,需要在spring啟動(dòng)時(shí),將構(gòu)建的命名為testjob的Job注入到容器中,以便啟動(dòng)時(shí)能被調(diào)用到
    @Bean
    public Step testStep() throws Exception {
        
        return stepBuildFactory.get("teststep")
        .<Car, Car> chunk(1) //三個(gè)參數(shù), 泛型的I/O, 括號(hào)內(nèi)的為commit interval,一般從配置文件讀取。
        .reader(reader())
        .processor(processor())
        .write(writer())
        .build();
    }
    
    @Bean
    @StepScope
    public MyBatisCursorItemReader<Car> reader() throws Exception {
        
        return new MyBatisCursorItemReaderBuilder<Car>()
        .sqlSessionFactory(sqlSessionFactory)
        .queryId("com.test.mapper.Itestquery.queryCar")
        .build();
    }
    
    @Bean
    @StepScope
    public CarProcessor processor() {
    
        return new CarProcessor();
    }
    
    @Bean
    @StepScope
    public CarWriter processor() {
    
        return new CarWriter();
    }
}


public class CarProcessor  implements ItemProcessor<Car, Car> {

    @Override
    public Car process(Car item) {
        
        System.out.println("執(zhí)行計(jì)劃,,,,");
    }
}

public class CarWriter<T> implements ItemWriter<T> {

    @Override
    public void write(List<? extends T> items ) throws Exception {
    
        System.out.println("執(zhí)行wirter計(jì)劃,,,");
        for(T t:items) {
            Car car = (Car)t;
            System.out.println(car.toString());
        }
    }
}

public class Car{

    private String carname;
    
    private String price;
    
    private String getCarname() {
        return carname;
    }
    
    private void setCarname(String carname) {
        this.carname = carname;
    }
    
    private String getPrice() {
        return price;
    }
    
    private void setPrice(String price) {
        this.price = price;
    }
}

TestConfiguration中數(shù)據(jù)庫的連接配置
hql語句定義在 com.test.mapper.TestMapper.xml中,存放路徑為classpath下的src/main/resource

<?xml version="1.0" encoding="UTF-8">
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd ">
<mapper namespace="com.test.mapper.ITestMapper">

    <resultMap id="basemap" type="com.test.Car">
        <arg column=""/>
        <arg column=""/>
    </resultMap>

    <sql id="base_list">
        carname, price
    </sql>
    
    <select id="queryCar" resultMap="basemap">
        select 
            <include refid="base_list" />
        from CarTable
    </select>

</mapper>

調(diào)用接口類定義com.test.mapper.ITestMapper ,接口類路徑src/main/java

@Mapper
public interface ITestMapper {
    
    List<Map> queryCar();
}

SqlSessionFactory 自定義

@Configuration
public class SqlSessionFactoryConfig {
    
    @Autowired
    @Qualifier("dataSource")
    private DataSource dataSource;
    
    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory() throws Exception {
    
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/test/mapper/*.xml"));
    
        return factoryBean.getObject();
    }
    
}


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容