3

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;
 }
}
asked Nov 17, 2020 at 7:44
1

2 Answers 2

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;
 }
answered Nov 17, 2020 at 8:04
Sign up to request clarification or add additional context in comments.

Comments

1

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.

answered Nov 17, 2020 at 8:00

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.