IMAP Idle, ability to skip events?
I am implementing a mail listener in java, the listener injects IDLE until new mail arrives, it prints the email subject and goes back to IDLE again.
Is there a chance that I will miss events (new emails) between the two IDLEs that I issue?
For example, I tried to sleep for 10 seconds before logging into IDLE again, and in those 10 seconds I sent 2 emails to my Inbox which I control, and 10 seconds later when I returned to IDLE, mine MessageCountListener
only received once, for the first message. The second was skipped.
Maybe I shouldn't rely on MessageCountListener
?
messageCountListener = new MessageCountListener() {
//lets go into idle again and then handle the message
public void messagesAdded(MessageCountEvent ev) {
Message message = ev.getMessages()[0];
try {
SimpleLogger.info("New message, subject: " + message.getSubject());
} catch (MessagingException e) {
e.printStackTrace();
}
//GeneralUtility.threadSleep(8000);
startImapIdle();
}
startImapIdle()
is called initially and then re-called from messagesAdded()
as above.
private void startImapIdle() {
SimpleLogger.info("Idling now");
Runnable r = () -> {
try {
inboxFolder.idle(false);
} catch (FolderClosedException e) {
SimpleLogger.warn("Push Error: [DISCONNECT]", e);
GeneralUtility.threadSleep(mailListenerRetryInterval);
startListening(inboxFolder);
} catch (StoreClosedException e) {
SimpleLogger.warn("Push Error: [GLOBAL DISCONNECT]", e);
GeneralUtility.threadSleep(mailListenerRetryInterval);
initConnection();
} catch (MessagingException e) {
SimpleLogger.warn("Push Error: [NO IDLE]", e);
GeneralUtility.threadSleep(mailListenerRetryInterval);
initConnection();
} catch (Exception e) {
SimpleLogger.warn("Push Error: [UNKNOWN]", e);
GeneralUtility.threadSleep(mailListenerRetryInterval);
initConnection();
}
};
imapIdleThread = new Thread(r);
imapIdleThread.start();
}
edit: (new code)
private void startImapIdle() {
while(true) {
try {
SimpleLogger.info("Idling now");
MailUtility.ensureFolderOpen(folder);
folder.idle();
} catch (FolderClosedException e) {
SimpleLogger.warn("Push Error: [DISCONNECT]", e);
GeneralUtility.threadSleep(mailListenerRetryInterval);
startListening();
break;
} catch (StoreClosedException e) {
SimpleLogger.warn("Push Error: [GLOBAL DISCONNECT]", e);
GeneralUtility.threadSleep(mailListenerRetryInterval);
initConnection();
break;
} catch (MessagingException e) {
SimpleLogger.warn("Push Error: [NO IDLE]", e);
GeneralUtility.threadSleep(mailListenerRetryInterval);
initConnection();
break;
} catch (Exception e) {
SimpleLogger.warn("Push Error: [UNKNOWN]", e);
GeneralUtility.threadSleep(mailListenerRetryInterval);
initConnection();
break;
}
}
}
source to share