简体   繁体   English

从Spring Batch分区步骤的Writer启动Runnable

[英]Launching a Runnable from Writer of Spring Batch partitioned step

I have a Spring batch job consisting of a partitioned step and partitioned step is doing processing in chunks. 我有一个Spring批处理作业,该工作由一个分区步骤组成,并且分区步骤正在按块进行处理。

Can I further launch new threads ( implementing Runnable ) from method, public void write(List<? extends VO> itemsToWrite) ? 我可以从方法public void write(List<? extends VO> itemsToWrite)进一步启动新线程(实现Runnable public void write(List<? extends VO> itemsToWrite)吗?

Basically, writer here writes indices using Lucene and since writer has a List of chunk-size items, I thought to divide that List into segments and pass each segment to a new Runnable . 基本上,这里的作家使用Lucene编写索引,并且由于作家拥有一个chunk-size项的List ,我想将List分成多个段,然后将每个段传递给一个新的Runnable

Is that a good approach? 那是一个好方法吗?

I coded a sample and it works most of the times but gets stuck few times. 我编码了一个示例,它在大多数情况下都有效,但卡住了几次。

Are there any thing that I need to worry about? 我有什么需要担心的吗? OR is there something inbuilt in spring batch to achieve this? 还是在Spring Batch中内置一些东西来实现这一目标?

I don't want write to happen by a single thread for whole chunk. 我不希望整个线程都由单个线程进行写操作。 I wish to further divide up chunk. 我希望进一步细分。

Lucene IndexWriter is thread safe and a approach is listed here Lucene IndexWriter是线程安全的, 此处列出一种方法

Sample Code - Writer gets a List of items for which I open threads from thread pool? 示例代码-Writer获取我从线程池中打开线程的项目的List Will there be any concern even if I wait for pool to terminate for a chunk, 即使我等待池终止一个块,也会有任何问题吗,

@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()){
            ;
        }

I'm not sure that launching new Threads in writer it's the good idea. 我不确定在writer中启动新线程是个好主意。 These threads are out of scope of spring batch framework, so you will need to implement shutdown and cancellation policy for above. 这些线程不在Spring Batch框架的范围内,因此您将需要实现上述的关闭和取消策略。 If processing of one segments will fail it can lead to fail entire queue. 如果对一个段的处理将失败,则可能导致整个队列失败。

As alternate approach I can suggest to promote your custom segments of list from writer to next step as described in official docs passingDataToFutureSteps 作为一种替代方法,我可以建议将您的列表的自定义细分从作家提升到下一步,如官方文档通过DataToFutureSteps中所述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM