在springbatch(spring-boot-1.5.2.RELEASE)中使用多个数据源,在启动时抛出异常

9 浏览
0 Comments

在springbatch(spring-boot-1.5.2.RELEASE)中使用多个数据源,在启动时抛出异常

我正在尝试使用两个数据源,一个用于从Spring Batch中的元数据表,另一个是我的应用程序数据库来读取/处理/写入。当我尝试同时使用它们时,会抛出以下异常。

java.lang.IllegalStateException: 要使用默认的BatchConfigurer,上下文中不能包含多个数据源,发现了2个
at org.springframework.batch.core.configuration.annotation.AbstractBatchConfiguration.getConfigurer(AbstractBatchConfiguration.java:108) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration.initialize(SimpleBatchConfiguration.java:114) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$ReferenceTargetSource.createObject(SimpleBatchConfiguration.java:142) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.aop.target.AbstractLazyCreationTargetSource.getTarget(AbstractLazyCreationTargetSource.java:86) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:192) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at com.sun.proxy.$Proxy43.getJobInstances(Unknown Source)

import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import com.springbatch_sample.domain.Person;
@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    
    @Bean
    @Primary
    public DataSource hsqldbDataSource() throws SQLException {
        final SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
        dataSource.setDriver(new org.hsqldb.jdbcDriver());
        dataSource.setUrl("jdbc:hsqldb:hsql://localhost:9001/xdb");
        dataSource.setUsername("sa");
        dataSource.setPassword("sa");
        return dataSource;
    }
    
    @Bean
    public JdbcTemplate jdbcTemplate(final DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
    
    @Bean
    @Qualifier("mysqlDataSource")
    public DataSource mysqlDataSource() throws SQLException {
        final SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
        dataSource.setDriver(new com.ibm.db2.jcc.DB2Driver());
        dataSource.setUrl("jdbc:db2://xxxx");
        dataSource.setUsername("user");
        dataSource.setPassword("pswd");
        return dataSource;
    }
    
    @Bean
    public FlatFileItemReader reader() {
        FlatFileItemReader reader = new FlatFileItemReader();
        reader.setResource(new ClassPathResource("sample-data.csv"));
        reader.setLineMapper(new DefaultLineMapper() {{
            setLineTokenizer(new DelimitedLineTokenizer() {{
                setNames(new String[] { "firstName", "lastName" });
            }});
            setFieldSetMapper(new BeanWrapperFieldSetMapper() {{
                setTargetType(Person.class);
            }});
        }});
        return reader;
    }
    
    @Bean
    public PersonItemProcessor processor() {
        return new PersonItemProcessor();
    }
    
    @Bean
    public JdbcBatchItemWriter writer() throws SQLException {
        JdbcBatchItemWriter writer = new JdbcBatchItemWriter();
        writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider());
        writer.setSql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)");
        writer.setDataSource(hsqldbDataSource());
        return writer;
    }
    
    @Bean
    public Job importUserJob(JobCompletionNotificationListener listener) throws SQLException {
        return jobBuilderFactory.get("importUserJob")
                .incrementer(new RunIdIncrementer())
                .listener(listener)
                .flow(step1())
                .end()
                .build();
    }
    
    @Bean
    public Step step1() throws SQLException {
        return stepBuilderFactory.get("step1")
                . chunk(10)
                .reader(reader())
                .processor(processor())
                .writer(writer())
                .build();
    }
}

0
0 Comments

使用多个数据源在springbatch(spring-boot-1.5.2.RELEASE)中引发引导异常的问题。

问题的原因是在Spring Batch中使用多个数据源时,可能会引发异常。解决方法是在配置文件中明确指定要使用的数据源。

在解决此问题的链接中提供了解决方法。以下是我根据链接中的信息整理出的内容:

**问题的原因:**

当在Spring Batch中使用多个数据源时,可能会引发异常。

**解决方法:**

在配置文件中明确指定要使用的数据源。

首先,需要在application.properties文件中配置要使用的数据源。以下是一个示例配置:

# 第一个数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/db1
spring.datasource.username=username1
spring.datasource.password=password1
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# 第二个数据源配置
spring.second-datasource.url=jdbc:mysql://localhost:3306/db2
spring.second-datasource.username=username2
spring.second-datasource.password=password2
spring.second-datasource.driver-class-name=com.mysql.jdbc.Driver

然后,需要在配置类中创建两个数据源的bean。以下是一个示例配置:

@Configuration
public class DataSourceConfig {
    @Primary
    @Bean(name = "firstDataSource")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource firstDataSource() {
        return DataSourceBuilder.create().build();
    }
    @Bean(name = "secondDataSource")
    @ConfigurationProperties(prefix = "spring.second-datasource")
    public DataSource secondDataSource() {
        return DataSourceBuilder.create().build();
    }
}

最后,在需要使用数据源的地方,可以通过`@Qualifier`注解指定要使用的数据源。以下是一个示例配置:

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
    @Autowired
    @Qualifier("firstDataSource")
    private DataSource firstDataSource;
    @Autowired
    @Qualifier("secondDataSource")
    private DataSource secondDataSource;
    // 其他配置...
}

通过以上步骤,可以在Spring Batch中成功使用多个数据源。

希望这篇文章对于理解如何在Spring Batch中使用多个数据源有所帮助。

0