2025-02-10

        最近有一個銀行數據漂白系統,要求操作人員在頁面調用遠端Linux服務器的shell,並將shell輸出的信息保存到一個日志文件,前臺頁面要實時顯示日志文件的內容.這個問題難點在於如何判斷哪些數據是新增加的,通過查看JDK 的幫助文檔, java.io.RandomAccessFile
可以解決這個問題.為瞭模擬這個問題,編寫LogSvr和 LogView類,LogSvr不斷向mock.log日志文件寫數據,而 LogView則實時輸出日志變化部分的數據.

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: 日志服務器</p>
 *<p>Description: 模擬日志服務器</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()) ” ” mesInfo ” “);  
        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);  
    }  
}
package com.bill99.seashell.domain.svr;

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *