[英]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.