简体   繁体   English

如何使用多线程从Gmail服务器获取邮件?

[英]How to get mails from Gmail server using multithreading?

I have a problem with getting e-mails from Gmail server using javax.mail API. 我使用javax.mail API从Gmail服务器获取电子邮件时遇到问题。 I have to fetch 5 mails at a time by using 5 threads. 我必须使用5个线程一次获取5个邮件。 Please help me. 请帮我。

folder = (IMAPFolder) store.getFolder(m_StrfolderName);
folder.open(Folder.READ_WRITE);
Message []messages = folder.getMessages(); 

Instead of doing this: 而不是这样做:

Message[] messages = folder.getMessages(); 

You could (in theory) do this: 您可以(理论上)这样做:

final int count = folder.getMessageCount();
// in multiple threads
for (int i /* in a subset of [0 .. count - 1] */) {
    Message message = folder.getMessage(i);
    // process it
}

However, I don't think this is going to fetch messages in parallel. 但是,我认为这不会并行获取消息。 The problem is that when getMessage(int) is talking to the IMAP server, it holds a local lock (the cache lock) on the folder. 问题在于,当getMessage(int)与IMAP服务器通信时,它将在文件夹上持有本地锁(缓存锁)。 This effectively means that messages will be fetched one at a time. 这实际上意味着将一次提取一条消息。

I guess you could attempt to open multiple IMAP sessions to your mailbox, but I suspect that the remote IMAP server (gmail) won't let you do that. 我猜您可以尝试打开到邮箱的多个IMAP会话,但是我怀疑远程IMAP服务器(gmail)不允许您这样做。

But here's a question for you. 但这是一个问题。 Do actually need to fetch the email messages in parallel, or would processing them in parallel be sufficient? 实际需要并行获取电子邮件,还是并行处理就足够了? (Where is the bottleneck in your code? Fetching or processing?) (代码的瓶颈在哪里?获取还是处理?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM