美文网首页
spring-batch 只需两个类,直接跑任务

spring-batch 只需两个类,直接跑任务

作者: 我还是老油条 | 来源:发表于2023-05-17 15:49 被阅读0次

    config类

    package net.petrikainulainen.springbatch.database;
    
    import net.petrikainulainen.springbatch.common.LoggingStudentProcessor;
    import net.petrikainulainen.springbatch.common.LoggingStudentWriter;
    import net.petrikainulainen.springbatch.student.StudentDTO;
    import org.springframework.batch.core.Job;
    import org.springframework.batch.core.Step;
    import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
    import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
    import org.springframework.batch.core.launch.support.RunIdIncrementer;
    import org.springframework.batch.item.ItemProcessor;
    import org.springframework.batch.item.ItemReader;
    import org.springframework.batch.item.ItemWriter;
    import org.springframework.batch.item.database.Order;
    import org.springframework.batch.item.database.PagingQueryProvider;
    import org.springframework.batch.item.database.builder.JdbcPagingItemReaderBuilder;
    import org.springframework.batch.item.database.support.SqlPagingQueryProviderFactoryBean;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.env.Environment;
    import org.springframework.jdbc.core.BeanPropertyRowMapper;
    import org.springframework.web.client.RestTemplate;
    
    import javax.sql.DataSource;
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * @author Petri Kainulainen
     */
    @Configuration
    public class DatabaseStudentJobConfig {
    
        private static final String PROPERTY_REST_API_URL = "rest.api.to.database.job.api.url";
    
    
    
        @Bean
        ItemProcessor<StudentDTO, StudentDTO> restStudentProcessor() {
            return new LoggingStudentProcessor();
        }
    
        @Bean
        ItemWriter<StudentDTO> restStudentWriter() {
            return new LoggingStudentWriter();
        }
    
        @Bean
        Step restStudentStep(ItemReader<StudentDTO> jdbcPaginationItemReader,
                             ItemProcessor<StudentDTO, StudentDTO> restStudentProcessor,
                             ItemWriter<StudentDTO> restStudentWriter,
                             StepBuilderFactory stepBuilderFactory) {
            return stepBuilderFactory.get("restStudentStep")
                    .<StudentDTO, StudentDTO>chunk(1)
                    .reader(jdbcPaginationItemReader)
                    .processor(restStudentProcessor)
                    .writer(restStudentWriter)
                    .build();
        }
    
        @Bean
        Job restStudentJob(JobBuilderFactory jobBuilderFactory,
                           @Qualifier("restStudentStep") Step restStudentStep) {
            return jobBuilderFactory.get("restStudentJob")
                    .incrementer(new RunIdIncrementer())
                    .flow(restStudentStep)
                    .end()
                    .build();
        }
    
        @Bean
        public ItemReader<StudentDTO> jdbcPaginationItemReader(DataSource dataSource, PagingQueryProvider queryProvider) {
            return new JdbcPagingItemReaderBuilder<StudentDTO>()
                    .name("pagingItemReader")
                    .dataSource(dataSource)
                    .pageSize(1)
                    .queryProvider(queryProvider)
                    .rowMapper(new BeanPropertyRowMapper<>(StudentDTO.class))
                    .build();
        }
    
        @Bean
        public SqlPagingQueryProviderFactoryBean queryProvider(DataSource dataSource) {
            SqlPagingQueryProviderFactoryBean queryProvider = new SqlPagingQueryProviderFactoryBean();
    
            queryProvider.setDataSource(dataSource);
            queryProvider.setSelectClause("SELECT email_address, name, purchased_package");
            queryProvider.setFromClause("FROM student");
            queryProvider.setSortKeys(sortByEmailAddressAsc());
    
            return queryProvider;
        }
    
        private Map<String, Order> sortByEmailAddressAsc() {
            Map<String, Order> sortConfiguration = new HashMap<>();
            sortConfiguration.put("email_address", Order.ASCENDING);
            return sortConfiguration;
        }
    
    
    }
    
    

    joblauncher

    package net.petrikainulainen.springbatch.database;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.batch.core.Job;
    import org.springframework.batch.core.JobParameter;
    import org.springframework.batch.core.JobParameters;
    import org.springframework.batch.core.JobParametersInvalidException;
    import org.springframework.batch.core.launch.JobLauncher;
    import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
    import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
    import org.springframework.batch.core.repository.JobRestartException;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * @author Petri Kainulainen
     */
    @Component
    public class DatabaseStudentJobLauncher {
    
    
        private static final Logger LOGGER = LoggerFactory.getLogger(DatabaseStudentJobLauncher.class);
    
        private final Job job;
    
        private final JobLauncher jobLauncher;
    
        @Autowired
        DatabaseStudentJobLauncher(@Qualifier("restStudentJob") Job job, JobLauncher jobLauncher) {
            this.job = job;
            this.jobLauncher = jobLauncher;
        }
    
        @Scheduled(cron = "${rest.api.to.database.job.cron:0 */1 * * * ?}")
        void launchXmlFileToDatabaseJob() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
            LOGGER.info("Starting restStudentJob job");
    
            jobLauncher.run(job, newExecution());
    
            LOGGER.info("Stopping restStudentJob job");
        }
    
        private JobParameters newExecution() {
            Map<String, JobParameter> parameters = new HashMap<>();
    
            JobParameter parameter = new JobParameter(new Date());
            parameters.put("currentTime", parameter);
    
            return new JobParameters(parameters);
        }
    }
    
    

    pom文件

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.3.1.RELEASE</version>
        </parent>
    
           <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-batch</artifactId>
            </dependency>
    
    package net.petrikainulainen.springbatch;
    
    import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.Bean;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import org.springframework.web.client.RestTemplate;
    
    /**
     * @author Petri Kainulainen
     */
    @SpringBootApplication
    @EnableBatchProcessing
    @EnableScheduling
    public class SpringBatchExampleApplication {
    
        @Bean
        RestTemplate restTemplate() {
            return new RestTemplate();
        }
    
        public static void main(String[] args) {
            SpringApplication.run(SpringBatchExampleApplication.class, args);
        }
    }
    
    

    有个sql 需要手动执行

    org\springframework\batch\spring-batch-core\4.2.4.RELEASE\spring-batch-core-4.2.4.RELEASE.jar!\org\springframework\batch\core\schema-mysql.sql
    

    properties

    spring.batch.job.enabled=false
    
    #Database Configuration
    spring.datasource.url=jdbc:mysql://172.0.0.1:3306/batch?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull
    spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
    spring.datasource.username=root
    spring.datasource.password=123456
    

    相关文章

      网友评论

          本文标题:spring-batch 只需两个类,直接跑任务

          本文链接:https://www.haomeiwen.com/subject/gppisdtx.html