com.googlecode.msidor.springframework.integration.history.MessageHistoryParser.java Source code

Java tutorial

Introduction

Here is the source code for com.googlecode.msidor.springframework.integration.history.MessageHistoryParser.java

Source

/*
Copyright 2015 Maciej SIDOR [maciejsidor@gmail.com]
    
The source code is licensed 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
    
http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.   
 */
package com.googlecode.msidor.springframework.integration.history;

import java.util.Iterator;
import java.util.Properties;

import org.springframework.integration.history.MessageHistory;
import org.springframework.messaging.MessageHeaders;

/**
 * Parse message history generated by spring integration and produce output with components names and components processing times.<br/>
 * This component requires the spring integration history to be activated. To do so use 
 * <code>
 *       &lt;int:message-history/ &gt;       
 * </code>
 *
 * @author Maciej SIDOR (maciejsidor@gmail.com)
 * @since 2015
 */

public class MessageHistoryParser {

    /**
     * Parse message history generated by spring integration and produce output with components names and components processing times
     * 
     * @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();
    }
}