Java即時監控記錄檔並輸出_java線程池__Java

來源:互聯網
上載者:User

引用:http://sunnylocus.iteye.com/blog/694666

最近有一個銀行資料漂白系統,要求操作人員在頁面調用遠端Linux伺服器的shell,並將shell輸出的資訊儲存到一個記錄檔,前台頁面要即時顯示記錄檔的內容.這個問題痛點在於如何判斷哪些資料是新增加的,通過查看JDK 的協助文檔, java.io.RandomAccessFile
可以解決這個問題.為了類比這個問題,編寫LogSvr和 LogView類,LogSvr不斷向mock.log記錄檔寫資料,而 LogView則即時輸出日誌變化部分的資料.

代碼1:日誌產生類 Java代碼   package com.bill99.seashell.domain.svr;      import java.io.File;   import java.io.FileWriter;   import java.io.IOException;   import java.io.Writer;   import java.text.SimpleDateFormat;   import java.util.Date;   import java.util.concurrent.Executors;   import java.util.concurrent.ScheduledExecutorService;   import java.util.concurrent.TimeUnit;   /**   *<p>title: Log Service器</p>   *<p>Description: 類比Log Service器</p>   *<p>CopyRight: CopyRight (c) 2010</p>   *<p>Company: 99bill.com</p>   *<p>Create date: 2010-6-18</P>   *@author Tank Zhang<tank.zhang@99bill.com>   *@version v0.1 2010-6-18   */   public class LogSvr {              private SimpleDateFormat dateFormat =            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");          /**       * 將資訊記錄到記錄檔       * @param logFile 記錄檔       * @param mesInfo 資訊       * @throws IOException        */       public void logMsg(File logFile,String mesInfo) throws IOException{           if(logFile == null) {               throw new IllegalStateException("logFile can not be null!");           }           Writer txtWriter = new FileWriter(logFile,true);           txtWriter.write(dateFormat.format(new Date()) +"\t"+mesInfo+"\n");           txtWriter.flush();       }              public static void main(String[] args) throws Exception{                      final LogSvr logSvr = new LogSvr();           final File tmpLogFile = new File("mock.log");           if(!tmpLogFile.exists()) {               tmpLogFile.createNewFile();           }           //啟動一個線程每5秒鐘向記錄檔寫一次資料           ScheduledExecutorService exec =                Executors.newScheduledThreadPool(1);           exec.scheduleWithFixedDelay(new Runnable(){               public void run() {                   try {                       logSvr.logMsg(tmpLogFile, " 99bill test !");                   } catch (IOException e) {                       throw new RuntimeException(e);                   }               }           }, 0, 5, TimeUnit.SECONDS);       }   }  

 代碼2:顯示日誌的類

 

  Java代碼   package com.bill99.seashell.domain.client;           import java.io.File;      import java.io.IOException;      import java.io.RandomAccessFile;      import java.util.concurrent.Executors;      import java.util.concurrent.ScheduledExecutorService;      import java.util.concurrent.TimeUnit;           public class LogView {          private long lastTimeFileSize = 0;  //上次檔案大小          /**         * 即時輸出日誌資訊         * @param logFile 記錄檔         * @throws IOException         */         public void realtimeShowLog(File logFile) throws IOException{              //指定檔案可讀可寫              final RandomAccessFile randomFile = new RandomAccessFile(logFile,"rw");              //啟動一個線程每10秒鐘讀取新增的日誌資訊              ScheduledExecutorService exec =                   Executors.newScheduledThreadPool(1);              exec.scheduleWithFixedDelay(new Runnable(){                  public void run() {                      try {                          //獲得變化部分的                          randomFile.seek(lastTimeFileSize);                          String tmp = "";                          while( (tmp = randomFile.readLine())!= null) {                              System.out.println(new String(tmp.getBytes("ISO8859-1")));                          }                          lastTimeFileSize = randomFile.length();                      } catch (IOException e) {                          throw new RuntimeException(e);                      }                  }              }, 0, 1, TimeUnit.SECONDS);          }                    public static void main(String[] args) throws Exception {              LogView view = new LogView();              final File tmpLogFile = new File("mock.log");              view.realtimeShowLog(tmpLogFile);          }           }    

 

執行LogSvr類,LogSvr類會啟動一個線程,每5秒鐘向mock.log記錄檔寫一次資料,然後再執行LogView類,LogView每隔1秒鐘讀一次,如果資料有變化則輸出變化的部分.

 

結果輸出:

2010-06-19 17:25:54  99bill test !
2010-06-19 17:25:59  99bill test !
2010-06-19 17:26:04  99bill test !
2010-06-19 17:26:09  99bill test !
2010-06-19 17:26:14  99bill test !
2010-06-19 17:26:19  99bill test !

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.