SpringBoot整合SpringBatch项目,已将代码开源至github,访问地址:https://github.com/cmlbeliever/SpringBatch 欢迎star or fork!

在框架整合的过程中,由于需要添加db读写分离配置,因此项目中有两个DataSource,运行batch后报错如下:

java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:779)
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:760)
at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:747)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
at com.cml.learning.framework.module.BaseModule.run(BaseModule.java:39)
at com.cml.learning.module.bat00X.Bat00XModule.main(Bat00XModule.java:20)
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)
at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration.initialize(SimpleBatchConfiguration.java:114)
at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$ReferenceTargetSource.createObject(SimpleBatchConfiguration.java:142)
at org.springframework.aop.target.AbstractLazyCreationTargetSource.getTarget(AbstractLazyCreationTargetSource.java:86)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:192)
at com.sun.proxy.$Proxy55.getJobInstances(Unknown Source)
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.getNextJobParameters(JobLauncherCommandLineRunner.java:131)
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.execute(JobLauncherCommandLineRunner.java:212)
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.executeLocalJobs(JobLauncherCommandLineRunner.java:231)
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.launchJobFromProperties(JobLauncherCommandLineRunner.java:123)
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.run(JobLauncherCommandLineRunner.java:117)
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:776)
... 5 common frames omitted

意思是要使用default BatchConfigurer,则项目中只能有一个Datasource。

根据异常堆栈信息,找到抛出此异常的源码位置和实现代码:

org.springframework.batch.core.configuration.annotation.AbstractBatchConfiguration

@Autowired(required = false)
private Collection<DataSource> dataSources; protected BatchConfigurer getConfigurer(Collection<BatchConfigurer> configurers) throws Exception {
if (this.configurer != null) {
return this.configurer;
}
if (configurers == null || configurers.isEmpty()) {
if (dataSources == null || dataSources.isEmpty()) {
DefaultBatchConfigurer configurer = new DefaultBatchConfigurer();
configurer.initialize();
this.configurer = configurer;
return configurer;
} else if(dataSources != null && dataSources.size() == 1) {
DataSource dataSource = dataSources.iterator().next();
DefaultBatchConfigurer configurer = new DefaultBatchConfigurer(dataSource);
configurer.initialize();
this.configurer = configurer;
return configurer;
} else {
throw new IllegalStateException("To use the default BatchConfigurer the context must contain no more than" +
"one DataSource, found " + dataSources.size());
}
}
if (configurers.size() > 1) {
throw new IllegalStateException(
"To use a custom BatchConfigurer the context must contain precisely one, found "
+ configurers.size());
}
this.configurer = configurers.iterator().next();
return this.configurer;
}

根据源码得知,当数据源不存的时候,BatchConfigure会将batch数据存储到内存中,当数据源只有一个时,会根据数据源建立batch执行所需要的表。当数据源有多个的时候就抛出异常,这样难怪,因为BatchConfigure在多个数据源的时候,它根本不知道要根据哪个数据源建立batch执行所需要的表。

既然知道了原因,只需要自定义BatchConfigure,并且在多数据源的情况下指定一个默认的数据源即可解决。

实现步骤:

1、将框架SimpleBatchConfiguration,AbstractBatchConfiguration源码复制出来

2、重写AbstractBatchConfiguration.getConfigurer方法,设置默认数据源

protected BatchConfigurer getConfigurer(Collection<BatchConfigurer> configurers) throws Exception {
if (this.configurer != null) {
return this.configurer;
}
if (configurers == null || configurers.isEmpty()) {
if (defaultDataSource == null) {
throw new IllegalStateException("Data source can not be null!!!");
} DefaultBatchConfigurer configurer = new DefaultBatchConfigurer(defaultDataSource);
configurer.initialize();
this.configurer = configurer;
return configurer;
}
if (configurers.size() > 1) {
throw new IllegalStateException("To use a custom BatchConfigurer the context must contain precisely one, found " + configurers.size());
}
this.configurer = configurers.iterator().next();
return this.configurer;
}

3、添加注解,将重写的BatchConfigure引入

@Import({ SimpleBatchConfiguration.class })

4、经过以上三个步骤,即可实现多数据源下SpringBatch启动报错问题

以上代码已经整合到SpringBatch,已将代码开源至github,访问地址:https://github.com/cmlbeliever/SpringBatch 欢迎star or fork!

如果有其他更好的方法,还请不吝赐教。

SpringBatch异常To use the default BatchConfigurer the context must contain no more thanone DataSource的更多相关文章

  1. Hibernate 异常 —— Unable to instantiate default tuplize

    出现这个异常 —— Unable to instantiate default tuplizer ,是 Hibernate 的映射文件(*.hbm.xml)导致的.仔细检查一下工程里的映射文件吧. 笔 ...

  2. spring异常 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderServlet

    spring异常 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderServlet   情况 ...

  3. asp.net4.0在Global中的Application_Start 中直接或间接使用 HttpUtility.UrlEncode等出现异常Response is not available in this context的解决方法

    HttpUtility.HtmlEncode HttpUtility.HtmlDecode HttpUtility.UrlEncode HttpUtility.UrlDecode 也会出现此异常. 这 ...

  4. ABP .Net Core 调用异步方法抛异常A second operation started on this context before a previous asynchronous operation completed

    1.  问题描述 最近使用ABP .Net Core框架做一个微信开发,同时采用了一个微信开发框架集成到ABP,在微信用户关注的推送事件里调用了一个async 方法,由于没有返回值,也没做任何处理,本 ...

  5. Linux Kernel Oops异常分析

    1.PowerPC小系统内核异常分析 1.1  异常打印 Unable to handle kernel paging request for data at address 0x36fef31eFa ...

  6. .NET异常问题总结

    输入字符串的格式不正确. 有格式化字符替换符号{0}等,“{”和“}”作为特殊符号出现,如果有多余的“{”和“}”就会出错 GZIP压缩出现FF-F0-F1…是无效的输入流 要解压的字节流无效,可能是 ...

  7. Android程序Crash时的异常上报

    转载请注明来源:http://blog.csdn.net/singwhatiwanna/article/details/17289479 前言 大家都知道,android应用不可避免的会发生crash ...

  8. response.sendRedirect 报 java.lang.IllegalStateException 异常的解决思路

    今天在进行代码开发的时候,出现了 java.lang.IllegalStateException异常,response.sendRedirect("./DEFAULT.html") ...

  9. HIbernate常见异常(转载)

    SSH阶段常见的异常系列之一hibernate(15条) 异常一 异常一 异常描述: Sax解析异常:cvc-复杂的类型,发现了以元素maping开头的无效内容,应该是以 ‘{“http://www. ...

随机推荐

  1. testNG 常用的注解

    常用注解介绍: @BeforeSuite 在该套件的所有测试都运行在注释的方法之前,仅运行一次 @AftereSuite 在该套件的所有测试都运行在注释方法之后,仅运行一次 @BeforeClass  ...

  2. 尝试用python开发一款图片压缩工具1:尝试 pillow库

    开发目的 我经常使用图片.公众号文章发文也好,还是生活中要使用素材.图片是一种比文字更加直观的载体.但是图片更加占用带宽,很多软件都对图片有大小限制.图片太大也会影响加载速度.我试过几款图片压缩工具, ...

  3. 微信小程序画布(1)

    wxml: <view  catchtouchmove="preventTouchMove" wx:if="{{canvas_haoBao}}"> ...

  4. 随笔之——浮动(float)的影响及其清除、、clear与overflow-hidden清除float的不同!!!

    一.浮动(float)对内联元素的影响. 1.我们都知道,内联元素(例如:span/img/input...)在正常情况下不可以对其设置宽高的,它的大小之只和它内部内容的多少有关. 我们怎样才可以对其 ...

  5. 2019-2020-1 20199308《Linux内核原理与分析》第六周作业

    <Linux内核分析> 第五章 系统调用的三层机制(下) 5.1 给MenuOS增加命令 强制删除当前menu目录,用get clone重新克隆一个新版本的menu,运行make root ...

  6. (第三篇)SSH网络协议概述

    ssh简介原理 windows远程登录,QQ远程协助修电脑 简单说,SSH是一种网络协议,用于计算机之间的加密登录.最早的时候,互联网通信都是明文通信,一旦被截获,内容就暴露无疑.1995年,芬兰学者 ...

  7. JDK13的六大重要新特性

    文章目录 JDK13的六大重要特性 支持Unicode 12.1 动态CDS归档(Dynamic CDS Archiving) java.net.Socket和java.net.ServerSocke ...

  8. JavaScript 后台获取数据 - HTTP203 Advent(中文字幕)

    如果关注过 Google 相关的开发技术,对 HTTP203 这个栏目应该不陌生. 这是 HTTP203 圣诞节的特别版! Jake(@jaffathecake)和 Surma(@DasSurma)有 ...

  9. 实战-MySQL定时增量备份(2)

    概要 引言 增量备份 恢复增量备份 定时备份 引言 在产品上线之后,我们的数据是相当重要的,容不得半点闪失,应该做好万全的准备,搞不好哪一天被黑客入侵或者恶意删除,那就 gg 了.所以要对我们的线上数 ...

  10. Fourier Transform

    为了在统一框架里分析周期信号与非周期信号,可以给周期信号也建立傅里叶变换. 有两种方法求周期信号的傅里叶变换: **1. 利用傅里叶级数进行构造 ** 对于周期信号\(x(t)\),其傅里叶级数展开式 ...