Spring Batch with Spring boot - 配置 JobRepositoryFactoryBean

Posted

技术标签:

【中文标题】Spring Batch with Spring boot - 配置 JobRepositoryFactoryBean【英文标题】:Spring batch with Spring boot - configuring JobRepositoryFactoryBean 【发布时间】:2018-01-17 15:44:56 【问题描述】:

我是 Spring Batch with Boot 的新手。我在使用 postgres 作为数据库配置 jobRepositoryFactory bean 时遇到问题。 下面是我的配置类。

@Configuration
@EnableBatchProcessing
@Import(DataSourceConfiguration.class)
public class BatchConfiguration 

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    private PlatformTransactionManager transactionManager;

    private final DataSourceConfiguration dataSourceConfiguration;

public BatchConfiguration(DataSourceConfiguration dataSourceConfiguration) 
    this.dataSourceConfiguration = dataSourceConfiguration;



    @Bean
    public ElasticReader elasticReader()  
        return new ElasticReader();
    

@Bean
public JobRepository jobRepositoryFactoryBean() throws Exception 
    JobRepositoryFactoryBean fb = new JobRepositoryFactoryBean();
    fb.setDatabaseType("postgres");
    fb.setDataSource(dataSourceConfiguration.dataSource());
    fb.setTransactionManager(transactionManager);
    return fb.getObject();


@Bean
public PlatformTransactionManager platformTransactionManager() 
    return transactionManager;


    @Bean
    public StageReader stageReader()
        return new StageReader();
    

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

    @Bean
    public ExcelWiter writer() 
        return new ExcelWiter();
    

    @Bean
    public StageWriter stageWriter() 
        return new StageWriter();
    

    @Bean
    public Job importUserJob() 
        return jobBuilderFactory.get("importUserJob")
                .incrementer(new RunIdIncrementer())
                .flow(step1())
                .next(step2())
                .end()
                .build();
    


    @Bean
    public Step step1() 
        return stepBuilderFactory.get("step1")
                .<List<JsonObject>,List<JsonObject>> chunk(10)
                .reader(stageReader())
                .writer(stageWriter())
                .build();
    


    @Bean
    public Step step2() 
        return stepBuilderFactory.get("step2")
                .<List<OnboardConfigVO>,List<ExportVO>> chunk(10)
                .reader(elasticReader())
                .processor(processor())
                .writer(writer())
                .build();
    


DataSourceConfiguration.class

@PropertySource("classpath:/batch-postgresql.properties")
public class DataSourceConfiguration 

@Autowired
private Environment environment;

@Autowired
private ResourceLoader resourceLoader;

@PostConstruct
protected void initialize() 
     ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
 populator.addScript(resourceLoader.getResource(environment.getProperty("bach.schema.script")));



    populator.setContinueOnError(true);
    DatabasePopulatorUtils.execute(populator , dataSource());


@Bean(destroyMethod="close")
public DataSource dataSource() 
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName
       (environment.getProperty(batch.jdbc.driver");
       dataSource.setUrl(environment.getProperty("batch.jdbc.url"));
    dataSource.setUsername(environment.getProperty("batch.jdbc.user"));

dataSource.setPassword(environment.getProperty("batch.jdbc.password"));
    return dataSource;

这是Spring boot App run的输出

Using default security password: f5cddd58-4790-427c-83b8-b6c25044db7f

   2017-08-09 09:36:59.589  INFO 42576 --- [           main] 
o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: 
 OrRequestMatcher [requestMatchers=[Ant [pattern='/css/**'], Ant 
 [pattern='/js/**'], Ant [pattern='/images/**'], Ant 
 [pattern='/webjars/**'], Ant [pattern='/**/favicon.ico'], Ant 
[pattern='/error']]], []
   2017-08-09 09:36:59.785  INFO 42576 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 9199 (http)
  2017-08-09 09:36:59.790  INFO 42576 --- [           main] o.s.b.a.b.JobLauncherCommandLineRunner   : Running default command line with: []
  2017-08-09 09:36:59.794  INFO 42576 --- [           main] o.s.b.c.r.s.JobRepositoryFactoryBean     : No database type set, using meta 
   data indicating: POSTGRES
  2017-08-09 09:36:59.798  INFO 42576 --- [           main] 
    o.s.b.c.l.support.SimpleJobLauncher      : No TaskExecutor has been set, 
    defaulting to synchronous executor.

我已经在我的配置类中配置了 bean。我在这里想念什么?

【问题讨论】:

有什么问题?它正在连接到您的 Postgresql 数据库... @MichaelMinella 这个问题(我认为)是信息警告 * 2017-08-09 09:36:59.794 INFO 42576 --- [main] osbcrsJobRepositoryFactoryBean :没有设置数据库类型,使用元数据表明:POSTGRES* 所以?您没有在配置中指定实际的数据库类型,因此元数据(正确地)计算出来了。这实际上只是一条信息消息。可以忽略。 【参考方案1】:

正如 Michael 所说,这只是来自 Spring Boot 的一条 INFO 消息,它连接到 postgres。

【讨论】:

【参考方案2】:

试试下面的。

@Autowired
private DataSource dataSource;

并将fb.setDataSource(dataSourceConfiguration.dataSource()); 替换为fb.setDataSource(dataSource);

【讨论】:

我尝试了自动装配 DataSource 并在 JobRepositoryFactoryBean 中使用它,但输出是相同的。我也从 BasicDataSource 更改为 DriverManagerDataSource。 您可以在jobRepositoryFactoryBean() 中插入断点,以跟踪dataSource 是否已被实例化或在该点为空。

以上是关于Spring Batch with Spring boot - 配置 JobRepositoryFactoryBean的主要内容,如果未能解决你的问题,请参考以下文章

spring batch(二):核心部分:配置Spring batch

spring batch ftp 集成超时错误 - 使用 spring-boot/spring-batch 进行 ETL

Spring-batch:如何在 Spring Batch 中使用 skip 方法捕获异常消息?

陪你解读Spring Batch带你入手Spring Batch

spring batch读取数据库怎么用

Spring boot spring.batch.job.enabled=false 无法识别