使用Spring启动的Spring批处理 - 配置JobRepositoryFactoryBean

时间:2017-08-09 16:22:04

标签: spring spring-boot spring-batch

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

 id | user1 | user2 | confirmed
-------------------------------
  1 |     1 |     2 | 1502296162
  2 |     3 |     1 | 1502296161
  3 |     2 |     4 | 1502296160

DataSourceConfiguration.class

@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();
    }

}

以下是Spring启动App运行的输出

@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;
}

我已在配置类中配置了bean。我在这里错过了什么?

2 个答案:

答案 0 :(得分:0)

请尝试以下操作。

@Autowired
private DataSource dataSource;

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

答案 1 :(得分:0)

正如迈克尔所说,这只是来自Spring Boot的INFO消息,它连接到postgres。