Recently, a bank data bleaching system requires the operator to call the shell of the remote Linux server on the page and save the shell output information to a log file, the front-end page must display the log file content in real time. the difficulty lies in how to determine which data is newly added. By checking the JDK help documentation, java. io. randomAccessFile
This problem can be solved. to simulate this problem, write the LogSvr and LogView classes. LogSvr constantly writes data to the mock. log file, while LogView outputs the data in the log change part in real time.
Java code
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 server </p>
* <P> Description: Simulated log server </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 ");
/**
* Record information to a log file
* @ Param logFile: Log File
* @ 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 ();
}
// Start a thread to write data to the log file every 5 seconds.
ScheduledExecutorService exec =
Executors. newScheduledThreadPool (1 );
Exec. scheduleWithFixedDelay (new Runnable (){
Public void run (){
Try {
LogSvr. logMsg (tmpLogFile, "99 bill test! ");
} Catch (IOException e ){
Throw new RuntimeException (e );
}
}
}, 0, 5, TimeUnit. SECONDS );
}
}
Package com. bill99.seashell. domain. svr;