Spring Batch: datasource for Batch and datasource for step

六月ゝ 毕业季﹏ 提交于 2020-12-15 06:41:23

问题


I need to get access to two datasources:

  • Spring batch repository: in memory H2
  • My step needs to get access to a mssql.

I've seen several example over there about how to create a custom batch configurer.

However, sometimes:

  1. implements BatchConfigurer
  2. extends DefaultBatchConfigurer

Currently, my configuration files are:

.
├── steps
│   └── MssqlBatchConfigurationStep.java
└── MainJobBatchConfiguration.java

My step configuration is:

@Configuration
public class MssqlBatchConfigurationStep {

    private DataSource dataSource;

    /**
     *
     * @param dataSource
     */
    public MssqlBatchConfigurationStep(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    /**
     *
     * @return
     */
    public ItemReader<Unitat> reader() {
        String sql = "SELECT operation,update_time,table_name,rowid,user_login,user_name, user_ip,application_name,application_version,new_value,old_value FROM renovastorage.data_log";

        JdbcCursorItemReader<Unitat> jdbcCursorItemReader = new JdbcCursorItemReader<>();
        jdbcCursorItemReader.setDataSource(this.dataSource);
        jdbcCursorItemReader.setSql(sql);
        jdbcCursorItemReader.setVerifyCursorPosition(false);
        jdbcCursorItemReader.setRowMapper(new UnitatRowMapper());

        return jdbcCursorItemReader;
    }

    /**
     *
     * @return
     */
    public ItemWriter<UnitatDenormalized> writer() {
        // write to solr
        return null;
    }

}

The problem here is that, this step is getting default datasource. This datasource is the same that is got by Spring Batch.

In order to solve that, I want to create a "Batch Configurer" in order to get specific datasource instead of the default one.

Here you can see my job configuration:

@Configuration
@EnableBatchProcessing
// @EnableScheduling
public class MainJobBatchConfiguration {

    private JobBuilderFactory jobBuilderFactory;
    private StepBuilderFactory stepBuilderFactory;

    private MssqlBatchConfigurationStep unitatBatchStep;

    /**
     *
     * @param jobBuilderFactory
     * @param stepBuilderFactory
     */
    public MainJobBatchConfiguration(
        JobBuilderFactory jobBuilderFactory,
        StepBuilderFactory stepBuilderFactory,
        MssqlBatchConfigurationStep unitatBatchStep
    ) {
        this.jobBuilderFactory = jobBuilderFactory;
        this.stepBuilderFactory = stepBuilderFactory;
        this.unitatBatchStep = unitatBatchStep;
    }


    /**
     *
     * @return
     */
    @Bean
    public Step step() {
        return this.stepBuilderFactory
            .get("mssql")
            .<Unitat, UnitatDenormalized>chunk(10)
            .reader(this.unitatBatchStep.reader())
            .writer(this.unitatBatchStep.writer())
            .build();
    }

    /**
     *
     * @param step
     * @return
     */
    @Bean
    public Job job(Step step) {
        Job job = this.jobBuilderFactory.get("job1")
            .flow(step)
            .end()
            .build();
        return job;
    }

}

回答1:


You need to add a secondary datasource bean and autowire that datasource.

application.properties

spring.second-datasource.url = [url]
spring.second-datasource.username = [username]
spring.second-datasource.password = [password]
spring.second-datasource.driverClassName= [driverClassName]

Datasource config

    @Primary
    @Bean(value = "defaultDataSource")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource datasource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        return dataSource;
    }

    @Bean(value = "secondDataSource")
    @ConfigurationProperties(prefix = "spring.second-datasource")
    public DataSource ticketDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        return dataSource;
    }

Autowire secondDataSource in your reader.

    private DataSource dataSource;

    /**
     *
     * @param dataSource
     */
    public MssqlBatchConfigurationStep(@Qualifier("secondDataSource") DataSource dataSource) {
        this.dataSource = dataSource;
    }



回答2:


My step needs to get access to a mssql.

In order to solve that, I want to create a "Batch Configurer" in order to get specific datasource instead of the default one.

In order to solve that, I would instead add a qualifier on the datasource to specify which one should be used in the step:

@Configuration
public class MssqlBatchConfigurationStep {

   private DataSource dataSource;

   /**
    *
    * @param dataSource
   */
   public MssqlBatchConfigurationStep(@Qualifier("YOUR_MSSQL_DATASOURCE_BEAN_NAME") DataSource dataSource) {
      this.dataSource = dataSource;
   }

}

With that, your reader should be pointing to the mssql datasource and read data from it.



来源:https://stackoverflow.com/questions/64871126/spring-batch-datasource-for-batch-and-datasource-for-step

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!