I want to run a method in a separate thread. Therefore I tried to use @Async but it does not work. The printed output always refers to the same thread id.
MyApp.java
@SpringBootApplication
@EnableAsync
@EnableConfigurationProperties(ConfigProperties.class)
public class MyApp {
public static void main(String[] args) throws Exception {
SpringApplication.run(MyApp.class, args);
}
@Bean("threadPoolTaskExecutor")
public TaskExecutor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(20);
executor.setMaxPoolSize(1000);
executor.setWaitForTasksToCompleteOnShutdown(true);
executor.setThreadNamePrefix("Async-");
return executor;
}
}
MyService.java
@Slf4j
@Service
public class MyService implements ISichtServiceBroker {
@Inject
public MyService(Helper helper) {
this.helper = helper;
}
@Transactional
public void handle(Message message) {
System.out.println("Execute method synchronously - " + Thread.currentThread().getName());
handleAsync(message);
}
@Async("threadPoolTaskExecutor")
public void handleAsync(Message message) {
System.out.println("Execute method asynchronously - " + Thread.currentThread().getName());
}
}