可以解决这个问题.为了模拟这个问题,编写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;
/**
*
title: 日志服务器
*
Description: 模拟日志服务器
*
CopyRight: CopyRight (c) 2010
*
Company: 99bill.com
*
Create date: 2010-6-18
*@author Tank Zhang
*@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;