建議:
1)請閱讀使用JdbcCursorItemReader數據。
所有開箱即用的組件都是不錯的選擇,因爲它們已經實現了使您的步驟可重新啓動的ItemStream接口。但就像你提到的那樣,有時候,請求只是爲了複雜,或者像我一樣,你已經有了一個可以重用的服務或DAO。
我建議你使用ItemReaderAdapter。它讓你配置一個委託服務來調用來獲取你的數據。
<bean id="MyReader" class="xxx.adapters.MyItemReaderAdapter">
<property name="targetObject" ref="AnExistingDao" />
<property name="targetMethod" value="next" />
</bean>
注意,targetMethod必須尊重ItemReaders的讀合同(當沒有更多的數據返回NULL)
如果你的工作並不需要被重新啓動,你可以簡單地使用類:組織。 springframework.batch.item.adapter.ItemReaderAdapter
但是如果你需要你的工作是重新啓動,你可以這樣創建自己的ItemReaderAdapter:
public class MyItemReaderAdapter<T> extends AbstractMethodInvokingDelegator<T> implements ItemReader<T>, ItemStream {
private long currentCount = 0;
private final String CONTEXT_COUNT_KEY = "count";
/**
* @return return value of the target method.
*/
public T read() throws Exception {
super.setArguments(new Long[]{currentCount++});
return invokeDelegateMethod();
}
@Override
public void open(ExecutionContext executionContext)
throws ItemStreamException {
currentCount = executionContext.getLong(CONTEXT_COUNT_KEY,0);
}
@Override
public void update(ExecutionContext executionContext) throws ItemStreamException {
executionContext.putLong(CONTEXT_COUNT_KEY, currentCount);
log.info("Update Stream current count : " + currentCount);
}
@Override
public void close() throws ItemStreamException {
// TODO Auto-generated method stub
}
}
由於開箱即用的itemReaderAdapter不可重新啓動,您只需創建自己的實現ItemStream的步驟
2)關於7步vs 1步。
我會用compositeProcessor在這一步上1步。 7步選項只會帶來IMO的問題。
1)7步databean:所以你的作家提交數據庫直到第7步..然後第7步作家試圖承諾真正的數據庫和繁榮錯誤!全部丟失,並且批次必須從步驟1重新開始!
2)使用上下文的7個步驟:可能會更好,因爲您將在spring批處理元數據中保存狀態..但在springBatch的元數據中存儲大數據並不是一個好習慣!
3)是國際海事組織的方式。 ;-)
感謝您的回覆。 – Nandan
在管理方面,哪種方式最適合?獨立的步驟或一步與compositeItemProcessor? – Nandan
單獨的步驟會在故障情況下重新啓動時引入複雜性。使用CompositeItemProcessor的單個步驟意味着所有的處理器在出現故障時都會回滾。 –