I wanted to write async method.
@Component
public class TaskImpl implements Task{
@Async(value="myExecutor")
public void async(int n) throws InterruptedException{
log.info("Executing task"+n);
Thread.sleep(1000);
log.info("Finished executing task" +n);
}
}
It is nice! I have config like that:
<task:annotation-driven executor="myExecutor" />
<task:executor id="myExecutor" pool-size="3" />
That's nice! I have write test class too!
@Autowired
private Task task;
@Test
public void mailSendTest() throws InterruptedException{
for (int i = 0; i <5; i++) {
task.async(i);
}
Thread.sleep(3000)
}
I have tested and async works very well! tasks are in parallel!
Question 1. but when main thread of test case destroys ,Execute threads does not work. In the other words, If I do not write Thread.sleep() in test cases, Executor is shooting down immediately. How can I fix that?
2015-03-12 17:02:26.706 INFO ThreadPoolTaskExecutor: Shutting down ExecutorService
I do not want to write JOIN or Sleep.
Question 2. I need async() method in order to invoke mail sender class. In the other words I have written this component.
@Component
public class MailMail {
private MailSender mailSender;
public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
}
@Async
public void sendMail(String from, String to, String subject, String msg) {
....
}
}
}
Now I'm interested how to solve the following problem. Might be there is communication , network problem, and I can't send email. what happens now? how to catch exception and send it again?