2017-03-09 13 views
0

私はオフィスのメールアカウントの受信トレイを読んでおり、添付ファイル(txtファイル)をダウンロードしています。 2 MB未満の小さなファイルではうまく動作しますが、20 MBのファイルで試してみると、実際には遅いです。私は2つの異なるマシン(LinuxとWindows)でコードをテストし、速度は同じで、本当に遅いものでした。Java Mail遅い添付ファイルをダウンロードするOffice 365

String user = "[email protected]"; 
String password = "mypassword"; 
try{ 

    boolean foundSites = false; 

    // Get a Properties object 
    Properties properties = new Properties(); 

    properties.put("mail.imaps.host", host); 
    properties.put("mail.imaps.port", port); 
    properties.put("mail.imaps.starttls.enable", SSL); 
    //properties.put("mail.imap.fetchsize", "1000000"); 
    Session emailSession = Session.getDefaultInstance(properties); 

    //create the POP3 store object and connect with the pop server 
    Store store = emailSession.getStore("imaps"); 
    store.connect(host, user, password); 

    //create the folder object and open it 
    Folder emailFolder = store.getFolder("INBOX"); 
    emailFolder.open(Folder.READ_ONLY); 

    // retrieve the messages from the folder in an array and print it 
    Message[] messages = emailFolder.getMessages(); 
    System.out.println("messages.length---" + messages.length); 

    LocalDateTime now = LocalDateTime.now(); 
    int year = now.getYear(); 
    int month = now.getMonthValue(); 
    int day = now.getDayOfMonth(); 

    for (int i = messages.length - 1; i >= 0; i--) { 
    Message message = messages[i]; 


    Calendar cal = Calendar.getInstance(); 
    cal.setTime(message.getReceivedDate()); 
    int yearMessage = cal.get(Calendar.YEAR); 
    int monthMessage = cal.get(Calendar.MONTH) + 1; 
    int dayMessage = cal.get(Calendar.DAY_OF_MONTH); 



    if(year == yearMessage && month == monthMessage && day == dayMessage) 
    { 
     //The real code is doing this with 20 Subjects (20 emails) 
     if(message.getSubject().equalsIgnoreCase("Integration - Site Info") && foundSites != true) 
     { 
      System.out.println("found Integration - Site Info"); 

      Multipart multipart = (Multipart) message.getContent(); 
      List<File> attachments = new ArrayList<>(); 
      for (int j = 0; j < multipart.getCount(); j++) { 
       BodyPart bodyPart = multipart.getBodyPart(j); 
       if(Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) 
       { 
        InputStream is = bodyPart.getInputStream(); 
        File f = new File("./attachments/" 
          + "sites_"+Integer.toString(day)+"-"+Integer.toString(month)+"-"+Integer.toString(year)+".csv"); 
        FileOutputStream fos = new FileOutputStream(f); 
        byte[] buf = new byte[4096]; 
        int bytesRead; 
        while((bytesRead = is.read(buf))!=-1) { 
         fos.write(buf, 0, bytesRead); 
        } 
        fos.close(); 
        attachments.add(f); 
        foundSites = true; 
        break; 
       } 


      } 
     } 
    } 

    if(foundSites) 
    { 
     break; 
    } 
} catch (Exception e) { 
    System.out.println(e); 
} 

私はスレッドを作成する可能性がありますが、それ以外の方法はありますか?

ちょうどメモ: 私はPythonでコードを試して、速度が大幅に向上します。

====================================== UPDATE 1:

私はsaveFileメソッドへの変更を行いました。コードは単純化されましたが、同じダウンロード速度は毎秒10 KBです。

MimeBodyPart bodyPart = (MimeBodyPart) multipart.getBodyPart(j); 
if(Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) 
{ 
    bodyPart.saveFile("./attachments/" 
     + "sites_"+Integer.toString(day)+"-"+Integer.toString(month)+"-"+Integer.toString(year)+".csv"); 

私はまた、プロファイラを使用し、結果は以下のとおりです。 enter image description here

enter image description here

+0

どれくらい遅いですか?どのくらい速いですか? – shmosel

+0

コードをプロファイリングしましたか? jvisualvmで実行し、遅い部分を教えてください。私の頭の上から、あなたは* FileOutputStreamにバッファリングを追加するかもしれません。 –

答えて

1

これらcommon mistakesを修正。

MimeBodyPart.saveFileを使用してコードを簡素化し、添付ファイルを保存します。

"imap"プロトコルではなく "imap"プロトコルを使用しているので、使用するメモリより多くのメモリを使用せずに、パフォーマンスを得るに十分な大きさにmail.imaps.fetchsizeプロパティを設定します。使用するメモリの量に気を使わず、常に十分なメモリがあると確信している場合は、mail.imaps.partialfetchをfalseに設定してください。

+0

私はsaveFileメソッドを実行しました。実際にはコードは単純化されましたが、ダウンロード速度は10 KB /秒です。 –

+0

私が提案した他の変更を試してみませんか? –

+0

私は 'mail.imaps.partialfetch'プロパティをfalseに設定し、' mail.imaps.fetchsize'を2000000に設定しました。 スピードは本当に良くなりました!ありがとう!この値を最適に計算する方法についていくつか推奨していますか? –