Java 8执行器使用ExecutorService读取`100k` filePath时抛出RejectedExecutionException

时间:2018-07-22 09:10:18

标签: java elasticsearch java-8 executorservice elastic-stack

我正在从ElasticSearch索引中获取一个100k文件路径,并且实际文件在本地驱动器中可用。基于文件路径,我必须将这些文件转换为base64。

Am使用100k获取scrollApi文件路径并添加到Arraylist中。收集完所有文件路径后,我想将这些文件转换为base64。

为此,我想为此进程创建一个工作线程(以加快进程)。

请在下面找到我的代码。最初,我从弹性搜索索引中读取文件路径,并将此文件路径传递给我的工作线程以读取该路径(尚未完全实现以转换为base64)。

public class DocumentIndex {

    private final static String INDEX = "documents_local";  
    private final static String ATTACHMENT = "document_suggestion";
    private final static String TYPE = "doc";
    private static final Logger logger = Logger.getLogger(Thread.currentThread().getStackTrace()[0].getClassName());
    private static final int BUFFER_SIZE = 3 * 1024;

     public static void main(String[] args) throws IOException {
            ExecutorService executor = Executors.newFixedThreadPool(5);
            List<String> filePathList = new ArrayList<String>();


            RestHighLevelClient restHighLevelClient = null;
            RestHighLevelClient restHighLevelClient2 = null;
            Document doc=new Document();

            logger.info("Started Indexing the Document.....");


            //Fetching Id, FilePath & FileName from Document Index. 
            SearchRequest searchRequest = new SearchRequest(INDEX); 
            searchRequest.types(TYPE);
            final Scroll scroll = new Scroll(TimeValue.timeValueMinutes(60L)); //part of Scroll API

            searchRequest.scroll(scroll); //part of Scroll API
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            QueryBuilder qb = QueryBuilders.matchAllQuery();

            searchSourceBuilder.query(qb);
            searchRequest.source(searchSourceBuilder);

            SearchResponse searchResponse = SearchEngineClient.getInstance3().search(searchRequest);
            String scrollId = searchResponse.getScrollId(); //part of Scroll API
            SearchHit[] searchHits = searchResponse.getHits().getHits();
            long totalHits=searchResponse.getHits().totalHits;
            logger.info("Total Hits --->"+totalHits);

            //part of Scroll API -- Starts
            while (searchHits != null && searchHits.length > 0) { 
                SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId); 
                scrollRequest.scroll(scroll);
                searchResponse = SearchEngineClient.getInstance3().searchScroll(scrollRequest);
                scrollId = searchResponse.getScrollId();
                searchHits = searchResponse.getHits().getHits();

                Map<String, Object> jsonMap ;
                for (SearchHit hit : searchHits) {

                    StringBuilder result = null;
                    String encodedfile = null;
                    File file=null;

                    Map<String, Object> sourceAsMap = hit.getSourceAsMap();


                    if(sourceAsMap != null) {  
                        doc.setId((int) sourceAsMap.get("id"));
                        doc.setApp_language(String.valueOf(sourceAsMap.get("app_language")));
                        doc.setFilename(String.valueOf(sourceAsMap.get("filename")));
                        doc.setPath(String.valueOf(sourceAsMap.get("path")));
                    }
                    if(doc.getPath()!= null && doc.getFilename() != null) {
                    filePathList.add(doc.getPath().concat(doc.getFilename()));
                    }
                }

                logger.info("File Path List size --->"+filePathList.size());


                    for (int i = 0; i < filePathList.size(); i++) {
                        Runnable worker = new WorkerThread(filePathList.get(i));
                        executor.execute(worker);
                      }
                    executor.shutdown();
                    while (!executor.isTerminated()) {
                    }
                    System.out.println("Finished all threads");
                        }
            }
}


public class WorkerThread implements Runnable {

 private String command;
 private static final Logger logger = Logger.getLogger(Thread.currentThread().getStackTrace()[0].getClassName());

    public WorkerThread(String s){
        this.command=s;
    }

    @Override
    public void run() {

        logger.info("File Path --->"+command);
        System.out.println(Thread.currentThread().getName()+" Start. Command = "+command);
        processCommand();
        System.out.println(Thread.currentThread().getName()+" End.");
    }

    private void processCommand() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String toString(){
        return this.command;
    }

}

从索引读取文件路径时出现以下错误。

Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task D:\data\Files\doc753_v1_fr-FR.pdf rejected from java.util.concurrent.ThreadPoolExecutor@35dab4eb[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 10]
    at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)
    at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
    at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
    at com.jci.vb2.SearchEngineUtility.DocumentIndex.main(DocumentIndex.java:104)

1 个答案:

答案 0 :(得分:2)

这是由于您在shutdown外部声明/创建了执行服务ExecutorService的对象而在ExecutorService executor = Executors.newFixedThreadPool(5);上调用while后试图提交任务循环while (searchHits != null && searchHits.length > 0) {。在while循环的第二次迭代中,它将尝试将任务提交到您在第一次迭代中已调用shutdown的executor service上。

要解决该问题,请在while循环内声明ExecutorService executor = Executors.newFixedThreadPool(5);或在shutdown的末尾声明while loop