Example usage for org.springframework.integration.history MessageHistory iterator

List of usage examples for org.springframework.integration.history MessageHistory iterator

Introduction

In this page you can find the example usage for org.springframework.integration.history MessageHistory iterator.

Prototype

@Override
    public Iterator<Properties> iterator() 

Source Link

Usage

From source file:com.googlecode.msidor.springframework.integration.history.MessageHistoryParser.java

/**
 * Parse message history generated by spring integration and produce output with components names and components processing times
 * //from  w w w .  j a v  a2  s.c o  m
 * @param mh message headers to be parsed
 * @return output with components names and components processing times
 */
public static String parse(MessageHeaders mh) {
    StringBuilder sb = new StringBuilder();

    //get message history
    MessageHistory history = mh.get(MessageHistory.HEADER_NAME, MessageHistory.class);
    String[] names = new String[history.size()];
    long[] times = new long[history.size()];

    //go thought all history entries
    Iterator<Properties> historyIterator = history.iterator();
    int i = 0;
    while (historyIterator.hasNext()) {
        Properties gatewayHistory = historyIterator.next();
        String name = gatewayHistory.getProperty("name");
        String historyTimestampStr = gatewayHistory.getProperty("timestamp");
        Long historyTimestamp = Long.parseLong(historyTimestampStr);

        names[i] = name;
        times[i++] = historyTimestamp;
    }

    //calculates the time deltas between components 
    final long lastTimestamp = mh.getTimestamp();
    for (int j = 0; j < names.length; j++) {
        if (j > 0) {
            sb.append("; ");
        }

        if (j + 1 < names.length)
            sb.append(names[j]).append("=").append(times[j + 1] - times[j]);
        else
            sb.append(names[j]).append("=").append(lastTimestamp - times[j]);
    }

    if (sb.length() > 0) {
        sb.append("; ");
    }

    sb.append("total=").append(lastTimestamp - times[0]);

    return sb.toString();
}