Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What could be the cause of RejectedExecutionException

I am getting this exception on my tomcat server (+liferay)

java.util.concurrent.RejectedExecutionException 

my class is like that :

public class SingleExecutor extends ThreadPoolExecutor {   public SingleExecutor(){     super(1, 1,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());   }    @Override   public void execute(Runnable command) {     if(command instanceof AccessLogInsert){         AccessLogInsert ali = (AccessLogInsert)command;         ali.setConn(conn);         ali.setPs(ps);     }     super.execute(command);   } } 

I get this exception on the line super.execute(command); This error can occur when the queue is full but the LinkedBlockingQueue size is 2^31, and I am sure that there is no so many command waiting.

At start everything is stable, but after I redeploy a war it starts occuring. This class is not part of the war but in a jar in tomcat/lib.

Do you have any idea why this happend and how to fix it ?

like image 435
jpprade Avatar asked Nov 18 '11 13:11

jpprade


People also ask

How do you handle RejectedExecutionException?

If you have constrained your thread pool to only allow a certain number of concurrent threads (generally a good thing), then the application needs to somehow push-back on the calling code, so when you receive a RejectedExecutionException from the ThreadPoolExecutor you need to indicate this to the caller and the caller ...

What is Java util concurrent RejectedExecutionException?

RejectedExecutionException: rejected from java. util. concurrent. ThreadPoolExecutor error occurs when the thread pool queue is full and no further threads can be created in spring boot.

What is RejectedExecutionHandler?

Interface RejectedExecutionHandlerA handler for tasks that cannot be executed by a ThreadPoolExecutor .

What is ScheduledThreadPoolExecutor?

ScheduledThreadPoolExecutor class in Java is a subclass of ThreadPoolExecutor class defined in java. util. concurrent package. As it is clear from its name that this class is useful when we want to schedule tasks to run repeatedly or to run after a given delay for some future time. It creates a fixed-sized Thread Pool.


1 Answers

From ThreadPoolExecutor JavaDoc (emphasis mine)

New tasks submitted in method execute(java.lang.Runnable) will be rejected when the Executor has been shut down, and also when the Executor uses finite bounds for both maximum threads and work queue capacity, and is saturated. In either case, the execute method invokes the RejectedExecutionHandler.rejectedExecution(java.lang.Runnable, java.util.concurrent.ThreadPoolExecutor) method of its RejectedExecutionHandler. Four predefined handler policies are provided:

  1. In the default ThreadPoolExecutor.AbortPolicy, the handler throws a runtime RejectedExecutionException upon rejection.
  2. In ThreadPoolExecutor.CallerRunsPolicy, the thread that invokes execute itself runs the task. This provides a simple feedback control mechanism that will slow down the rate that new tasks are submitted.
  3. In ThreadPoolExecutor.DiscardPolicy, a task that cannot be executed is simply dropped.
  4. In ThreadPoolExecutor.DiscardOldestPolicy, if the executor is not shut down, the task at the head of the work queue is dropped, and then execution is retried (which can fail again, causing this to be repeated.)

It is possible to define and use other kinds of RejectedExecutionHandler classes. Doing so requires some care especially when policies are designed to work only under particular capacity or queuing policies.

Presumably therefore, reloading the war triggers a shutdown of the Executor. Try putting the relevant libraries in the war, so that Tomcat's ClassLoader has a better chance of correctly reloading your app.

like image 191
OrangeDog Avatar answered Oct 13 '22 02:10

OrangeDog