All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.spardat.xma.monitoring.client.ClientTimingEvent Maven / Gradle / Ivy

There is a newer version: 6.0.2
Show newest version
/*******************************************************************************
 *
 * @(#) $Id: ClientTimingEvent.java 2641 2008-08-25 07:10:22Z webok $
 *
 * Copyright (c) 2003, 2008 s IT Solutions AT Spardat GmbH .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 *******************************************************************************/

package at.spardat.xma.monitoring.client;

import java.io.IOException;

import at.spardat.xma.serializer.XmaInput;
import at.spardat.xma.serializer.XmaOutput;

/**
 * Instances of this class can be used to measure timing events at the client side.
 * Timing events reported by this class are transmitted to the server side (with the next RPC after event completion).
 * At the server side the events are handed to the server side monitoring plugin as it is configured in the xma-app.xml.
 *
 * Usage:
 * At the beginning of the measurment create a new ClientTimingEvent with a variable name.
 * End the duration of the measurement by calls to success() or failure() (in case of an exception).
 * Alternativly you can report variable names and values by the static sucess(String variable, int value)/failure(String variable, int value) methods.
   
   ClientTimingEvent event = new ClientTimingEvent("myvariable");
   try {
      //do something
      event.success(); //end event, record success
    } finally { //end event, record failure: only the first call to an event is recorded
      event.failure();
   }
  
 *
 * @author s3460
 * @since 2.0.4
 */
public class ClientTimingEvent {

    private transient long  startTime = System.currentTimeMillis();

    private transient boolean finished = false;

    private String varName;

    private int value = -1;

    private boolean success;


    public ClientTimingEvent(String varName) {
        super();
        this.varName = varName;
    }

    /**
     *
     * @return the variable name
     * @since version_number
     * @author s3460
     */
    public String getVarName() {
        return varName;
    }

    /**
     *
     * @return the value, usually the duration of the event
     * @since version_number
     * @author s3460
     */
    public int getValue() {
        return this.value;
    }

    /**
     *
     * @return the success state as indicated by success()/failure calls.
     * @since version_number
     * @author s3460
     */
    public boolean isSuccess() {
        return success;
    }

    /**
     * sets the value to the currrent event duration and ends the event.
     * @param success
     * @return true if was the first call to endTiming() (subsequent calls are ignored)
     * @since version_number
     * @author s3460
     */
    private boolean endTiming(boolean success){
        return reportValue((int) (System.currentTimeMillis() - startTime), success);
    }

    /**
     * sets the value to 'value' and ends the event.
     * @param value
     * @param success
     * @return true if was the first call to reportValue() (subsequent calls are ignored)
     * @since version_number
     * @author s3460
     */
    private boolean reportValue(int value, boolean success) {
        if(!finished){
            this.value = value;
            this.success = success;
            finished = true;
            TimingEventListClient.add(this);

            return true;
        }else{
            return false;
        }
    }

    /**
     * Creates a event, sets the value to 'value' and ends the event.
     * @param varName
     * @param value
     * @param success
     * @since version_number
     * @author s3460
     */
    private static void reportValue(String varName, int value, boolean success) {
        ClientTimingEvent event = new ClientTimingEvent(varName);
        event.reportValue(value, success);
    }


    /**
     * Ends the measurement with a success status.
     * @return true if the measurement was actually ended otherwise false (as only the first call to success()/failure() ends the measurment)
     * @since version_number
     * @author s3460
     */
    public boolean success () {
        return endTiming(true);
    }

    /**
     * Ends the measusuremnt with a failure status.
     * This is used to indicate exceptions.
     * @return true if the measurement was actually ended otherwise false (as only the first call to success()/failure() ends the measurment)
     * @since version_number
     * @author s3460
     */
    public boolean failure () {
        return endTiming(false);
    }

    /**
     * Reports observations to the monitoring system.
     * It is reported with a success status.
     * @param variable the name of the parameter to report
     * @param value the value of the parameter to report.
     */
    public static void success (String variable, int value) {
        reportValue(variable, value, true);
    }

    /**
     * Reports observations to the monitoring system.
     * It is reprorted with a failure status.
     * @param variable the name of the parameter to report
     * @param value the value of the parameter to report.
     */
    public static void failure (String variable, int value) {
        reportValue(variable, value, true);
    }

    /**
     * Writes the state of this instance to XmaOutput.
     * For internal XMA use only.
     * @param o
     * @throws IOException
     * @since version_number
     * @author s3460
     */
    public void externalize (XmaOutput o) throws IOException {
        o.writeString("varName", this.varName);
        o.writeInt("value", this.value);
        o.writeBoolean("success", this.success);
    }

    /**
     * Reads the state of an instance from XmaInput.
     * For internal XMA use only.
     * @param in
     * @return
     * @throws IOException
     * @since version_number
     * @author s3460
     */
    public boolean internalize (XmaInput in) throws IOException {
        this.varName = in.readString();
        this.value = in.readInt();
        this.success = in.readBoolean();

        return true;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy