IMAP: (JavaMail) Get unread number of messages from many folders (performance improvement)

Our client has an imap account in 400 folders. We receive mail using the java mail api. At the beginning of the program, we need all the unread messages in each folder.

Suppose we have folder names in a string array

String arryFolderNames[] = { "f1", "f2"..

      

And for each folder:

for (String folderName : arryFolderNames) {
    IMAPFolder imapFolder = MailStore.getFolder(folderName);

    //takes 100-300 milliseconds
    int unreadCount = imapFolder.getUnreadMessageCount();
}

      

It takes at least 100 milliseconds to get the unread number of messages in one folder, and the whole process takes over 40 seconds, but the client only takes 5-6 seconds. in the majority. I have to optimize this process:

  • All folders are closed and I am executing getUnreadMessgeCount on the closed folder. But when I check before calling getUnreadCount if the folder is open, open the folder, getUnreadCount and close again, then the process of opening / closing the folder will take extra time and I don't get any performance gain.

  • When I open all 400 files at once and leave them open, things get slower and it is believed to be not always a good idea to always open all folders.

  • When I put the folder.getUnreadCount file in different threads (blocks of threads with size 10 or 1 thread for a folder, with 400 threads or 4 threads with 100 folders, etc.), there is no amplification and it takes longer to process all folders.

Any suggestions, ideas?

+3


source to share





All Articles