1

I'm using IdleManager, in Scala, to listen to a Gmail folder.

I already have this props.setProperty("mail.imaps.usesocketchannels", "true")

The main part of my code is like this:

  folder.addMessageCountListener(new MessageCountAdapter() {
    override def messagesAdded(ev: MessageCountEvent) {
      Logger.info("Got " + ev.getMessages.length + " new messages")
      idleManager.watch(folder)
    }
  })

  // timeLength = 20 minutes
  system.scheduler.schedule(initialDelay = timeLength, interval = timeLength) {
    try {
      folder.asInstanceOf[IMAPFolder].doCommand(new IMAPFolder.ProtocolCommand() {
        def doCommand(p: IMAPProtocol) = {
          p.simpleCommand("NOOP", null)
          null
        }
      })
      Logger.debug("Continue after sending NOOP")
      idleManager.watch(folder)
    } catch {
      case e: Exception => Logger.error(s"MailHelper: ${e.getMessage}")
    }
  }

  idleManager.watch(folder)

You can see that I let the idleManager continue watching the folder after I get new messages and after I send a NOOP command. A scheduler is created to periodically (currently, once in 20 minutes) send a NOOP command to the server, to keep the connection. My program worked fine, but just for a while.

14 hours after the first call idleManager.watch(folder) and about 12.5 hours after the last email received, I still got the log Continue after sending NOOP, but right after that is an error log MailHelper: Folder is not using SocketChannels.

Could you please help me with this?


Edited: Thanks @BillShannon for your quick reply. I have updated from v1.5.2 to v1.5.6 and turned on the debug output. I'm sure the Properties object and the "store" instance (created from a Session with the "imaps" protocol) are unchanged.

The error has appeared again. After a call to idleManager.watch(folder), here is the log ([folder] is the imaps protocol string for my folder)

DEBUG IMAP: IdleManager watching [folder]
A385 IDLE
+ idling
DEBUG IMAP: startIdle: set to IDLE
DEBUG IMAP: startIdle: return true
DEBUG IMAP: IdleManager.watch startIdle succeeded for [folder]
DEBUG IMAP: IdleManager selected 0 channels
DEBUG IMAP: IdleManager adding [folder] to selector
DEBUG IMAP: IdleManager waiting...
DEBUG IMAP: IdleManager selected 1 channels
DEBUG IMAP: IdleManager selected folder: [folder]
DEBUG IMAP: handleIdle: set to RUNNING
DEBUG IMAP: IdleManager got exception for folder: [folder], THROW: 
javax.mail.FolderClosedException: * BYE JavaMail Exception: java.io.IOException: Connection dropped by server?
    at com.sun.mail.imap.IMAPFolder.handleIdle(IMAPFolder.java:3199)
    at com.sun.mail.imap.IdleManager.processKeys(IdleManager.java:370)
    at com.sun.mail.imap.IdleManager.select(IdleManager.java:281)
    at com.sun.mail.imap.IdleManager.access$200(IdleManager.java:137)
    at com.sun.mail.imap.IdleManager$1.run(IdleManager.java:164)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
DEBUG IMAP: IdleManager waiting...

20 minutes later, the program sent another "NOOP", the status returned was "OK". And the program called idleManager.watch(folder) once again, here the error log reappeared Folder is not using SocketChannels.

Do you need anything else? Is this an issue with the library?

Phu Nguyen
  • 43
  • 5
  • Is this reproducible? What version of JavaMail are you using? Are you sure nothing is modifying the Properties object used by the JavaMail Session? Are you sure you're always using the "imaps" protocol and never the "imap" protocol or the "gimap" protocol? You may need to turn on [JavaMail debug output](http://www.oracle.com/technetwork/java/javamail/faq/index.html#debug) to get more detail when this happens. – Bill Shannon Feb 23 '17 at 06:50
  • @BillShannon I have updated my question, please check it – Phu Nguyen Feb 24 '17 at 12:36
  • 1
    The exception message is misleading. The problem is that the folder you're trying to watch is actually closed because of the FolderClosedException you got. Because you're using a low level IMAP protocol command, it's not checking whether the folder is open. Because it's not open, the low level command is using the store's connection to execute the command. Thus, your application is never getting the direct notification that the folder is closed. Instead of the low level NOOP, just do a folder.getMessageCount(). That will do a NOOP and throw a FolderClosedException when appropriate. – Bill Shannon Feb 24 '17 at 21:26
  • So can you tell me how I should do to check what caused that FolderClosedException? I can't think of any way for it to appear since everything had been working normally for more than 1 day and then it came out of nowhere – Phu Nguyen Feb 27 '17 at 04:40
  • It really doesn't matter what caused the exception. The connection can be closed for many reasons you can't predict and you have to be prepared to handle any of them. The fact that you got lucky for 1 day doesn't mean anything. – Bill Shannon Feb 27 '17 at 05:00
  • I see. Thank you very much for your help – Phu Nguyen Feb 27 '17 at 07:09

0 Answers0