Javamail的性能 [英] Javamail performance

查看:97
本文介绍了Javamail的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直使用javamail从IMAP服务器(目前是GMail)检索邮件。 Javamail从服务器中快速地检索特定文件夹中的消息列表(仅ids),但是当我实际获取消息(仅包含内容)时,每条消息大约需要1到2秒。什么技术应该用于快速检索?

I've been using javamail to retrieve mails from IMAP server (currently GMail). Javamail retrieves list of messages (only ids) in a particular folder from server very fast, but when I actually fetch message (only envelop not even contents) it takes around 1 to 2 seconds for each message. What are the techniques should be used for fast retrieval?

这里是我的代码:

here is my code:

    try {
        IMAPStore store = null;
        if(store!=null&&store.isConnected())return;
        Properties props = System.getProperties();
        Session sessionIMAP = Session.getInstance(props, null);
        try {
            store = (IMAPStore) sessionIMAP.getStore("imaps");
            store.connect("imap.gmail.com",993,"username@gmail.com","password");
        } catch (Exception e) {
            e.printStackTrace();
        }

        IMAPFolder folder = (IMAPFolder) store.getFolder("INBOX");
        folder.open(Folder.READ_ONLY);
        System.out.println("start");
        Message[] msgs = folder.getMessages(1,10);
        long ftime = System.currentTimeMillis();
        FetchProfile fp=new FetchProfile();
        fp.add(FetchProfile.Item.ENVELOPE);
        folder.fetch(msgs, fp);
        long time = System.currentTimeMillis();
        System.out.println("fetch: "+(time-ftime));
        for (Message message : msgs) {
            System.out.println(message.getSubject());
            Address[] from = message.getFrom();
            for (Address address : from) {
                System.out.println(address);
            }
            Address[] recipients = message.getAllRecipients();
            for (Address address : recipients) {
                System.out.println(address);
            }

        }
        long newTime = System.currentTimeMillis();
        System.out.println("convert: "+(newTime-time));
    }catch (Exception e) {
        e.printStackTrace();
    }


}


推荐答案

我相信Gmail每隔一秒左右就会将IMAP邮件读取次数限制为一次。您可以通过多个IMAP连接来加速它。

I believe that Gmail throttles the IMAP message reads to one every second or so. You might be able to speed it up with multiple IMAP connections.

这篇关于Javamail的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆