【问题标题】:IMAP Idle, possibility to miss events?IMAP 空闲,有可能错过活动吗?
【发布时间】: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


    【解决方案1】:

    你是怎么做到的?下面的代码运行良好。

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

    当然你需要有另一个无限循环,比如:

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

    【讨论】:

    • 在上面添加了一些代码。我不明白你为什么要在无限循环中使用 .idle() 。规范说:客户端不得在服务器等待 DONE 时发送命令,因为服务器将无法区分命令和延续。
    • api 文档 (javamail.java.net/nonav/docs/api/com/sun/mail/imap/… ) 说如果另一个线程对该文件夹发出命令,idle 将返回。它还建议在循环中使用它。请从示例代码中查看monitor.java (java.net/projects/javamail/downloads/download/…)
    • 感谢您的参考。我改变了我这样做的方式,我把空闲放在一个无限循环中,现在我想我没有错过任何事件。老实说,虽然我仍然不明白为什么我的原始方法错过了一些事件,即使我在 .idle() 被中断后立即发出。您可以查看原始帖子以了解代码更改。无论如何,现在一切正常,所以我会接受这个,谢谢。
    猜你喜欢
    • 2011-01-31
    • 1970-01-01
    • 2017-07-14
    • 2021-10-29
    • 1970-01-01
    • 2011-05-16
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多