5 Haziran 2023 Pazartesi

SpringAsync AsyncConfigurer Arayüzü - Custom Thread Pool Yaratır

Giriş
Açıklaması şöyle. 
AsyncConfigurer is an Interface provided by Spring, It provides two methods. One is if you want to override the TaskExecutor(Threadpool), another is an Exception handler where you can inject your exception handler so it can catch the uncaught exceptions. 
getAsyncExecutor metodu
Örnek
Şöyle yaparız.
@Configuration
@EnableAsync
public class ServiceExecutorConfig implements AsyncConfigurer {

  @Override
  public Executor getAsyncExecutor() {
    ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    taskExecutor.setCorePoolSize(30);
    taskExecutor.setMaxPoolSize(40);
    taskExecutor.setQueueCapacity(10);
    taskExecutor.initialize();
    return taskExecutor;
  }
}
Örnek
Şöyle yaparız
@Slf4j
@Configuration
public class BaseAsyncConfigurer implements AsyncConfigurer {

  //replace default Async Executor with Customize One.
  @Override
  public Executor getAsyncExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(Runtime.getRuntime().availableProcessors());
    executor.setMaxPoolSize(Runtime.getRuntime().availableProcessors()*5);
    executor.setQueueCapacity(Runtime.getRuntime().availableProcessors()*10);
    executor.setThreadNamePrefix("replacedAsync-");
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    executor.initialize();
    return executor;
  }
  ...
}
getAsyncUncaughtExceptionHandler metodu
@Async olarak işaretli metodlardan fırlatılan exception'ları işleyen AsyncUncaughtExceptionHandler  arayüzündne kalıtan nesneyi döndürür.


Örnek
Şöyle yaparız. Burada exception handler direkt AsyncConfigurer içinde
@Slf4j @Configuration public class BaseAsyncConfigurer implements AsyncConfigurer { ... //override the getAsyncUncaughtExceptionHandler() method to return our // custom asynchronous exception handler: //!!! All the exception happened in @async will been handler here. @Override public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { return (Throwable ex, Method method, Object... params)-> { try { log.error("\n\n[Exception-Async-Handler] Class-Name: {}-{}\n Type: {}\nException: {}\n\n", method.getDeclaringClass().getName(),method.getName(), ex.getClass().getName(), ex.getMessage()); } catch (Throwable e) { log.error("catch Async Exception: {}", e); } }; } }
Örnek
Şöyle yaparız. Burada exception handler farklı bir sınıf
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import java.lang.reflect.Method; public class AsyncExceptionHandler implements AsyncUncaughtExceptionHandler { @Override public void handleUncaughtException(Throwable throwable, Method method, Object... obj) { System.out.println("Message from exception - " + throwable.getMessage()); System.out.println("Method name " + method.getName()); } }
Kullanmak için şöyle yaparız
@Configuration public class AsyncConfig implements AsyncConfigurer { @Override public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { return new AsyncExceptionHandler(); } }


Hiç yorum yok:

Yorum Gönder