package froala.editor.service; import java.util.List; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import org.apache.pdfbox.rendering.PDFRenderer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import froala.editor.utils.FileUtil; /** * 다중 쓰레드 파일 업로드 * * @since 2020. 8. 29. * @author 임종호 * *
 * -----------------------
 * 개정이력
 * 2020. 8. 29. 임종호 : 최초작성
 *         
*/ public class ParallelExcutorService { private static final Logger LOGGER = LoggerFactory.getLogger(ParallelExcutorService.class); // private final int maxCore = Runtime.getRuntime().availableProcessors(); // private final ExecutorService executor = Executors.newFixedThreadPool(maxCore); private final ExecutorService executor = Executors.newCachedThreadPool(); private final BlockingQueue queue = new ArrayBlockingQueue<>(10); public ParallelExcutorService() { } /** * start : 시작번호, repeat : 반복횟수, max : pdf파일 최대번호, pdfRenderer : PDFBox 객체, * returnFileRoute : 이미지 파일저장경로 * * @Author : 임종호 * @Date : 2020. 8. 29. * @Method Name : submit * @return : void */ public void pdfSubmit(String job, int start, int repeat, int max, PDFRenderer pdfRenderer, String returnFileRoute) { executor.submit(() -> { for (int i = start; i < repeat * 3 + start;) { int fileIndex = i + 1; if (max < fileIndex) { break; } String imgFileName = returnFileRoute + '_' + fileIndex + ".png"; java.io.File existCheck2 = new java.io.File(imgFileName); if (!existCheck2.exists()) { // 순회하며 이미지로 변환 처리 FileUtil.pdfImageWrite(pdfRenderer, i, imgFileName); } i += repeat; } String threadName = Thread.currentThread().getName(); String result = job + ", " + threadName; try { queue.put(result); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } }); } public String take() { try { return queue.take(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IllegalStateException(e); } } public void close() { List unfinishedTasks = executor.shutdownNow(); if (!unfinishedTasks.isEmpty()) { LOGGER.error("Not all tasks finished before calling close: " + unfinishedTasks.size()); } } }