2014-03-25 3 views
0

자바 프로그램이 텍스트 파일의 변경 사항을 기록하고 다른 로그 파일에 기록한 후 이메일을 통해 전송하는 프로젝트에서 작업하고 있습니다. 내가 직면하고있는 문제는 모니터링 변경에 사용되는 while 루프가 중단되지 않는다는 것입니다. 루프 안에 메일 링 코드를 넣으면 메일이 무한 루프가됩니다. 루프가 실행되는 동안 코드를 외부에두면 while이 무한 루프 상태에 있기 때문에 main에 연결할 수 없습니다. 나는 휴식 조건이 필요하고 그것을 파악할 수 없습니다. 누구든지 도와 줄 수 있습니까?While 루프가 LogMonitor에 대해 java에서 무한 시간 실행되는 동안

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

은 완벽하게 작동했습니다. 엄청 고마워 :) –

관련 문제