Java执行服务

时间:2015-04-16 15:11:48

标签: java

有时候我会得到当前的例外:

[java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.ExecutorCompletionService$QueueingFuture@746c196 rejected from java.util.concurrent.ThreadPoolExecutor@5eeac923[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]]

我正在调用runAndAwaitResult()函数将其限制为100毫秒。如果结果已成功收到并准备就绪,如果超出超时,我将关闭ExecuorService,我将获取可用结果并关闭服务。但有时我会得到那个例外。我应该如何处理它,或者什么是终止服务以避免这种异常的最佳方式。注意我不能等待温和关机我两个工作只有100毫秒。

try{
        generalPersonalisationHandler = new GeneralPersonalisationHandler(resourceStack);
        com.company.personalisation.jobs.result.GeneralPersonalisationResult result = timeLimiter.callWithTimeout(new Callable<com.company.personalisation.jobs.result.GeneralPersonalisationResult>() {
            @Override
            public com.company.personalisation.jobs.result.GeneralPersonalisationResult call() throws Exception {
                com.company.personalisation.jobs.result.GeneralPersonalisationResult result = generalPersonalisationHandler.runAndAwaitResults(customerCookie);
                generalPersonalisationHandler.shutdown();
                return result;
            }
        }, timeout, TimeUnit.MILLISECONDS, true);

        PersonalisationData response = transformAvailableResults(result);
        return  response;
    }
    catch (UncheckedTimeoutException e) {
        String errorMsg = String.format("TIMEOUT for RunGeneralPersonalisation() execution time has exceeded maximum limit of: [%s] ms", timeout);
        ErrorLogger.error(errorMsg, e);
        com.company.personalisation.jobs.result.GeneralPersonalisationResult result = generalPersonalisationHandler.returnAvailableResults();
        generalPersonalisationHandler.shutdown();
        return transformAvailableResults(result);
    }  

GeneralPersonalisationHandler.java

public class GeneralPersonalisationHandler {

    private GeneralPersonalisationResult generalPersonalisationResult;

    private final int SUBMITTED_JOBS = 2;
    private CompletionService<JobResult> completionService;
    private ResourceStack resourceStack;
    private ExecutorService executor;
    public GeneralPersonalisationHandler(ResourceStack resourceStack){
        this.executor = Executors.newFixedThreadPool(SUBMITTED_JOBS);
        this.completionService = new ExecutorCompletionService<JobResult>(executor);
        this.resourceStack = resourceStack;
    }

    public GeneralPersonalisationResult runAndAwaitResults(String customerCookie) throws Exception {



            Job customerEventsJob = new CustomerEventsJob(customerCookie, resourceStack);
            Job customerInfoJob = new CustomerPersonalInfoJob(customerCookie, resourceStack);

            completionService.submit(customerInfoJob);
            completionService.submit(customerEventsJob);


            generalPersonalisationResult = new GeneralPersonalisationResult();
            for (int handledJobs = 0; handledJobs < SUBMITTED_JOBS; handledJobs++) {
                Future<JobResult> result = completionService.take();

                JobResult jobResult = result.get();
                if (jobResult instanceof CustomerPersonalInfoJobResult) {
                    CustomerPersonalInfoJobResult customerPersonalInfoJobResult = (CustomerPersonalInfoJobResult) jobResult;
                    generalPersonalisationResult.setCustomerPersonalInfoJobResult(customerPersonalInfoJobResult);
                }

                if (jobResult instanceof CustomerEventsJobResult) {
                    CustomerEventsJobResult customerEventsJobResult = (CustomerEventsJobResult) jobResult;
                    generalPersonalisationResult.setCustomerEventsJobResult(customerEventsJobResult);
                }
            }

        }

        return generalPersonalisationResult;
    }

    public GeneralPersonalisationResult returnAvailableResults(){
        return this.generalPersonalisationResult;
    }

    public void shutdown(){
        if(!this.executor.isShutdown()) {
            this.executor.shutdown();
        }
    }
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

catch (UncheckedTimeoutException e) {
    String errorMsg = String.format("TIMEOUT for RunGeneralPersonalisation() execution time has exceeded maximum limit of: [%s] ms", timeout);
    ErrorLogger.error(errorMsg, e);
    com.thehutgroup.personalisation.jobs.result.GeneralPersonalisationResult result = generalPersonalisationHandler.returnAvailableResults();
    generalPersonalisationHandler.shutdown();  // possible problem !!!
    return transformAvailableResults(result);
}

看起来如果关闭执行程序服务会发生UncheckedTimeoutException,之后如果您尝试添加某个任务,则会出现RejectedExecutionException。

仅当您确定不会添加任何任务时才调用关闭。