001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.jbi.monitoring;
018    
019    import org.apache.servicemix.jbi.monitoring.stats.CountStatisticImpl;
020    import org.apache.servicemix.jbi.monitoring.stats.StatsImpl;
021    import org.apache.servicemix.jbi.monitoring.stats.TimeStatisticImpl;
022    import org.apache.servicemix.jbi.util.IndentPrinter;
023    
024    
025    /**
026     * Basic J2EE stats for the messaging in the NMR
027     * 
028     * @version $Revision: 564607 $
029     */
030    public class MessagingStats extends StatsImpl {
031    
032        protected CountStatisticImpl inboundExchanges;
033        protected CountStatisticImpl outboundExchanges;
034        protected TimeStatisticImpl inboundExchangeRate;
035        protected TimeStatisticImpl outboundExchangeRate;
036        private String name;
037    
038        /**
039         * Default Constructor
040         * @param name
041         */
042        public MessagingStats(String name) {
043            this.name = name;
044            inboundExchanges = new CountStatisticImpl("inboundExchanges", "Number of Inbound MessageExchanges");
045            outboundExchanges = new CountStatisticImpl("outboundExchanges", "Number of Outbound MessageExchanges");
046            inboundExchangeRate = new TimeStatisticImpl("inboundExchangeRate", "time taken to process an Exchange");
047            outboundExchangeRate = new TimeStatisticImpl("outboundExchangeRate", "time taken to send an Exchange");
048            addStatistic("inboundExchanges", inboundExchanges);
049            addStatistic("outboundExchanges", outboundExchanges);
050            addStatistic("inboundExchangeRate", inboundExchangeRate);
051            addStatistic("outboundExchangeRate", outboundExchangeRate);
052        }
053        
054        /**
055         * Default Constructor
056         * @param name
057         */
058        public MessagingStats(String name, MessagingStats parent) {
059            this.name = name;
060            inboundExchanges = new CountStatisticImpl(parent.inboundExchanges, "inboundExchanges", "Number of Inbound MessageExchanges");
061            outboundExchanges = new CountStatisticImpl(parent.outboundExchanges, "outboundExchanges", "Number of Outbound MessageExchanges");
062            inboundExchangeRate = new TimeStatisticImpl(parent.inboundExchangeRate, "inboundExchangeRate", "time taken to process an Exchange");
063            outboundExchangeRate = new TimeStatisticImpl(parent.outboundExchangeRate, "outboundExchangeRate", "time taken to send an Exchange");
064            addStatistic("inboundExchanges", inboundExchanges);
065            addStatistic("outboundExchanges", outboundExchanges);
066            addStatistic("inboundExchangeRate", inboundExchangeRate);
067            addStatistic("outboundExchangeRate", outboundExchangeRate);
068        }
069        
070        /**
071         * @return Returns the name.
072         */
073        public String getName() {
074            return name;
075        }
076        /**
077         * @return Returns the inboundExchangeRate.
078         */
079        public TimeStatisticImpl getInboundExchangeRate() {
080            return inboundExchangeRate;
081        }
082        /**
083         * @return Returns the inboundExchanges.
084         */
085        public CountStatisticImpl getInboundExchanges() {
086            return inboundExchanges;
087        }
088        /**
089         * @return Returns the outboundExchangeRate.
090         */
091        public TimeStatisticImpl getOutboundExchangeRate() {
092            return outboundExchangeRate;
093        }
094        /**
095         * @return Returns the outboundExchanges.
096         */
097        public CountStatisticImpl getOutboundExchanges() {
098            return outboundExchanges;
099        }
100    
101        /**
102         * reset the Stats
103         */
104        public synchronized void reset() {
105            super.reset();
106            inboundExchanges.reset();
107            outboundExchanges.reset();
108            inboundExchangeRate.reset();
109            outboundExchangeRate.reset();
110        }
111    
112        /**
113         * @return pretty print
114         */
115        public String toString() {
116            StringBuffer buffer = new StringBuffer();
117            buffer.append("Statistics: ");
118            buffer.append(name);
119            buffer.append(" { ");
120            buffer.append(inboundExchanges);
121            buffer.append(" ");
122            buffer.append(inboundExchangeRate);
123            buffer.append(" ");
124            buffer.append(outboundExchanges);
125            buffer.append(" ");
126            buffer.append(outboundExchangeRate);
127            buffer.append(" }");
128            return buffer.toString();
129        }
130    
131        /**
132         * Dump out to an IndentPrinter
133         * 
134         * @param out
135         */
136        public void dump(IndentPrinter out) {
137            out.printIndent();
138            out.println("Statistics: ");
139            out.print(name);
140            out.println(" {");
141            out.incrementIndent();
142            out.println(inboundExchanges);
143            out.printIndent();
144            out.println(inboundExchangeRate);
145            out.printIndent();
146            out.println(outboundExchanges);
147            out.printIndent();
148            out.decrementIndent();
149            out.printIndent();
150            out.println("}");
151        }
152        
153    }