Example usage for org.apache.commons.lang3.time StopWatch getStartTime

List of usage examples for org.apache.commons.lang3.time StopWatch getStartTime

Introduction

In this page you can find the example usage for org.apache.commons.lang3.time StopWatch getStartTime.

Prototype

public long getStartTime() 

Source Link

Document

Returns the time this stopwatch was started.

Usage

From source file:org.alfresco.bm.event.EventWork.java

@Override
public void run() {
    // Set the start and end times for the event
    long warnDelay = processor.getWarnDelay();
    boolean chart = processor.isChart();

    EventResult result = null;//from www  .  ja va 2s  .  c  o m
    StopWatch stopWatch = new StopWatch();
    try {
        // Process the event
        result = processor.processEvent(event, stopWatch);
        if (result == null) {
            String msg = "Event processtor returned null result: " + processor;
            logService.log(LogLevel.FATAL, msg);
            throw new RuntimeException(msg);
        }
    } catch (Throwable e) {
        DateTime eventTime = new DateTime(stopWatch.getStartTime());
        String stack = ExceptionUtils.getStackTrace(e);
        String error = "[" + eventTime
                + "] Event processing exception; no further events will be published. \r\n" + stack;
        result = new EventResult(error, Collections.<Event>emptyList(), false);
        // Close any associated session
        String sessionId = event.getSessionId();
        if (sessionId != null && processor.isAutoCloseSessionId()) {
            sessionService.endSession(sessionId);
        }
    }
    // See how long it took
    long before = stopWatch.getStartTime();
    long time = stopWatch.getTime();

    // Get any supplemental data to be recorded
    Object data = result.getData();
    // Get the next events to publish
    List<Event> nextEvents = result.getNextEvents();
    // Was it successful?
    boolean wasSuccess = result.isSuccess();
    // Construct the recorded event
    EventRecord recordedEvent = new EventRecord(driverId, wasSuccess, before, time, data, event);
    recordedEvent.setChart(chart);
    recordedEvent.setProcessedBy(processor.getName());

    // Check the time taken against the time allowed
    if (time > warnDelay) {
        String msg = "Event processing exceeded warning threshold by " + (time - warnDelay) + "ms.";
        recordedEvent.setWarning(msg);
    }

    if (logger.isDebugEnabled()) {
        logger.debug("\n" + "Event processing completed: \n" + "   Driver:    " + driverId + "\n"
                + "   Test Run:  " + testRunFqn + "\n" + "   Event:     " + event + "\n" + "   Time:      "
                + time + "\n" + "   Processor: " + processor);
    }

    // Record the event
    try {
        resultService.recordResult(recordedEvent);
    } catch (Throwable e) {
        String stack = ExceptionUtils.getStackTrace(e);
        logService.log(LogLevel.ERROR, "Failed to record an result " + recordedEvent + ": " + stack);
        logger.error("Failed recorded event: " + recordedEvent, e);
    }

    // Pass the event(s) through the producers
    Set<String> eventNamesSeen = new HashSet<>(nextEvents.size() + 17);
    nextEvents = getNextEvents(nextEvents, eventNamesSeen);

    // Only propagate session IDs automatically if there is a 1:1 relationship between the event processed
    // and the next event i.e. we branching of the session is not intrinsically supported
    String sessionId = event.getSessionId();
    boolean propagateSessionId = (sessionId != null) && processor.isAutoPropagateSessionId();
    if (sessionId != null && nextEvents.size() != 1 && processor.isAutoCloseSessionId()) {
        // No further events and we have to auto-close
        sessionService.endSession(sessionId);
        propagateSessionId = false;
    }

    // Use weightings (https://github.com/AlfrescoBenchmark/alfresco-benchmark/issues/54)
    RandomWeightedSelector<String> driverSelector = new RandomWeightedSelector<String>();
    for (String driverId : driverIds) {
        driverSelector.add(100, driverId);
    }

    // Publish the next events
    for (Event nextEvent : nextEvents) {
        if (nextEvent == null) {
            // Ignore it but log the error
            String msg = "Null event in list of next events: \n" + "   Driver:    " + driverId + "\n"
                    + "   Test Run:  " + testRunFqn + "\n" + "   Event:     " + event + "\n" + "   Time:      "
                    + time + "\n" + "   Processor: " + processor;
            logger.error("\n" + msg);
            logService.log(LogLevel.WARN, msg);
            continue;
        }
        // Carry over the session ID, if required
        if (propagateSessionId) {
            nextEvent.setSessionId(sessionId);
        }

        // Randomly distribute the event execution across the drivers, unless there is implied
        // data afinity, in which case use this driver instance
        if (!nextEvent.getDataInMemory()) {
            // The data is not held in memory, so we can assign the event to any server
            String driverIdForNextEvent = driverSelector.next();
            nextEvent.setDriver(driverIdForNextEvent);
        }

        // Persist the event
        try {
            eventService.putEvent(nextEvent);
        } catch (Throwable e) {
            String stack = ExceptionUtils.getStackTrace(e);
            String msg = "Failed to insert event into queue: \n" + "  Event to insert:     " + nextEvent + "\n"
                    + "  Inbound event:       " + event + "\n" + "  Process used:        " + processor + "\n"
                    + "  Events produced:     " + eventNamesSeen;
            logService.log(LogLevel.ERROR, msg + "\n" + stack);
            logger.error(msg, e);
        }
    }

    // Remove the event from the queue.
    try {
        boolean deleted = eventService.deleteEvent(event);
        if (!deleted) {
            String msg = "Event was not deleted from the queue: " + event;
            logger.error(msg);
            logService.log(LogLevel.ERROR, msg);
        }
    } catch (Throwable e) {
        String stack = ExceptionUtils.getStackTrace(e);
        String msg = "Failed to remove event from the queue: " + event;
        logger.error(msg, e);
        logService.log(LogLevel.ERROR, msg + "\n" + stack);
    }
}