erotsppa
erotsppa

Reputation: 15031

How can I interrupt IMAP's IDLE?

I am using the Javamail API connecting to my IMAP server. Everything is working great with the javax.mail.Folder.idle() method. My listener gets called when a new mail comes in. However the problem is idle blocks forever, how do I interrupt it? How do I actually stop the listening without killing my Java program?

I've tried calling Thread.interrupt() on the idle'd thread. Nothing happens. I am running out of ideas.

Upvotes: 8

Views: 3155

Answers (3)

Pascal Chardon
Pascal Chardon

Reputation: 206

A proper way to abort IDLE command is the following snippet. Note that the Folder instance should be the same as the one used to start idling. I've tested the other solutions proposed on this thread but they didn't work in my case.

IMAPFolder folder = store.getFolder("INBOX");
try {
  folder.doOptionalCommand("Abort IDLE error mesage", new IMAPFolder.ProtocolCommand() {
  @Override
  public Object doCommand(IMAPProtocol p) throws ProtocolException {
    p.idleAbort();
    return Boolean.TRUE;
  }
});
} catch (MessagingException e) {
  e.printStackTrace();
}

Upvotes: 1

Mo Firouz
Mo Firouz

Reputation: 107

If you read the documentation properly, and read the source code, you'll realise that you have to create a new thread for calling .idle().

Allocate that thread to a variable, and whenever you want call the interrupt() on that thread, or just ignore notifications!

If you need to get idle() going again, just rerun the thread!

I created something similar, so you might wanna check it out.

https://github.com/mofirouz/JavaPushMail/blob/master/src/main/java/com/mofirouz/javapushmail/JavaPushMailAccount.java

Good luck

Upvotes: 4

ChssPly76
ChssPly76

Reputation: 100736

Performing any operation on that folder (from another thread) will cause idle() method to return immediately. So if you want to forcefully interrupt it, just call close() from a new thread.

Upvotes: 10

Related Questions