I have a problem with sending email with method annotated as @Async. Firstly, I am not sure if it is possible to work as I want so I need help with explanation.
Here is what am doing now:
In main method i have annotation
@EnableAsync(proxyTargetClass = true) Next I have AsyncConfig class
import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.AsyncConfigurerSupport; import java.util.concurrent.Executor; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; @Configuration public class AsyncConfig extends AsyncConfigurerSupport { @Override public Executor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(2); executor.setMaxPoolSize(2); executor.setQueueCapacity(500); executor.setThreadNamePrefix("email-"); executor.initialize(); return executor; } } Of course, its rest application so i have controller, service etc, looks normally, nothing special
My async method looks like this:
@Async public void sendEmail() throws InterruptedException { log.info("Sleep"); Thread.sleep(10000L); //method code log.info("Done"); } I executing this method in another service method:
@Override public boolean sendSystemEmail() { try { this.sendEmail(); } catch (InterruptedException e) { e.printStackTrace(); } log.info("pending sendEmail method"); return true; } Now what I want archive is to ignore executing sendEmail() function and execute return true; meanwhile function sendEmail() will be executing in another Thread. Of course it doesn't work now as I want. Unfortunately.
Note that I am new into async programming, so I have lack of knowledge in some parts of this programming method.
Thanks for any help.
sendMailexternally and not internally.