[英]Launching a Runnable from Writer of Spring Batch partitioned step
我有一個Spring批處理作業,該工作由一個分區步驟組成,並且分區步驟正在按塊進行處理。
我可以從方法public void write(List<? extends VO> itemsToWrite)
進一步啟動新線程(實現Runnable
public void write(List<? extends VO> itemsToWrite)
嗎?
基本上,這里的作家使用Lucene編寫索引,並且由於作家擁有一個chunk-size
項的List
,我想將List
分成多個段,然后將每個段傳遞給一個新的Runnable
。
那是一個好方法嗎?
我編碼了一個示例,它在大多數情況下都有效,但卡住了幾次。
我有什么需要擔心的嗎? 還是在Spring Batch中內置一些東西來實現這一目標?
我不希望整個線程都由單個線程進行寫操作。 我希望進一步細分。
Lucene IndexWriter
是線程安全的, 此處列出了一種方法
示例代碼-Writer獲取我從線程池中打開線程的項目的List
? 即使我等待池終止一個塊,也會有任何問題嗎,
@Override
public void write(List<? extends IndexerInputVO> inputItems) throws Exception {
int docsPerThread = Constants.NUMBER_OF_DOCS_PER_INDEX_WRITER_THREADS;
int docSize = inputItems.size();
int remainder = docSize%docsPerThread;
int poolSize = docSize/docsPerThread;
ExecutorService executor = Executors.newFixedThreadPool(poolSize+1);
int fromIndex=0;
int toIndex = docsPerThread;
if(docSize < docsPerThread){
executor.submit(new IndexWriterRunnable(this.luceneObjects,service,inputItems));
}else{
for(int i=1;i<=poolSize;i++){
executor.submit(new IndexWriterRunnable(this.luceneObjects,service,inputItems.subList(fromIndex, toIndex)));
fromIndex+=docsPerThread;
toIndex+=docsPerThread;
}
if(remainder != 0){
toIndex=docSize;
executor.submit(new IndexWriterRunnable(this.luceneObjects,service,inputItems.subList(fromIndex, toIndex)));
}
}
executor.shutdown();
while(executor.isTerminated()){
;
}
我不確定在writer中啟動新線程是個好主意。 這些線程不在Spring Batch框架的范圍內,因此您將需要實現上述的關閉和取消策略。 如果對一個段的處理將失敗,則可能導致整個隊列失敗。
作為一種替代方法,我可以建議將您的列表的自定義細分從作家提升到下一步,如官方文檔通過DataToFutureSteps中所述
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.