【发布时间】:2015-09-19 18:18:27
【问题描述】:
我在java中实现了一个邮件监听器,监听器进入空闲状态,直到有新邮件到达,它打印电子邮件主题并立即再次进入空闲状态。
我是否有可能错过我发出的两个 IDLE 之间的事件(新电子邮件)?
例如,我尝试在再次进入 IDLE 之前睡 10 秒,在这 10 秒内我向我正在监控的收件箱发送了 2 封电子邮件,在我回到 IDLE 的 10 秒后我的@ 987654321@ 只接到了一次电话,第一封电子邮件。第二次错过了。
也许我不应该依赖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() 最初被调用,然后像上面一样从messagesAdded() 被重新调用。
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();
}
编辑:(新代码)
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;
}
}
}
【问题讨论】:
标签: java email jakarta-mail imap