前言
最近有一個銀行數據漂白系統,要求操作人員在頁面調用遠端Linux服務器的shell,并將shell輸出的信息保存到一個日志文件,前臺頁面要實時顯示日志文件的內容.這個問題難點在于如何判斷哪些數據是新增加的,通過查看JDK 的幫助文檔,
java.io.RandomAccessFile
可以解決這個問題.為了模擬這個問題,編寫LogSvr和 LogView類,LogSvr不斷向mock.log日志文件寫數據,而 LogView則實時輸出日志變化部分的數據.
代碼1:日志產生類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
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<[email protected]> *@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:顯示日志的類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
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秒鐘讀一次,如果數據有變化則輸出變化的部分.
結果輸出:
1
2
3
4
5
6
|
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 ! |
PS:
代碼修改過, 有朋友下載了我的代碼,說如果是中文會亂碼,將日志輸出類的第30行的代碼 System.out.println(tmp)
改成 System.out.println(new String(tmp.getBytes("ISO8859-1")))
,就會正常顯示中文.
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:http://sunnylocus.iteye.com/blog/694666