队列已满时的Spring线程和TaskRejectException

时间:2016-05-26 10:54:44

标签: java spring multithreading asynchronous

我使用Spring创建Web应用程序,其中多个用户可以上传文件,而异步任务我转换此文件。 现在有时我收到这个异常并且tomcat出现故障:

Caused by: org.springframework.core.task.TaskRejectedException: Executor [java.util.concurrent.ThreadPoolExecutor@54a611af[Running, pool size = 10, active threads = 10, queued tasks = 10, completed tasks = 230]] did not accept task: org.springframework.aop.interceptor.AsyncExecutionInterceptor$1@598b845a
    at org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor.submit(ThreadPoolTaskExecutor.java:284)
    at org.springframework.aop.interceptor.AsyncExecutionAspectSupport.doSubmit(AsyncExecutionAspectSupport.java:189)
    at org.springframework.aop.interceptor.AsyncExecutionInterceptor.invoke(AsyncExecutionInterceptor.java:123)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
    at com.model.ZipAndMat$$EnhancerBySpringCGLIB$$5bcfcf5.createZipAndMat(<generated>)
    at com.services.FleetAcquisitionServicesImpl.uploadFiles(FleetAcquisitionServicesImpl.java:95)
    ... 81 more
Caused by: java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.FutureTask@5a47368c rejected from java.util.concurrent.ThreadPoolExecutor@54a611af[Running, pool size = 10, active threads = 10, queued tasks = 10, completed tasks = 230]
    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 java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
    at org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor.submit(ThreadPoolTaskExecutor.java:281)
    ... 87 more

我该如何处理这个问题?我想增加队列大小,当它满了时,其他任务应该等到队列有空闲插槽。 感谢

1 个答案:

答案 0 :(得分:0)

如果任务大小超过队列大小,则不得使用taskexecutor。改变你的方法。 例如,将所有请求写入永久存储(数据库或文件等),并从存储创建作业读取并以顺序处理的方式执行。