Java real-time monitoring log files and output _java thread pool __java

Source: Internet
Author: User
Tags dateformat log log

Reference: http://sunnylocus.iteye.com/blog/694666

Recently, there is a bank data bleaching system, requiring operators to call the shell of the remote Linux server on the page, and to save the shell output to a log file, the front page to display the contents of the log file in real time. The problem is how to tell which data is new, by looking at the JDK Help document, Java.io.RandomAccessFile
can solve this problem. To simulate this problem, write Logsvr and LogView classes, logsvr keep writing data to mock.log log files, and LogView will output the data of the log change part in real time.

Code 1: Log Generation class 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:  analog 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&NBsp;class logsvr {              private  SimpleDateFormat dateFormat =             new simpledateformat ("Yyyy-mm-dd hh:mm:ss");          /* *       *  record information to log file        *  @param   logfile  log files        *  @param  mesInfo  information         *  @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");       &Nbsp;   if (!tmplogfile.exists ())  {                tmplogfile.createnewfile ();            }           //start a thread to write data to the log file once every 5 seconds            ScheduledExecutorService exec =                executors.newscheduledthreadpool (1);            exec.schedulewithfixeddelay (new Runnable () {                public void run ( )  {                    try {                    &nbsP;   logsvr.logmsg (tmplogfile,  " 99bill test !");                    }  catch  (ioexception e)  {                        throw new runtimeexception (e);                    }                }            }, 0, 5, timeunit.seconds);        }  }  

Code 2: Class that displays the log

  Java code   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;  //last file size           /**         *  real-time output log information          *  @param  logFile  Log Files          *  @throws  IOException         */          public void realtimeshowlog (File logfile)  throws IOException{               //Specifies that the file can be read-writable                final RandomAccessFile randomFile =  New randomaccessfile (LogFile, "RW");               //start a thread to read the new log information every 10 seconds                ScheduledExecutorService exec =                    executors.newscheduledthreadpool (1);               exec.schedulewithfixeddelay (new Runnable () {       &Nbsp;           public void run ()  {                        try {                           //gets the change part of the                             randomfile.seek (lasttimefilesize);                           String  tmp =  "";                           while (  Tmp = randomfile.readline () ) &NBSP;{&NBSP;&NB!= null)sp;                             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);           }          }    

To perform the Logsvr class, the Logsvr class starts a thread, writes data to the Mock.log log file every 5 seconds, and then executes the LogView class, which is read every 1 seconds, and the part that outputs the change if the data changes.

Result output:

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!

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.