Timer stopwatch in spring

Source: Internet
Author: User
Tags current time
1 Overview

You need to record the execution time of each task, or record a period of code execution, simply by printing the difference between the current time and the execution time, and if performing a large number of tests is cumbersome and not intuitive. If you want to control the execution time further, you need to modify it in many parts of the program. Spring-framework provides a stopwatch class that can do similar task execution time control.

stopwatch SW = new stopwatch ();
Sw.start ();
Business Operation
Sw.stop ();
Logger.info ("Time consuming:" + sw.gettotaltimemillis ());


2 match interceptors use spring stopwatch in the filter to count the execution times of each request. Add the following code to the Dofilter of filter:

Stopwatch stopwatch = new Stopwatch (Url+system.currenttimemillis ());  
Stopwatch.start ();  
Dofilter (ARG0,ARG1);  
Opwatch.stop ();  
Loginfo (Stopwatch.gettotaltimemillis () + "---" +request.getrequesturi () + "Execution Time");
As you can see from the source code constructs, stopwatch constructs the object according to the ID, ensuring that the unique construction ID distinguishes between different requests.

Public stopwatch () { 
    keeptasklist = true;  
    TaskList = new LinkedList ();  
    id = "";  
}  
  
Public stopwatch (String ID) {  
    keeptasklist = true;  
    TaskList = new LinkedList ();  
    This.id = ID;  
}

Original posted Address: http://blog.csdn.net/linfssay/article/details/7680323
http://blog.csdn.net/u012186154/article/details/54923210

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.