按照我的理解,我编写了一个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类用作单例,而store和inbox作为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 log语句中看到 Store.idle()
命令运行了整整一分钟,在此期间测试线程发送了测试电子邮件。
我能想到的任何其他相关信息:
交易所2010
汤姆猫7
javamail 1.5.0-b01
jdk 1.7.0-55标准
1条答案
按热度按时间mccptt671#
首先,这是一个非常古老的javamail版本。你应该升级到当前版本,以防它与你的问题有关。
另外,通过在while循环中包含“store.isconnected()”,可以强制javamail创建到服务器的另一个连接。那可能不是你需要的。
是什么产生了“停止侦听器”消息?这是听众自己说的吗?有些东西正在中止idle命令并关闭文件夹和存储区,但您的代码不清楚是什么导致了这种情况。
这是一个独立的java程序吗?还是在应用服务器上运行?