Java programs monitor log files in real time and output result code

Source: Internet
Author: User

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;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.