Search code examples
javaspringmultithreadingexecutor

Spring TaskExecutor Implementation


The application I am working on receives notifications from external systems, which I want to process sequentially, since I am experiencing some deadlocks.

I am using the TaskExecutor from Spring which is the equivalent of the JDK 1.5's Executor.

I've implemented it in the following way:

I've a java interface containing 1 method:

    public interface AsynchronousService {
    void executeAsynchronously(Runnable task);
}

and the corresponding implementation:

    public class AsynchronousServiceImpl implements AsynchronousService {

    private TaskExecutor taskExecutor;

    @Override
    public void executeAsynchronously(Runnable task) {
        taskExecutor.execute(task);
    }

    @Required
    public void setTaskExecutor(TaskExecutor taskExecutor) {
        this.taskExecutor = taskExecutor;
    }
}

Here is the configuration of the TaskExecutor. I am not sure about this configuration. Since, I want the notifications to execute sequentially, I set 1 for both, corePoolSize and maxPoolSize. This means that only 1 thread will be created in the threadpool and retrieves the notifications sequentially from the queue. I also set "false" for "WaitForTasksToCompleteOnShutdown" in order not to shutdown after each task is executed, but rather when the spring context is destroyed. Am I generally correct with my assumptions?

<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="1"/>
    <property name="maxPoolSize" value="1"/>
    <property name="WaitForTasksToCompleteOnShutdown" value="false"/>
</bean>

Here I execute then the code:

 asynchronousService.executeAsynchronously(new Runnable() {
     @Override
     public void run() {
         someMethod.processNotification(notification)
      }
   });

What do you think of my implementation? Did I miss something? I am not sure if/where I need to implement some error-handling?

EDIT: Is there any possibilty to tweak the implementation of the task-executor in spring to use a custom queue? Or how difficult is it to prioritize the tasks in the queue? I looked at some implementations but most of them implement the executor-service from sratch without using Spring.


Solution

  • The application I am working on receives notifications from external systems, which I want to process sequentially, since I am experiencing some deadlocks.

    If you process sequentially, you don't need thread pool anyway.

    Since, I want the notifications to execute sequentially, I set 1 for both, corePoolSize and maxPoolSize

    This will create a fixed pool size of 1. There's only one thread so it will execute sequentially

    I also set "false" for "WaitForTasksToCompleteOnShutdown" in order not to shutdown after each task is executed, but rather when the spring context is destroyed

    This is wrong. This flag tells the pool to wait for task completion on shutdown (Eg. If you call pool.shutdownNow(), the pool will wait for any executing threads)

    And to summarize: Don't use thread pool if you want to execute one by one serially without concurrent