在 Spring Boot 应用程序中嵌入带有作业定义的 Spring Batch Admin

Posted

技术标签:

【中文标题】在 Spring Boot 应用程序中嵌入带有作业定义的 Spring Batch Admin【英文标题】:Embedding Spring Batch Admin with job definitions in Spring Boot app 【发布时间】:2017-08-12 12:23:23 【问题描述】:

我已经按照示例将 Spring Batch Admin 作为引导应用程序运行 spring-batch-admin-spring-boot

按预期工作。我添加了一个名为 BatchConfiguration 的新配置,定义如下:

package spring.batch.jobs;

... imports ...

@Configuration
public class BatchConfiguration 

    @Autowired
    private Environment env;

    @Autowired
    public PlatformTransactionManager transactionManager;

    @Autowired
    public JobRepository jobRepository;

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    public DataSource dataSource;

    @Bean
    public PlatformTransactionManager transactionManager(DataSource ds) 
        return new DataSourceTransactionManager(ds);
    

    @Bean
    public StepBuilderFactory makeStepBuilderFactory() 
        return new StepBuilderFactory(jobRepository, transactionManager);
    

    @Bean
    public JobRepository createJobRepository() throws Exception 
        JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
        factory.setDataSource(dataSource);
        factory.setTransactionManager(transactionManager);
        String isolationLevelForCreate = env.getProperty("batch.repository.isolationlevelforcreate");
        if (isolationLevelForCreate != null) 
            factory.setIsolationLevelForCreate(isolationLevelForCreate);
        
        String tablePrefix = env.getProperty("batch.repository.tableprefix");
        if (tablePrefix != null) 
            factory.setTablePrefix(tablePrefix);
        
        factory.afterPropertiesSet();
        return factory.getObject();
    

    @Bean
    public JobBuilderFactory makeJobBuilderFactory() 
        return new JobBuilderFactory(jobRepository);
    

    @Bean
    public DataSource dataSource(Environment e) 
        org.apache.tomcat.jdbc.pool.DataSource ds = new org.apache.tomcat.jdbc.pool.DataSource();
        ds.setDriverClassName(e.getProperty("batch.jdbc.driver"));
        ds.setUsername(e.getProperty("batch.jdbc.user"));
        ds.setPassword(e.getProperty("batch.jdbc.password"));
        ds.setUrl(e.getProperty("batch.jdbc.url"));
        return ds;
    

    // tag::readerwriterprocessor[]
    @Bean
    public FlatFileItemReader<Person> reader() 
        FlatFileItemReader<Person> reader = new FlatFileItemReader<Person>();
        reader.setResource(new ClassPathResource("sample-data.csv"));
        reader.setLineMapper(new DefaultLineMapper<Person>() 
            setLineTokenizer(new DelimitedLineTokenizer() 
                setNames(new String[]"firstName", "lastName");
            );
            setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() 
                setTargetType(Person.class);
            );
        );
        return reader;
    

    @Bean
    public PersonItemProcessor processor() 
        return new PersonItemProcessor();
    

    @Bean
    public JdbcBatchItemWriter<Person> writer() 
        JdbcBatchItemWriter<Person> writer = new JdbcBatchItemWriter<Person>();
        writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Person>());
        writer.setSql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)");
        writer.setDataSource(dataSource);
        return writer;
    
    // end::readerwriterprocessor[]

    // tag::jobstep[]
    @Bean
    public Job importUserJob() 
        return jobBuilderFactory.get("importUserJob")
                .incrementer(new RunIdIncrementer())
                .flow(step1())
                .end()
                .build();
    

    @Bean
    public Step step1() 
        return stepBuilderFactory.get("step1")
                .<Person, Person>chunk(10)
                .reader(reader())
                .processor(processor())
                .writer(writer())
                .build();
    
    // end::jobstep[]

然后我通过将类添加到导入注释中,将此配置导入MainConfiguration。

启动应用程序后,我没有看到工作页面下列出的importUserJob。如何在 Spring Batch admin 中正确注册我的作业,以便在 UI 中列出并运行它?

【问题讨论】:

【参考方案1】:

通过将以下内容添加到 main/resources/META-INF/spring/batch/jobs/myjob.xml 中,我能够获得在我的项目中注册的 Spring Batch 管理员中定义的作业

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="spring.batch.jobs"/>
</beans>

这似乎告诉应用上下文spring.batch.jobs包下有基于注解的配置类需要注册。

【讨论】:

以上是关于在 Spring Boot 应用程序中嵌入带有作业定义的 Spring Batch Admin的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 CommandLineJobRunner 调用嵌入在 Spring Boot 应用程序中的 Spring Batch 作业

如何在带有嵌入式 tomcat 的 Spring Boot 应用程序中运行 hawt.io

运行带有嵌入式容器和 JNDI 的 Spring Boot 应用程序

使用带有嵌入式 Tomcat 的 Spring Boot Weblfux

如何在带有嵌入式 tomcat 的 Spring Boot App 中设置域名

带有嵌入式 Tomcat 的 Jndi Mongodb Spring Boot,[重复]