Creating standalone program to monitor the mailbox

Discussions

General J2EE: Creating standalone program to monitor the mailbox

  1. I am trying to create a standalone application which needs to monitor my mailbox.
    I have created 2 classes.

    MailScanner.class = which is the parent program. On execution of this program it creates objects of InboxMonitor class. Each InboxMonitor object is associated with a mail account. If any new mail arrives in the specified mail account then the event listener automatically listens to it.

    To keep this parent program running i have coded an infinite while loop which sleeps for every 5 secs. Then the program resumes. Hence the event listener will be notified only after 5 secs.(Probably need to find a much better way to do it)

    InboxMonitor.class = This is the event listener class for recording new incoming messages. On arrival of a new mail the appropriate action is taken.

    Now i need to keep my MailScanner.class continuously running.
    Hence i have coded an infinite loop in my MailScanner.class.

    -- Here goes the code for creating the instances of InboxMonitor classes and register the listener to the inbox

    while (true)
    {
    Thread.sleep(5000);
    }

    This piece of code keeps the mailscanner class alive but sleeps for 5 secs. Hence the incoming mails will get pooled for every 5 sec.
    But what i require is a program that continuously runs without any sleep and listen to any incoming request without any delay.

    If i keep the the code as while(true){}, then the cpu usage is at maximum.
    Is there any alternate way to keep my MailScanner class active and alive ?
    I am also a novice in thread programming. So can anybody help me out

    Thanks in advance
  2. Monitor Mial box[ Go to top ]

    Hi, I have same requirement. Kindly update me with the information which you gathered. Thanks, Sri
  3. Since mail is not being sent to you your only option is to pull. This requires polling. Now you can use a scheduler like Quartz or you will have to write a loop that polls and sleeps as you have done. No magic bullet here. Not sure what type of mail box you have but you might get the mailbox to trigger and event to send (push) you data. In this case you would be able to wait for the message.
    But what i require is a program that continuously runs without any sleep and listen to any incoming request without any delay.
    Note sure what this means since you are not listening to anything incomming, you are polling your mail box to see if anything new has arrived.