ThreadPoolTaskExecutor Configuration with Spring Boot

Posted By : Vishal Kumar | 26-May-2018

We will investigate designing SimpleAsyncTaskExecutor, ConcurrentTaskExecutor, ThreadPoolExecutor in a spring venture. Aside from this, we will be likewise investigating how real technique return compose can be wrapped in a Future question while managing async conduct in spring.

 

Method Level ThreadPoolTaskExecutor and ConcurrentTaskExecutor

As a matter of course, spring utilizes SimpleAsyncTaskExecutor to run techniques explained with @Async. We can likewise characterize our customs agent bean as taking after and utilize it at technique level.

 

ThreadPoolTaskExecutor

@Bean(name = "threadPoolExecutor")
public Executor getAsyncExecutor() {
	ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
	executor.setCorePoolSize(7);
	executor.setMaxPoolSize(42);
	executor.setQueueCapacity(11);
	executor.setThreadNamePrefix("threadPoolExecutor-");
	executor.initialize();
	return executor;
}

ConcurrentTaskExecutor

@Bean(name = "ConcurrentTaskExecutor")
public TaskExecutor taskExecutor2 () {
	return new ConcurrentTaskExecutor(
			Executors.newFixedThreadPool(3));
}

In following ways these beans can be used at the method level

@Override
@Async("threadPoolExecutor")
public void threadPoolExecutorCreateUser(){
	System.out.println("Currently Executing thread name - " + Thread.currentThread().getName());
	System.out.println("User created with thread pool executor");
}

@Override
@Async("ConcurrentTaskExecutor")
public void concurrentExecutorCreateUser(){
	System.out.println("Currently Executing thread name - " + Thread.currentThread().getName());
	System.out.println("User created with concurrent task executor");
}

SimpleAsyncTaskExecutor makes sense in cases, on the off chance that you need to execute some long-term executing errands, e.g. in the event that you need to pack log documents toward the finish of a day. In different cases, on the off chance that you need to execute a brief span executing assignment each n seconds or minutes, you should utilize the ThreadPoolTaskExecutor, due to reusing of framework assets.

 

Application Level Executor

To actualize agent at an application level, we require to execute AsyncConfigurer and supersede following strategies.

@Configuration
public class AsyncConfig implements AsyncConfigurer {

    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(7);
        executor.setMaxPoolSize(42);
        executor.setQueueCapacity(11);
        executor.setThreadNamePrefix("MyExecutor-");
        executor.initialize();
        return executor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return new AsyncExceptionHandler();
    }
}

An exception handler is following.

 

AsyncExceptionHandler.class

public class AsyncExceptionHandler implements AsyncUncaughtExceptionHandler {

    @Override
    public void handleUncaughtException(Throwable throwable, Method method, Object... obj) {

        System.out.println("Exception Cause - " + throwable.getMessage());
        System.out.println("Method name - " + method.getName());
        for (Object param : obj) {
            System.out.println("Parameter value - " + param);
        }
    }
}

Conclusion

I hope this article served you that you were searching for. In the event that you have anything that you need to include or share at that point please share it underneath in the remark area. My previous article was on async configuration with spring.

Thanks.

About Author

Author Image
Vishal Kumar

Vishal Kumar is Master in Computers Application. He has good technical skills in Java and always motivated to learn new things.

Request for Proposal

Name is required

Comment is required

Sending message..