sunIMAPFolderとidleコマンドを使用してセンサーから送信された電子メールを解析するメールサーバーを実装しようとしています。
残念ながら、アイドル状態は30分後に閉じ、FolderClosedExceptionをスローします。アイドル状態の再起動中に電子メールが到着するという最悪のシナリオをシミュレートするために、例外本文でテスト電子メールを送信し、電子メールが処理されるかどうかを確認します。
私のコード:
public void run() {
try {
Properties props = System.getProperties();
props.setProperty("mail.imap.connectiontimeout", "5000");
props.setProperty("mail.imap.timeout", "5000");
for(;;) {
try {
Session session = Session.getInstance(props, null);
Store store = session.getStore("imap");
store.connect("localhost", "test@email.db.de", "testpw1");
Folder folder = store.getFolder("inbox");
if ((folder == null) || !folder.exists()) {
logger.error("folder -inbox- is invalid.");
return;
}
folder.open(Folder.READ_WRITE);
// Add messageCountListener to listen for new messages
folder.addMessageCountListener(new MessageCountAdapter() {
@Override
public void messagesAdded(MessageCountEvent event) {
Message[] messages = event.getMessages();
processMessages(messages);
}
});
logger.debug("idle start");
IMAPFolder f = (IMAPFolder) folder;
f.idle(); //idle state
} catch (FolderClosedException ex) {
logger.info("idle timeout");
long time = Calendar.getInstance().getTimeInMillis();
DateFormat df = DateFormat.getDateTimeInstance();
SendEmail.send("test@email.db.de", "TestSubject", "This mail is sent when idle is off" + df.format(new Date(time));
} catch (Exception ex) {
ex.printStackTrace();
}
}//endfor
} catch (Exception ex) {
ex.printStackTrace();
}
}
残念ながら、2つの異なる出力が得られ、その理由はわかりません。
メールが処理される場合があります。
02/25/2013 11:45:04.202 DEBUG [Thread-0] logger - idle start
02/25/2013 12:15:04.202 INFO [Thread-0] logger - idle timeout
This mail is sent when idle is off 25.02.2013 12:15:04
02/25/2013 12:15:04.235 DEBUG [Thread-0] logger - idle start
Got 1 new messages
02/25/2013 12:15:04.267 DEBUG [Thread-0] logger - idle start
そして時々そうではありません:
02/25/2013 10:41:28.895 DEBUG [Thread-0] logger - idle start
02/25/2013 11:11:28.898 INFO [Thread-0] logger - idle timeout
This mail is sent when idle is off 25.02.2013 11:11:28
02/25/2013 11:11:28.962 DEBUG [Thread-0] logger - idle start
私の目標は、folderClosedExceptionがスローされなくなるか、キャッチ本文の電子メールが常に処理されるようにすることです。
どうすればこれを達成できますか?ここに2種類の出力があるのはなぜですか?
前もって感謝します。