I have ThreadPoolTaskExecutor. I should send too many emails (different emails). If I have error during email sending, I should write it in database.
<bean id="taskExecutor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="5" />
<property name="maxPoolSize" value="10" />
<property name="WaitForTasksToCompleteOnShutdown" value="true" />
</bean>
I'm executing tasks. taskExecutor.execute(Runable). Everything works well!
@Override
public void run() {
try {
mailService.sendMail();
} catch (Throwable t) {
daoService.writeFaillStatus();
}
}
Everything seems ok! Async request are doing well!
I also have
white(true) {
if(executor.getActiveCount()==0){
executor.shutdown(); break;
}
Thread.sleep(3000)
}
Because of WaitForTasksToCompleteOnShutdown=true task never will be shut down automatically. In the other words, main thread never will be destroyed (Main thread is the thread where I invoke thread executor tasks - when I run code in eclipse, terminal is active always). Even after executor thread will finish their work my console looks like this:
I think this is because, main thread is waiting something - someone who tell that "everything is already done, relax, go and shut down"
So thought about this solution while(true). Could you tell me if this is good idea? Might be it is not good.
I know that this executor also have submit() method. I think I do not need here. Please correct me If I am not correct in this post.
If you know before hand how many are "too many emails", I would suggest to take a look at CountDownLatch
rather than the busy wait loop to check the task status.
In main thread set the
CountDownLatch latch = new CountDownLatch(TOO_MANY_EMAILS);
Pass this instance to runnable instance, where we call latch.countDown()
after sending each mail.
In the main thread we wait for the latch to countdown: latch.await()
. This will block main thread execution.
After which you could safely shutdown the thread pool knowing all the work has been completed.