Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Periodically check for condition with timeout

The process is the following:
Request to check for particular email message received is came. This email message should be added to the list of messages that are periodically checked for availability on mail server. Every 30 seconds another thread should search for messages from this list. If message is found it should be returned somehow to function that made request. If message isn't found during specified timeout period, exception should be thrown.

Note: I think that it may be quiet costly to create new thread every time new message appears. So I want to search for all messages from the list in one thread periodically.

How / with help of which classes I can implement it? (Javamail part is ready)

like image 622
Andrei Botalov Avatar asked Dec 12 '25 11:12

Andrei Botalov


1 Answers

  1. Use a java.util.concurrent.BlockingQueue to receive the message because you can say poll(long timeout, TimeUnit unit) so the receiving thread doesn't use any CPU at all.

  2. To check the mails periodically, use a java.util.Timer "for repeated execution at regular intervals".

like image 105
Aaron Digulla Avatar answered Dec 14 '25 04:12

Aaron Digulla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!