我编写了一个IMAP处理程序和一个事件监听器,因为我知道它应该完成,但我没有看到任何事件发生,尽管用新电子邮件填充收件箱。可能是我的问题?
我将商店设置为空闲,直到有人关闭邮箱:
public void run() {
boolean logged = false;
synchronized (emailListener) {
while (!isCancelled) {
try {
if (isListening) {
throw new EtlSystemException(
null,
"Disallowed attempt to "
+ "start email listener - listening status already set",
null, null);
} else {
isListening = true;
}
if (imapProperties == null) {
imapProperties = new Properties();
imapProperties.put("mail.debug", "true");
}
Session session =
javax.mail.Session.getInstance(imapProperties);
session.setDebug(true);
store = (IMAPStore) session.getStore(imapProtocol);
store.connect(imapHost, imapUser, imapPassword);
inbox = (IMAPFolder) store.getFolder("INBOX");
if (inbox == null) {
throw new NonEtlSystemException(null, "No inbox in "
+ this.imapUser,
null, null);
}
inbox.open(Folder.READ_WRITE);
inbox.addMessageCountListener(emailListener);
// While the listener is registered, we loop over idle().
// Only stop if the inbox is closed, or thread interrupted
while (inbox.isOpen()) {
log.debug("store idling...");
inbox.idle();
}
// if any system errors occur processing the emails, stop
// the thread
if (!emailListener.getStatusOK()) {
break;
}
} catch (MessagingException e) {
lastThrowable = e;
} catch (Throwable throwable) {
lastThrowable = throwable;
break;
} finally {
close();
if (lastThrowable != null) {
if (!logged) {
EtlAlert alert = new EtlAlert(lastThrowable);
log.error(alert.getSysLogText(),
alert.getThrowable());
auditLogService.addEvent(alert.getEcnCode(),
alert.getEcnName(),
new Date(),
alert.getUserGpn(),
alert.getUserName(),
alert.getEventType(),
alert.getAuditLogText());
logged = true;
}
if (lastThrowable instanceof MessagingException) {
try {
log.info("Waiting for IMAP server recovery ... "
+ "checks every " + WAIT_AT_IMAP_ERROR
+ "ms ");
Thread.sleep(WAIT_AT_IMAP_ERROR);
} catch (InterruptedException e1) {
}
}
}
}
}
}
log.debug("thread dying now");
}
这就是我测试它的方式:
@Test
public void testImapHandler() {
try {
// launch ImapHandler
ImapHandler imapHandler =
new ImapHandler(new EmailListener());
imapHandler.setConfig(host, protocol, imapUser, imapPass);
// now the listener starts listening in a separate thread
Thread test = new Thread(imapHandler, "ImapHandler");
test.start();
// send a test email to inbox
int startCount = imapHandler.getProcessedMessageCount();
sendTestEmail();
// wait for a bit
Thread.sleep(60000);
// stop it
log.debug("stopping listener on thread [" + test.getName() + "]");
test.interrupt();
imapHandler.closeAndTerminate(); // this forces it out of idling
test.join();
// test listener count to see if it raised an email event
int finalCount = imapHandler.getProcessedMessageCount();
assertNotSame("start with " + startCount + ", ended with "
+ finalCount, startCount, finalCount);
} catch (final InterruptedException e) {
}
}
imapHandler.closeAndTerminate()
方法会关闭收件箱和商店,这会强制store.idle()
停止阻止。 IMAPHandler类用作单例,商店和收件箱保存为memvars,仅用于此目的。
这是IMAP会话的调试日志:
DEBUG: JavaMail version 1.5.0-b01
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
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]}
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]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: setDebug: JavaMail version 1.5.0-b01
DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle]
DEBUG IMAPS: mail.imap.fetchsize: 16384
DEBUG IMAPS: mail.imap.ignorebodystructuresize: false
DEBUG IMAPS: mail.imap.statuscachetimeout: 1000
DEBUG IMAPS: mail.imap.appendbuffersize: -1
DEBUG IMAPS: mail.imap.minidletime: 10
DEBUG IMAPS: disable AUTH=PLAIN
DEBUG IMAPS: disable AUTH=NTLM
DEBUG IMAPS: trying to connect to host "webmail", port 993, isSSL true
* OK The Microsoft Exchange IMAP4 service is ready.
A0 CAPABILITY
* CAPABILITY IMAP4 IMAP4rev1 AUTH=NTLM AUTH=GSSAPI AUTH=PLAIN UIDPLUS CHILDREN IDLE NAMESPACE LITERAL+
A0 OK CAPABILITY completed.
DEBUG IMAPS: AUTH: NTLM
DEBUG IMAPS: AUTH: GSSAPI
DEBUG IMAPS: AUTH: PLAIN
DEBUG IMAPS: protocolConnect login, host=webmail, user=826, password=<non-null>
DEBUG IMAPS: LOGIN command trace suppressed
DEBUG IMAPS: LOGIN command result: A1 OK LOGIN completed.
A2 CAPABILITY
* CAPABILITY IMAP4 IMAP4rev1 AUTH=NTLM AUTH=GSSAPI AUTH=PLAIN UIDPLUS CHILDREN IDLE NAMESPACE LITERAL+
A2 OK CAPABILITY completed.
DEBUG IMAPS: AUTH: NTLM
DEBUG IMAPS: AUTH: GSSAPI
DEBUG IMAPS: AUTH: PLAIN
DEBUG IMAPS: connection available -- size: 1
A3 SELECT INBOX
* 42 EXISTS
* 2 RECENT
* FLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)
* OK [PERMANENTFLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)] Permanent flags
* OK [UNSEEN 3] Is the first unseen message
* OK [UIDVALIDITY 71441] UIDVALIDITY value
* OK [UIDNEXT 46] The next unique identifier value
A3 OK [READ-WRITE] SELECT completed.
17:54:34.132 [ImapHandler] DEBUG (ImapHandler.java:205) runListener() - store idling...
A4 IDLE
+ IDLE accepted, awaiting DONE command.
* 35 FETCH (FLAGS (\Seen))
17:55:33.320 [main] DEBUG (ImapTest.java:96) testImapHandler() - stopping listener on thread [ImapHandler]
DONE
A4 OK IDLE completed.
DEBUG IMAPS: IMAPProtocol noop
A5 NOOP
A5 OK NOOP completed.
A6 CLOSE
A6 OK CLOSE completed.
DEBUG IMAPS: added an Authenticated connection -- size: 1
DEBUG IMAPS: IMAPProtocol noop
A7 NOOP
A7 OK NOOP completed.
A8 LOGOUT
* BYE Microsoft Exchange Server 2010 IMAP4 server signing off.
A8 OK LOGOUT completed.
DEBUG IMAPS: IMAPStore connection dead
DEBUG IMAPS: IMAPStore cleanup, force false
DEBUG IMAPS: IMAPStore cleanup done
17:55:33.342 [ImapHandler] DEBUG (ImapHandler.java:188) run() - thread dying now
您可以在IMAP日志记录中的log4j日志语句中看到Store.idle()
命令运行了整整一分钟,在此期间测试线程发送了测试电子邮件。
我能想到的任何其他相关信息:
Exchange 2010
Tomcat 7
Javamail 1.5.0-b01
jdk 1.7.0-55
答案 0 :(得分:0)
嗯,首先,这是一个非常古老的JavaMail版本。您应该升级到current version,以防它与您的问题有关。
此外,通过包含&#34; store.isConnected()&#34;在while循环中,您强制JavaMail创建与服务器的另一个连接。这可能不是你需要的东西。
产生&#34;停止听众&#34;信息?这是来自听众吗?有些东西正在中止IDLE命令并关闭文件夹和存储,但是你的代码并不清楚导致它的原因。
这是一个独立的Java程序吗?或者它是否在应用程序服务器中运行?