spring boot(多数据源)+spring batch 解决异常: java.lang.IllegalStateException

项目集成说明:

 spring boot(配置多数据源)
 spring batch

数据源配置文件:

@Configuration
public class DataSourceConfig {

    @Primary
    @Bean(name = "primaryDataSource")
    @ConfigurationProperties(prefix="spring.datasource.primary")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "secondaryDataSource")
    @ConfigurationProperties(prefix="spring.datasource.secondary")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }
}

spring batch配置文件:

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {

    //读数据
    @Autowired private ReaderService readerService;
    //处理数据
    @Autowired private PersonItemProcessor processor;
    //写数据
    @Autowired private WriterService writerService;

    @Autowired private JobBuilderFactory jobBuilderFactory;
    @Autowired private StepBuilderFactory stepBuilderFactory;

    /**
     * 异常监听
     * @return
     */
    @Bean
    public JobExecutionListener listener() {
        return new JobNotification();
    }

    /**
     * 导入任务
     */
    @Bean
    public Job importUserJob() {
        return jobBuilderFactory.get("importUserJob")
                .incrementer(new RunIdIncrementer())
                .listener(listener())
                .flow(step1())
                .end()
                .build();
    }

    /**
     * 执行步骤
     */
    @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1")
                .<Person, Person> chunk(10)
                .reader( readerService.readerDatabase() )
                .processor( processor )
                .writer( writerService.writerDatabase() )
                .build();
    }
}

启动spring boot 报错:


***************************
APPLICATION FAILED TO START
***************************

Description:

Field dataSource in com.scp.hello.reader.ReaderService required a single bean, but 2 were found:
    - primaryDataSource: defined by method 'primaryDataSource' in class path resource [com/scp/hello/DataSourceConfig.class]
    - secondaryDataSource: defined by method 'secondaryDataSource' in class path resource [com/scp/hello/DataSourceConfig.class]


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed


Process finished with exit code 1

异常说明:多个数据源中没有指定一个默认数据源。
解决方法:在一个数据源,添加注解@Primary即可。

再次启动spring boot项目,出现异常信息如下:

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:803) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:784) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
    at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:771) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
    at com.scp.SpringBatchApplication.main(SpringBatchApplication.java:12) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_73]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_73]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_73]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_73]
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) [idea_rt.jar:na]
Caused by: java.lang.IllegalStateException: To use the default BatchConfigurer the context must contain no more thanone DataSource, found 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.3.RELEASE.jar:4.3.3.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:192) ~[spring-aop-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    at com.sun.proxy.$Proxy45.getJobInstances(Unknown Source) ~[na:na]
    at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.getNextJobParameters(JobLauncherCommandLineRunner.java:133) ~[spring-boot-autoconfigure-1.4.1.RELEASE.jar:1.4.1.RELEASE]
    at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.execute(JobLauncherCommandLineRunner.java:214) ~[spring-boot-autoconfigure-1.4.1.RELEASE.jar:1.4.1.RELEASE]
    at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.executeLocalJobs(JobLauncherCommandLineRunner.java:233) ~[spring-boot-autoconfigure-1.4.1.RELEASE.jar:1.4.1.RELEASE]
    at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.launchJobFromProperties(JobLauncherCommandLineRunner.java:125) ~[spring-boot-autoconfigure-1.4.1.RELEASE.jar:1.4.1.RELEASE]
    at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.run(JobLauncherCommandLineRunner.java:119) ~[spring-boot-autoconfigure-1.4.1.RELEASE.jar:1.4.1.RELEASE]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:800) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
    ... 11 common frames omitted

异常指出:To use the default BatchConfigurer the context must contain no more thanone DataSource, found 2

异常原因:spring batch启动时,AbstractBatchConfiguration尝试首先在Spring容器中查找BatchConfigurer,如果没有找到,则尝试创建它本身, 这时在容器中找到多个DataSource 的实例,因此抛出IllegalStateException异常。

解决方法:

@Configuration
@EnableBatchProcessing
@ComponentScan(basePackageClasses = DefaultBatchConfigurer.class)
public class BatchConfiguration {

添加注解:@ComponentScan 扫描 DefaultBatchConfigurer.class datasource注入成功。

参考博客(Stack Overflow):
http://stackoverflow.com/questions/25540502/use-of-multiple-datasources-in-spring-batch

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
The `com.alibaba.excel.exception.ExcelGenerateException` with the inner exception `java.lang.OutOfMemoryError: GC overhead limit exceeded` typically occurs when you are trying to generate an Excel file using the Alibaba Excel library (EExcel) in a Java application, and the JVM encounters a severe memory issue. 1. **Issue**: The `OutOfMemoryError: GC overhead limit exceeded` means that the garbage collector (GC) is unable to free up enough memory for the ongoing operation due to excessive memory usage or allocation. The 'GC overhead limit' refers to the threshold beyond which the time spent in garbage collection becomes too high, causing the application to stop processing new requests. 2. **Possible Causes**: - Insufficient heap size: Your application might not have allocated enough memory for the task at hand, particularly when dealing with large datasets or complex formatting. - Memory-intensive operations: Writing a large number of rows, cells, or applying heavy calculations may lead to this error. - Memory leaks: If there are any unintentional leaks in your code where memory isn't being properly released, it can accumulate and eventually hit the limit. 3. **Solutions**: - **Increase Heap Size**: You can try increasing the `-Xmx` flag in your `java` command-line options to allocate more memory for the JVM. However, be cautious as setting it too high might cause other problems if not managed properly. - **Optimize memory usage**: Review your code for memory-intensive operations, consider caching or lazy loading data, and release resources when no longer needed. - **Batch processing**: If possible, break down the generation process into smaller chunks or batches to reduce the memory footprint at any given time. - **Use streaming APIs**: EExcel provides streaming APIs that can help reduce memory consumption by writing data one record at a time.
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值