I am making a program that needs to monitor a Gmail account for new messages, and in order to get them ASAP I am using JavaMail's idle feature. Here is a code snippet from the thread I am using to call folder.idle():
//Run method that waits for idle input. If an exception occurs, end the thread's life.
public void run() {
IMAPFolder folder = null;
try {
folder = getFolder();
while(true)
{
//If connection has been lost, attempt to restore it
if (!folder.isOpen())
folder = getFolder();
//Wait until something happens in inbox
folder.idle(true);
//Notify controller of event
cont.inboxEventOccured();
}
}
catch (Exception ex) {
ex.printStackTrace();
}
System.out.println("MailIdleWaiter thread ending.");
}
The getFolder() method basically opens the connection to the IMAP server and opens the inbox.
This works for a while, but after 10 minutes or so it stops getting updates (no exception is thrown).
I am looking for suggestions to keep the connection alive. Do I need a second thread whose only role is to sleep and renew the idle() thread every 10 minutes or is there an easier/better way?
Thanks in advance.