I hava a Java server application that monitors a big number of Gmail IMAP folders for new messages.
It's implemented with javamail IdleManager. I have scheduled task that works like this (shortened example)
After a number of runnings I catch a kind of deadlock. Synchronized folder.close(false) hangs forever.
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
props.put("mail.event.scope", "session"); // or "application"
props.put("mail.event.executor", es);
props.setProperty("mail.imaps.usesocketchannels", "true");
// initialized in other place.
// ArrayList<IMAPFolder> folders = new ArrayList<>();
for (IMAPFolder folder : folders) {
folder.close(false);
}
folders = new ArrayList<>();
// initialized in other place.
// ArrayList<Store> stores = new ArrayList<>();
for (Store store : stores) {
store.close();
}
stores = new ArrayList<>();
// initialized in other place.
// ExecutorService es = Executors.newCachedThreadPool();
//IdleManager idleManager = new IdleManager(session, es);
es = Executors.newCachedThreadPool();
if (idleManager != null) {
idleManager.stop();
}
idleManager = new IdleManager(session, es);
//here I run a number of threads in a loop.
Here is the simplified example of these threads.
Store store = connectStore();
IMAPFolder folder = (IMAPFolder) store.getFolder("INBOX");
stores.add(store);
folders.add(folder);
folder.open(Folder.READ_ONLY);
idleManager.watch(folder);
I'm sure that this is connected to IdleManager, because I've tried my code without idleManager.watch(folder) and everything was ok.
Btw, I've tried this approach of detecting deadlock in Java and it did not show any deadlock.
If I remove the close folder block from the code and close only the stores - the same scenario. store.close() hangs forever just like folder.close(false).
I appreciate any help! Thanks!