Skip to content

Commit

Permalink
Resolve FolderClosedException error (#46)
Browse files Browse the repository at this point in the history
* Resolve FolderClosedException error

 - Accroding to RFC 2177, IDLE connection terminated after timeout and The javax.mail APIs have no way to set a timeout for the IDLE command
 - So to solve issue #4 creating the second thread which issue a NOOP command to the server. This does nothing at all, but is enough to have IDLE abort and be reissued

* Change KEEP_ALIVE_FREQ from 10 minutes to 2 minutes
  • Loading branch information
nishitm authored Jun 20, 2020
1 parent 7fcad5c commit e6b0a8b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ lib/
bin/
*.html
./EmailsToTelegramService/src/main/resources/application-qa.yml
./EmailsToTelegramService/src/main/resources/application-dev.yml
*.yml

### STS ###
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import io.github.trashemail.EmailsToTelegramService.Configuration.ImapClientServiceConfig;
import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPStore;
import com.sun.mail.imap.protocol.IMAPProtocol;
import com.sun.mail.iap.ProtocolException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -31,13 +33,13 @@ public class ImapClient {

private static final Logger log = LoggerFactory.getLogger(ImapClient.class);

private static String username ;
private static String password ;
private static String imapHost ;
private static String imapPort ;
private static String username;
private static String password;
private static String imapHost;
private static String imapPort;

@PostConstruct
public void init(){
public void init() {
username = imapClientServiceConfig.getImap().getEmail();
password = imapClientServiceConfig.getImap().getPassword();
imapHost = imapClientServiceConfig.getImap().getHost();
Expand Down Expand Up @@ -82,11 +84,27 @@ public void messagesAdded(MessageCountEvent event) {
}
});

IdleThread idleThread = new IdleThread(inbox);
idleThread.setDaemon(false);
Thread idleThread = new Thread(new KeepAliveRunnable((IMAPFolder) inbox));

idleThread.start();
while (!Thread.interrupted()) {
try {
ensureOpen(inbox);
log.info("IMAP client: IDLE Listening state ...");
((IMAPFolder) inbox).idle();
} catch (Exception e) {
e.printStackTrace();
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
}
}
}

if (idleThread.isAlive()) {
idleThread.interrupt();
}

idleThread.join();
} catch (Exception e) {
e.printStackTrace();
} finally {
Expand All @@ -95,37 +113,35 @@ public void messagesAdded(MessageCountEvent event) {
}
}

private static class IdleThread extends Thread {
private final Folder folder;
private volatile boolean running = true;
private static class KeepAliveRunnable implements Runnable {

public IdleThread(Folder folder) {
super();
this.folder = folder;
}
private static final long KEEP_ALIVE_FREQ = 120000; // 2 minutes

public synchronized void kill() {
private IMAPFolder folder;

if (!running)
return;
this.running = false;
public KeepAliveRunnable(IMAPFolder folder) {
this.folder = folder;
}

@Override
public void run() {
while (running) {
while (!Thread.interrupted()) {
try {
ensureOpen(folder);
log.info("IMAP client: IDLE Listening state ...");
((IMAPFolder) folder).idle();
} catch (Exception e) {
e.printStackTrace();
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
}
Thread.sleep(KEEP_ALIVE_FREQ);

// Perform a NOOP just to keep alive the connection
log.debug("Performing a NOOP to keep alive the connection");
folder.doCommand(new IMAPFolder.ProtocolCommand() {
public Object doCommand(IMAPProtocol protocol)
throws ProtocolException {
protocol.simpleCommand("NOOP", null);
return null;
}
});
} catch (InterruptedException e) {
} catch (MessagingException e) {
log.warn("Unexpected exception while keeping alive the IDLE connection", e);
}

}
}
}
Expand All @@ -151,7 +167,7 @@ public static void close(final Store store) {
}

public static void ensureOpen(final Folder folder)
throws MessagingException {
throws MessagingException {

if (folder != null) {
Store store = folder.getStore();
Expand All @@ -168,7 +184,7 @@ public static void ensureOpen(final Folder folder)
folder.open(Folder.READ_ONLY);
if (!folder.isOpen())
throw new MessagingException("Unable to open folder " +
folder.getFullName());
folder.getFullName());
}

}
Expand Down

0 comments on commit e6b0a8b

Please sign in to comment.