Wednesday, October 31, 2012

Multithreaded Download mail from gmail server in eml format


am find lot of example to download mail from gmail server using imap protocal , but those are single thread that's takes more time , am modify those code and create this to increase time optimization

take look over the code and leave comment to make more efficient


package helper;


import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.search.AndTerm;
import javax.mail.search.BodyTerm;
import javax.mail.search.FlagTerm;


public class multithread {

 /**
  * @param args
  */

 private static Object bb = new Object();
 private static Message[] msgs;
 private static int completed = 0;
 public static long start;
 public static void main(String args[]) throws Exception {

  msgs = null;
  Store store = null;
  Folder folder = null;
  try {
   start = System.currentTimeMillis();
  Properties props = System.getProperties();
  props.setProperty("mail.store.protocol", "imaps");
  Session session = Session.getDefaultInstance(props, null);
  session.setDebug(true);
  store = session.getStore("imaps");
  store.connect("imap.gmail.com", "--gmail id--", "--password--");
  folder = store.getDefaultFolder();
  if (folder == null) {
   throw new Exception("No default folder");
  }
  folder = folder.getFolder("INBOX");
  if (folder == null) {
   throw new Exception("No IMAP INBOX");
  }
  folder.open(Folder.READ_WRITE);

  //unread mail flag
  FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
  BodyTerm bt =  new BodyTerm("Sales"); // that keyword which you want to search in mail 
  msgs = folder.search(new AndTerm(bt, ft));
  if(msgs.length!=0){
  ThreadPoolExecutor executorService = (ThreadPoolExecutor) Executors.newFixedThreadPool(msgs.length);
  executorService.prestartAllCoreThreads();
  for(Message m : msgs){
   executorService.execute(new Runs(m));
  }
  executorService.shutdown();
        executorService.awaitTermination(10000, TimeUnit.SECONDS);
  }
  } catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
  } finally {
   int i=0;
   while(msgs.length!=completed){
    try{
     synchronized (bb) {
      System.out.println("wait for 300 millisecond "+i);
      bb.wait(300);
      i++;
     }
    }catch (Exception e) {
     // TODO: handle exception
     e.printStackTrace();
    }
   }
   if (folder != null) {
    try {
     folder.close(true);
    } catch (MessagingException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   if (store != null) {
    try {
     store.close();
    } catch (MessagingException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
    long end = System.currentTimeMillis() - start;
       System.out.println("--- Done: , end: " + end + "ms");
  }
  
 }

 private static class Runs implements Runnable{

  private Message file;
  
  public Runs(Message m) {
   this.file = m;
  }
  @Override
  public void run() {
   // TODO Auto-generated method stub
   try{
   System.out.println("downloading startsssss ");
   File emlFile = new File("C:/Documents and Settings/india/Desktop/emlss/msg"+file.getHeader("message-id")[0].replace("<", "").replace(">", "")+".eml");
   System.out.println(emlFile.getAbsolutePath());
   emlFile.createNewFile();
   file.writeTo(new FileOutputStream(emlFile));
   completed++;
   synchronized (bb) {
    bb.notifyAll();
   }
   }catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
   }
  }
  
 }
}


No comments:

Post a Comment