3

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)

Andrei Botalov
  • 20,686
  • 11
  • 89
  • 123

1 Answers1

3
  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".

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • 3
    Although you should check the ExecutorService interface in preference to Timer, see http://stackoverflow.com/questions/409932/java-timer-vs-executorservice – ptomli Jul 19 '11 at 17:01
  • 1. Not only thread that made request but also thread that performs search should know that timeout expires. When using `poll` with specified timeout thread that performs search will know nothing about that timeout has been expired. 2. Some functions can send request that they are waiting for message. When using `scheduleAtFixedRate` method each search for message will perform independently from other searches. I prefer to retrieve list of messages every 30 seconds and then search in this list every message we are waiting for from the list. – Andrei Botalov Jul 19 '11 at 17:40
  • 1. use the same pattern: In the search thread, wait for a queue in which the poll thread will eventually put the search results. 2. Use a timer which retrieves list of messages every 30 seconds and put that into a queue for a search-the-list-thread. – Aaron Digulla Jul 20 '11 at 07:14