IMAP空闲,错过事件的可能性?

时间:2015-07-02 09:31:48

标签: java email javamail imap

我在java中实现了一个邮件监听器,监听器进入IDLE直到新邮件到达,它打印出电子邮件主题并再次立即返回IDLE。

我是否有机会错过我发出的两个IDLE之间的事件(新电子邮件)?

例如,在再次进入IDLE之前我已经尝试过睡了10秒钟,在这10秒钟内我发送了2封电子邮件到收件箱我监控,10秒后我回去了进入IDLE我的MessageCountListener只被调用一次,第一封电子邮件。第二个错过了。

也许我不应该依赖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;
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

你好吗?以下代码运行良好。

  

inbox.addMessageCountListener(new MessageCountAdapter(){...})

当然你还需要另外一个无限循环:

          while (true) {
                  IMAPFolder f = (IMAPFolder)inbox;
                  f.idle();
          }