xed*_*edo 15 java database spring spring-batch
如何从多个数据库中读取项目?我已经知道可以从文件中获取.
以下示例适用于从多个文件中读取
...
<job id="readMultiFileJob" xmlns="http://www.springframework.org/schema/batch">
<step id="step1">
<tasklet>
<chunk reader="multiResourceReader" writer="flatFileItemWriter"
commit-interval="1" />
</tasklet>
</step>
</job>
...
<bean id="multiResourceReader"
class=" org.springframework.batch.item.file.MultiResourceItemReader">
<property name="resources" value="file:csv/inputs/domain-*.csv" />
<property name="delegate" ref="flatFileItemReader" />
</bean>
...
Run Code Online (Sandbox Code Playgroud)
像这样的三个豆子.
<bean id="database2" class="org.springframework.batch.item.database.JdbcCursorItemReader">
<property name="name" value="database2Reader" />
<property name="dataSource" ref="dataSource2" />
<property name="sql" value="select image from object where image like '%/images/%'" />
<property name="rowMapper">
<bean class="sym.batch.ImagesRowMapper2" />
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
Luc*_*cci 14
没有一个随时可用的组件可以满足您的要求; 唯一的解决方案是编写一个ItemReader<>委托给JdbcCursorItemReader(或HibernateCursorItemReader任何通用ItemReader实现)的自定义.
您需要准备所有必要的东西(数据源,会话,真正的数据库阅读器)并将所有委派的读者绑定到您的自定义阅读器.
编辑:您需要使用recusion ItemReader.read()和mantain reader 来模拟循环,并在作业重新启动时委托状态.
class MyItemReader<T> implements ItemReader<T>, ItemStream {
private ItemReader[] delegates;
private int delegateIndex;
private ItemReader<T> currentDelegate;
private ExecutionContext stepExecutionContext;
public void setDelegates(ItemReader[] delegates) {
this.delegates = delegates;
}
@BeforeStep
private void beforeStep(StepExecution stepExecution) {
this.stepExecutionContext = stepExecution.getExecutionContext();
}
public T read() {
T item = null;
if(null != currentDelegate) {
item = currentDelegate.read();
if(null == item) {
((ItemStream)this.currentDelegate).close();
this.currentDelegate = null;
}
}
// Move to next delegate if previous was exhausted!
if(null == item && this.delegateIndex< this.delegates.length) {
this.currentDelegate = this.delegates[this.currentIndex++];
((ItemStream)this.currentDelegate).open(this.stepExecutionContext);
update(this.stepExecutionContext);
// Recurse to read() to simulate loop through delegates
item = read();
}
return item;
}
public void open(ExecutionContext ctx) {
// During open restore last active reader and restore its state
if(ctx.containsKey("index")) {
this.delegateIndex = ctx.getInt("index");
this.currentDelegate = this.delegates[this.delegateIndex];
((ItemStream)this.currentDelegate ).open(ctx);
}
}
public void update(ExecutionContext ctx) {
// Update current delegate index and state
ctx.putInt("index", this.delegateIndex);
if(null != this.currentDelegate) {
((ItemStream)this.currentDelegate).update(ctx);
}
}
public void close(ExecutionContext ctx) {
if(null != this.currentDelegate) {
((ItemStream)this.currentDelegate).close();
}
}
Run Code Online (Sandbox Code Playgroud)
<bean id="myItemReader" class=path.to.MyItemReader>
<property name="delegates">
<array>
<ref bean="itemReader1"/>
<ref bean="itemReader2"/>
<ref bean="itemReader3"/>
</array>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
EDIT2:记得设置属性名称 ; 这是必须让MyItemReader.read()正常工作
<bean id="itemReader1" class="JdbcCursorItemReader">
<property name="name" value="itemReader1" />
<!-- Set other properties -->
</bean>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15187 次 |
| 最近记录: |