Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
 import java.util.List;
 import java.util.Map;
The SlingRequestProgressTracker class provides the functionality to track the progress of request processing. Instances of this class are provided through the method.

The following functionality is provided:

  1. Track the progress of request processing through the log(java.lang.String) and log(java.lang.String,java.lang.Object[]) methods.
  2. Ability to measure and track processing times of parts of request processing through the startTimer(java.lang.String) and logTimer(java.lang.String) methods.
  3. Dumping the recording messages through the dump( method.
  4. Resetting the tracker through the reset() method.

Tracking Request Processing

As the request being processed, certain steps may be tracked by calling either of the log methods. A tracking entry consists of a time stamp managed by this class, and a tracking message noting the actual step being tracked.

Timing Processing Steps

Certain steps during request processing may need to be timed in that the time required for processing should be recorded. Instances of this class maintain a map of named timers. Each timer is started (initialized or reset) by calling the startTimer(java.lang.String) method. This method just records the starting time of the named timer.

To record the number of milliseconds ellapsed since a timer has been started, the logTimer(java.lang.String) method may be called. This method logs the tracking entry with message consisting of the name of the timer and the number of milliseconds ellapsed since the timer was last started. The logTimer(java.lang.String) method may be called multiple times to record several timed steps.

Additional information can be logged using the logTimer(java.lang.String,java.lang.String,java.lang.Object[]) method.

Calling the startTimer(java.lang.String) method with the name of timer which already exists, resets the start time of the named timer to the current system time.

Dumping Tracking Entries

The dump( methods writes all tracking entries to the given PrintWriter. Each entry is written on a single line consisting of the following fields:

  1. The number of milliseconds since the last reset() (or creation) of this timer.
  2. The absolute time of the timer in parenthesis.
  3. The entry message
 public class SlingRequestProgressTracker implements RequestProgressTracker {

The name of the timer tracking the processing time of the complete process.
     private static final String REQUEST_PROCESSING_TIMER = "Request Processing";

Prefix for log messages
    private static final String LOG_PREFIX = "LOG ";

Prefix for comment messages
    private static final String COMMENT_PREFIX = "COMMENT ";

TIMER_END format explanation
    private static final String TIMER_END_FORMAT = "{<elapsed msec>,<timer name>} <optional message>";

The leading millisecond number is left-padded with white-space to this width.
    private static final int PADDING_WIDTH = 7;

The system time at creation of this instance or the last reset().
    private long processingStart;

The system time when done() was called or -1 while processing is in progress.
    private long processingEnd;

The list of tracking entries.
    private final List<TrackingEntryentries = new ArrayList<TrackingEntry>();
Map of named timers indexed by timer name storing the system time of start of the respective timer.
    private final Map<StringLongnamedTimerEntries = new HashMap<StringLong>();

Creates a new request progress tracker.
    public SlingRequestProgressTracker() {

Resets this timer by removing all current entries and timers and adds an initial timer entry
    public void reset() {
        // remove all entries
        // enter initial messages
         = -1;
        .add(new TrackingEntry( + "timer_end format is " + ));

    public Iterator<StringgetMessages() {
        return new Iterator<String>() {
            private final Iterator<TrackingEntryentryIter = .iterator();
            public boolean hasNext() {
                return .hasNext();
            public String next() {
                // throws NoSuchElementException if no entries any more
                final TrackingEntry entry = .next();
                final long offset = entry.getTimeStamp() - getTimeStamp();
                return formatMessage(offsetentry.getMessage());
            public void remove() {
                throw new UnsupportedOperationException("remove");
    private String formatMessage(long offsetString message) {
        // Set exact length to avoid array copies within StringBuilder
        final StringBuilder sb = new StringBuilder( + 1 +  message.length() + 1);
        final String offsetStr = Long.toString(offset);
        for (int i =  - offsetStr.length(); i > 0; i--) {
            sb.append(' ');
        sb.append(offsetStr).append(' ').append(message).append('\n');
        return sb.toString();

Dumps the process timer entries to the given writer, one entry per line. See the class comments for the rough format of each message line.
    public void dump(final PrintWriter writer) {
            "Dumping SlingRequestProgressTracker Entries");
        final StringBuilder sb = new StringBuilder();
        final Iterator<Stringmessages = getMessages();
        while (messages.hasNext()) {

Creates an entry with the given message.
    public void log(String message) {
        .add(new TrackingEntry( + message));

Creates an entry with the given entry tag and message
    public void log(String formatObject... args) {
        String message = MessageFormat.format(formatargs);
        .add(new TrackingEntry( + message));

Starts a named timer. If a timer of the same name already exists, it is reset to the current time.
    public void startTimer(String name) {

Start the named timer and returns the start time in milliseconds. Logs a message with format
 TIMER_START{<name>} <optional message>
    private long startTimerInternal(String name) {
        long timer = System.currentTimeMillis();
        .add(new TrackingEntry(timer"TIMER_START{" + name + "}"));
        return timer;

Log a timer entry, including start, end and elapsed time.
    public void logTimer(String name) {
        if (.containsKey(name)) {

Log a timer entry, including start, end and elapsed time.
    public void logTimer(String nameString formatObject... args) {
        if (.containsKey(name)) {
            logTimerInternal(name, MessageFormat.format(formatargs), .get(name));

Log a timer entry, including start, end and elapsed time using TIMER_END_FORMAT
    private void logTimerInternal(String nameString msglong startTime) {
        final StringBuilder sb = new StringBuilder();
        sb.append(System.currentTimeMillis() - startTime);
        if(msg != null) {
            sb.append(' ');
        .add(new TrackingEntry(sb.toString()));
    public void done() {
        if( != -1) return;
         = System.currentTimeMillis();
    private long getTimeStamp() {
        return ;
    public long getDuration() {
        if ( != -1) {
            return  - ;
        return System.currentTimeMillis() - ;

Process tracker entry keeping timestamp, tag and message
    private static class TrackingEntry {
        // creation time stamp
        private final long timeStamp;
        // tracking message
        private final String message;
        TrackingEntry(String message) {
            this. = System.currentTimeMillis();
            this. = message;
        TrackingEntry(long timeStampString message) {
            this. = timeStamp;
            this. = message;
        long getTimeStamp() {
            return ;
        String getMessage() {
            return ;
New to GrepCode? Check out our FAQ X