2014-03-25 40 views
0

我正在開發一個項目,其中一個java程序記錄對文本文件所做的更改,將其寫入其他日誌文件並通過電子郵件發送。我面臨的問題是用於監視更改的while循環沒有中斷。 如果我把郵件裏面的代碼放在while循環中,郵件會進入無限循環。 如果我在while循環外面放置代碼,main無法到達那裏,因爲處於無限循環。我需要一個休息條件,我不能弄明白。誰能幫忙?while循環在java中執行無限次LogMonitor

import java.util.Properties; 
import java.io.*; 
import javax.mail.*; 
import javax.mail.internet.*; 
import javax.activation.*; 

public class LogMonitor { 
    public static void main(String[] args) throws Exception 
{ 
     FileReader fr = new FileReader("D:/test.txt"); 
     BufferedReader br = new BufferedReader(fr); 
     while (true) { 
      String line = br.readLine(); 
      if (line == null) 
      { 
       Thread.sleep(1*1000); 
      } else 
      { 
       byte[] y = line.getBytes(); 
       File g = new File("D:/abc.txt"); 
       try (OutputStream f = new FileOutputStream(g,true)) 
       { 
        f.write(y); 
       } 
      }  

String to="[email protected]";//change accordingly 
    final String user="[email protected]";//change accordingly 
    final String password="xxxxxxx";//change accordingly 
// final String d_port = "465"; 

    //1) get the session object 
    // java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); 
    Properties props = new Properties(); 
       props.put("mail.smtp.host", "smtp.gmail.com"); 
       props.put("mail.smtp.socketFactory.port", "465"); 
       props.put("mail.smtp.socketFactory.class", 
           "javax.net.ssl.SSLSocketFactory"); 
       props.put("mail.smtp.auth", "true"); 
       props.put("mail.smtp.port", "465"); 

    Session session = Session.getDefaultInstance(props, 
    new javax.mail.Authenticator() 
    { 
    @Override 
    protected PasswordAuthentication getPasswordAuthentication() 
    { 
    return new PasswordAuthentication(user,password); 
    } 
    }); 

    //2) compose message  
    try{ 
    MimeMessage message = new MimeMessage(session); 
    message.setFrom(new InternetAddress(user)); 
    message.addRecipient(Message.RecipientType.TO,new InternetAddress(to)); 
    message.setSubject("Message Alert! Changes made to your file"); 

    //3) create MimeBodyPart object and set your message text  
    BodyPart messageBodyPart1 = new MimeBodyPart(); 
    messageBodyPart1.setText("This is message body"); 

    //4) create new MimeBodyPart object and set DataHandler object to this object  
    MimeBodyPart messageBodyPart2 = new MimeBodyPart(); 

String filename = "D://abc.txt";//change accordingly 
    DataSource source = new FileDataSource(filename); 
    messageBodyPart2.setDataHandler(new DataHandler(source)); 
    messageBodyPart2.setFileName(filename); 


    //5) create Multipart object and add MimeBodyPart objects to this object  
    Multipart multipart = new MimeMultipart(); 
    multipart.addBodyPart(messageBodyPart1); 
    multipart.addBodyPart(messageBodyPart2); 

    //6) set the multiplart object to the message object 
    message.setContent(multipart); 

    //7) send message 
    Transport.send(message); 

    System.out.println("message sent...."); 
    }catch (MessagingException ex) { 
     System.out.println(ex); 


    } 
     } 
}} 
+0

你想發送郵件只有當有變化? – Mani

回答

0

首先移動發送電子郵件ail塊到單獨的方法。 我只是剪切和粘貼你的代碼..

public static void sendEmail() { 
    String to = "[email protected]";// change accordingly 
    final String user = "[email protected]";// change accordingly 
    final String password = "xxxxxxx";// change accordingly 
    // final String d_port = "465"; 

    // 1) get the session object 
    // java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); 
    Properties props = new Properties(); 
    props.put("mail.smtp.host", "smtp.gmail.com"); 
    props.put("mail.smtp.socketFactory.port", "465"); 
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.port", "465"); 

    Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { 
     @Override 
     protected PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication(user, password); 
     } 
    }); 

    // 2) compose message 
    try { 
     MimeMessage message = new MimeMessage(session); 
     message.setFrom(new InternetAddress(user)); 
     message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 
     message.setSubject("Message Alert! Changes made to your file"); 

     // 3) create MimeBodyPart object and set your message text 
     BodyPart messageBodyPart1 = new MimeBodyPart(); 
     messageBodyPart1.setText("This is message body"); 

     // 4) create new MimeBodyPart object and set DataHandler object to this object 
     MimeBodyPart messageBodyPart2 = new MimeBodyPart(); 

     String filename = "D://abc.txt";// change accordingly 
     DataSource source = new FileDataSource(filename); 
     messageBodyPart2.setDataHandler(new DataHandler(source)); 
     messageBodyPart2.setFileName(filename); 

     // 5) create Multipart object and add MimeBodyPart objects to this object 
     Multipart multipart = new MimeMultipart(); 
     multipart.addBodyPart(messageBodyPart1); 
     multipart.addBodyPart(messageBodyPart2); 

     // 6) set the multiplart object to the message object 
     message.setContent(multipart); 

     // 7) send message 
     Transport.send(message); 

     System.out.println("message sent...."); 
    } 
    catch (MessagingException ex) { 
     System.out.println(ex); 

    } 

} 

然後加入布爾,以紀念在主要變化如.. 注isFileChanged用於發送電子郵件捕獲的變化。

public static void main(String[] args) throws Exception { 
    FileReader fr = new FileReader("D:/test.txt"); 
    BufferedReader br = new BufferedReader(fr); 
    boolean isFileChanged = false; 
    while (true) { 
     String line = br.readLine(); 
     if (line == null) { 
      if (isFileChanged){ 
       isFileChanged = false; 
       sendEmail(); 
      } 
      Thread.sleep(1 * 1000); 
     } 
     else { 
      isFileChanged = true; 
      byte[] y = line.getBytes(); 
      File g = new File("D:/abc.txt"); 
      try (OutputStream f = new FileOutputStream(g, true)) { 
       f.write(y); 
      } 
     } 

    } 
} 
+0

完美運作。萬分感謝 :) –