javamail事件侦听器未获取任何事件

oyxsuwqo  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(651)

按照我的理解,我编写了一个imap处理程序和一个事件监听器,但是我没有看到任何事件触发,尽管收件箱中充满了新的电子邮件。我有什么问题?
我将存储设置为空闲,直到有人关闭邮箱:

  1. public void run() {
  2. boolean logged = false;
  3. synchronized (emailListener) {
  4. while (!isCancelled) {
  5. try {
  6. if (isListening) {
  7. throw new EtlSystemException(
  8. null,
  9. "Disallowed attempt to "
  10. + "start email listener - listening status already set",
  11. null, null);
  12. } else {
  13. isListening = true;
  14. }
  15. if (imapProperties == null) {
  16. imapProperties = new Properties();
  17. imapProperties.put("mail.debug", "true");
  18. }
  19. Session session =
  20. javax.mail.Session.getInstance(imapProperties);
  21. session.setDebug(true);
  22. store = (IMAPStore) session.getStore(imapProtocol);
  23. store.connect(imapHost, imapUser, imapPassword);
  24. inbox = (IMAPFolder) store.getFolder("INBOX");
  25. if (inbox == null) {
  26. throw new NonEtlSystemException(null, "No inbox in "
  27. + this.imapUser,
  28. null, null);
  29. }
  30. inbox.open(Folder.READ_WRITE);
  31. inbox.addMessageCountListener(emailListener);
  32. // While the listener is registered, we loop over idle().
  33. // Only stop if the inbox is closed, or thread interrupted
  34. while (inbox.isOpen()) {
  35. log.debug("store idling...");
  36. inbox.idle();
  37. }
  38. // if any system errors occur processing the emails, stop
  39. // the thread
  40. if (!emailListener.getStatusOK()) {
  41. break;
  42. }
  43. } catch (MessagingException e) {
  44. lastThrowable = e;
  45. } catch (Throwable throwable) {
  46. lastThrowable = throwable;
  47. break;
  48. } finally {
  49. close();
  50. if (lastThrowable != null) {
  51. if (!logged) {
  52. EtlAlert alert = new EtlAlert(lastThrowable);
  53. log.error(alert.getSysLogText(),
  54. alert.getThrowable());
  55. auditLogService.addEvent(alert.getEcnCode(),
  56. alert.getEcnName(),
  57. new Date(),
  58. alert.getUserGpn(),
  59. alert.getUserName(),
  60. alert.getEventType(),
  61. alert.getAuditLogText());
  62. logged = true;
  63. }
  64. if (lastThrowable instanceof MessagingException) {
  65. try {
  66. log.info("Waiting for IMAP server recovery ... "
  67. + "checks every " + WAIT_AT_IMAP_ERROR
  68. + "ms ");
  69. Thread.sleep(WAIT_AT_IMAP_ERROR);
  70. } catch (InterruptedException e1) {
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }
  77. log.debug("thread dying now");
  78. }

我就是这样测试的:

  1. @Test
  2. public void testImapHandler() {
  3. try {
  4. // launch ImapHandler
  5. ImapHandler imapHandler =
  6. new ImapHandler(new EmailListener());
  7. imapHandler.setConfig(host, protocol, imapUser, imapPass);
  8. // now the listener starts listening in a separate thread
  9. Thread test = new Thread(imapHandler, "ImapHandler");
  10. test.start();
  11. // send a test email to inbox
  12. int startCount = imapHandler.getProcessedMessageCount();
  13. sendTestEmail();
  14. // wait for a bit
  15. Thread.sleep(60000);
  16. // stop it
  17. log.debug("stopping listener on thread [" + test.getName() + "]");
  18. test.interrupt();
  19. imapHandler.closeAndTerminate(); // this forces it out of idling
  20. test.join();
  21. // test listener count to see if it raised an email event
  22. int finalCount = imapHandler.getProcessedMessageCount();
  23. assertNotSame("start with " + startCount + ", ended with "
  24. + finalCount, startCount, finalCount);
  25. } catch (final InterruptedException e) {
  26. }
  27. }

这个 imapHandler.closeAndTerminate() 方法关闭收件箱和存储,这将强制 store.idle() 停止阻塞。imaphandler类用作单例,而store和inbox作为memvars保存,仅此而已。
这是imap会话的调试日志:

  1. DEBUG: JavaMail version 1.5.0-b01
  2. DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
  3. DEBUG: Tables of loaded providers
  4. DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
  5. DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
  6. DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
  7. DEBUG: setDebug: JavaMail version 1.5.0-b01
  8. DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle]
  9. DEBUG IMAPS: mail.imap.fetchsize: 16384
  10. DEBUG IMAPS: mail.imap.ignorebodystructuresize: false
  11. DEBUG IMAPS: mail.imap.statuscachetimeout: 1000
  12. DEBUG IMAPS: mail.imap.appendbuffersize: -1
  13. DEBUG IMAPS: mail.imap.minidletime: 10
  14. DEBUG IMAPS: disable AUTH=PLAIN
  15. DEBUG IMAPS: disable AUTH=NTLM
  16. DEBUG IMAPS: trying to connect to host "webmail", port 993, isSSL true
  17. * OK The Microsoft Exchange IMAP4 service is ready.
  18. A0 CAPABILITY
  19. * CAPABILITY IMAP4 IMAP4rev1 AUTH=NTLM AUTH=GSSAPI AUTH=PLAIN UIDPLUS CHILDREN IDLE NAMESPACE LITERAL+
  20. A0 OK CAPABILITY completed.
  21. DEBUG IMAPS: AUTH: NTLM
  22. DEBUG IMAPS: AUTH: GSSAPI
  23. DEBUG IMAPS: AUTH: PLAIN
  24. DEBUG IMAPS: protocolConnect login, host=webmail, user=826, password=<non-null>
  25. DEBUG IMAPS: LOGIN command trace suppressed
  26. DEBUG IMAPS: LOGIN command result: A1 OK LOGIN completed.
  27. A2 CAPABILITY
  28. * CAPABILITY IMAP4 IMAP4rev1 AUTH=NTLM AUTH=GSSAPI AUTH=PLAIN UIDPLUS CHILDREN IDLE NAMESPACE LITERAL+
  29. A2 OK CAPABILITY completed.
  30. DEBUG IMAPS: AUTH: NTLM
  31. DEBUG IMAPS: AUTH: GSSAPI
  32. DEBUG IMAPS: AUTH: PLAIN
  33. DEBUG IMAPS: connection available -- size: 1
  34. A3 SELECT INBOX
  35. * 42 EXISTS
  36. * 2 RECENT
  37. * FLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)
  38. * OK [PERMANENTFLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)] Permanent flags
  39. * OK [UNSEEN 3] Is the first unseen message
  40. * OK [UIDVALIDITY 71441] UIDVALIDITY value
  41. * OK [UIDNEXT 46] The next unique identifier value
  42. A3 OK [READ-WRITE] SELECT completed.
  43. 17:54:34.132 [ImapHandler] DEBUG (ImapHandler.java:205) runListener() - store idling...
  44. A4 IDLE
  45. + IDLE accepted, awaiting DONE command.
  46. * 35 FETCH (FLAGS (\Seen))
  47. 17:55:33.320 [main] DEBUG (ImapTest.java:96) testImapHandler() - stopping listener on thread [ImapHandler]
  48. DONE
  49. A4 OK IDLE completed.
  50. DEBUG IMAPS: IMAPProtocol noop
  51. A5 NOOP
  52. A5 OK NOOP completed.
  53. A6 CLOSE
  54. A6 OK CLOSE completed.
  55. DEBUG IMAPS: added an Authenticated connection -- size: 1
  56. DEBUG IMAPS: IMAPProtocol noop
  57. A7 NOOP
  58. A7 OK NOOP completed.
  59. A8 LOGOUT
  60. * BYE Microsoft Exchange Server 2010 IMAP4 server signing off.
  61. A8 OK LOGOUT completed.
  62. DEBUG IMAPS: IMAPStore connection dead
  63. DEBUG IMAPS: IMAPStore cleanup, force false
  64. DEBUG IMAPS: IMAPStore cleanup done
  65. 17:55:33.342 [ImapHandler] DEBUG (ImapHandler.java:188) run() - thread dying now

您可以从imap日志记录之间的log4j log语句中看到 Store.idle() 命令运行了整整一分钟,在此期间测试线程发送了测试电子邮件。
我能想到的任何其他相关信息:
交易所2010
汤姆猫7
javamail 1.5.0-b01
jdk 1.7.0-55标准

mccptt67

mccptt671#

首先,这是一个非常古老的javamail版本。你应该升级到当前版本,以防它与你的问题有关。
另外,通过在while循环中包含“store.isconnected()”,可以强制javamail创建到服务器的另一个连接。那可能不是你需要的。
是什么产生了“停止侦听器”消息?这是听众自己说的吗?有些东西正在中止idle命令并关闭文件夹和存储区,但您的代码不清楚是什么导致了这种情况。
这是一个独立的java程序吗?还是在应用服务器上运行?

相关问题