001    /** 
002     * 
003     * Copyright 2004 Protique Ltd
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * 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     **/
018    package org.activemq.management;
019    
020    import org.activemq.util.IndentPrinter;
021    import org.apache.commons.logging.Log;
022    import org.apache.commons.logging.LogFactory;
023    
024    import javax.jms.Destination;
025    import javax.jms.Message;
026    import javax.jms.MessageConsumer;
027    import javax.jms.MessageProducer;
028    import javax.jms.Session;
029    
030    /**
031     * Statistics for a JMS endpoint, typically a MessageProducer or MessageConsumer
032     * but this class can also be used to represent statistics on a {@link Destination} as well.
033     *
034     * @version $Revision: 1.1.1.1 $
035     */
036    public class JMSEndpointStatsImpl extends StatsImpl {
037        private static final Log log = LogFactory.getLog(JMSEndpointStatsImpl.class);
038    
039        protected CountStatisticImpl messageCount;
040        protected CountStatisticImpl pendingMessageCount;
041        protected CountStatisticImpl expiredMessageCount;
042        protected TimeStatisticImpl messageWaitTime;
043        protected TimeStatisticImpl messageRateTime;
044    
045        /**
046         * This constructor is used to create statistics for a
047         * {@link MessageProducer} or {@link MessageConsumer} as it passes in a
048         * {@link Session} parent statistic.
049         *
050         * @param sessionStats
051         */
052        public JMSEndpointStatsImpl(JMSSessionStatsImpl sessionStats) {
053            this();
054            setParent(messageCount, sessionStats.getMessageCount());
055            setParent(pendingMessageCount, sessionStats.getPendingMessageCount());
056            setParent(expiredMessageCount, sessionStats.getExpiredMessageCount());
057            setParent(messageWaitTime, sessionStats.getMessageWaitTime());
058            setParent(messageRateTime, sessionStats.getMessageRateTime());
059        }
060    
061        /**
062         * This constructor is typically used to create a statistics object for a
063         * {@link Destination}
064         */
065        public JMSEndpointStatsImpl() {
066            this(new CountStatisticImpl("messageCount", "Number of messages processed"),
067                    new CountStatisticImpl("pendingMessageCount", "Number of pending messages"),
068                    new CountStatisticImpl("expiredMessageCount", "Number of expired messages"),
069                    new TimeStatisticImpl("messageWaitTime", "Time spent by a message before being delivered"),
070                    new TimeStatisticImpl("messageRateTime", "Time taken to process a message (thoughtput rate)"));
071        }
072    
073        public JMSEndpointStatsImpl(CountStatisticImpl messageCount, CountStatisticImpl pendingMessageCount, CountStatisticImpl expiredMessageCount, TimeStatisticImpl messageWaitTime, TimeStatisticImpl messageRateTime) {
074            this.messageCount = messageCount;
075            this.pendingMessageCount = pendingMessageCount;
076            this.expiredMessageCount = expiredMessageCount;
077            this.messageWaitTime = messageWaitTime;
078            this.messageRateTime = messageRateTime;
079    
080            // lets add named stats
081            addStatistic("messageCount", messageCount);
082            addStatistic("pendingMessageCount", pendingMessageCount);
083            addStatistic("expiredMessageCount", expiredMessageCount);
084            addStatistic("messageWaitTime", messageWaitTime);
085            addStatistic("messageRateTime", messageRateTime);
086        }
087    
088        public synchronized void reset() {
089            super.reset();
090            messageCount.reset();
091            messageRateTime.reset();
092            pendingMessageCount.reset();
093            expiredMessageCount.reset();
094            messageWaitTime.reset();
095        }
096    
097        public CountStatisticImpl getMessageCount() {
098            return messageCount;
099        }
100    
101        public CountStatisticImpl getPendingMessageCount() {
102            return pendingMessageCount;
103        }
104    
105        public CountStatisticImpl getExpiredMessageCount() {
106            return expiredMessageCount;
107        }
108    
109        public TimeStatisticImpl getMessageRateTime() {
110            return messageRateTime;
111        }
112    
113        public TimeStatisticImpl getMessageWaitTime() {
114            return messageWaitTime;
115        }
116    
117        public String toString() {
118            StringBuffer buffer = new StringBuffer();
119            buffer.append(messageCount);
120            buffer.append(" ");
121            buffer.append(messageRateTime);
122            buffer.append(" ");
123            buffer.append(pendingMessageCount);
124            buffer.append(" ");
125            buffer.append(expiredMessageCount);
126            buffer.append(" ");
127            buffer.append(messageWaitTime);
128            return buffer.toString();
129        }
130    
131        public void onMessage(Message message) {
132            long start = messageCount.getLastSampleTime();
133            messageCount.increment();
134            long end = messageCount.getLastSampleTime();
135            messageRateTime.addTime(end - start);
136        }
137    
138        public void dump(IndentPrinter out) {
139            out.printIndent();
140            out.println(messageCount);
141            out.printIndent();
142            out.println(messageRateTime);
143            out.printIndent();
144            out.println(pendingMessageCount);
145            out.printIndent();
146            out.println(messageRateTime);
147            out.printIndent();
148            out.println(expiredMessageCount);
149            out.printIndent();
150            out.println(messageWaitTime);
151        }
152    
153        // Implementation methods
154        //-------------------------------------------------------------------------
155        protected void setParent(CountStatisticImpl child, CountStatisticImpl parent) {
156            if (child instanceof CountStatisticImpl && parent instanceof CountStatisticImpl) {
157                CountStatisticImpl c = (CountStatisticImpl) child;
158                c.setParent((CountStatisticImpl) parent);
159            }
160            else {
161                log.warn("Cannot associate endpoint counters with session level counters as they are not both CountStatisticImpl clases. Endpoint: " + child + " session: " + parent);
162            }
163        }
164    
165        protected void setParent(TimeStatisticImpl child, TimeStatisticImpl parent) {
166            if (child instanceof TimeStatisticImpl && parent instanceof TimeStatisticImpl) {
167                TimeStatisticImpl c = (TimeStatisticImpl) child;
168                c.setParent((TimeStatisticImpl) parent);
169            }
170            else {
171                log.warn("Cannot associate endpoint counters with session level counters as they are not both TimeStatisticImpl clases. Endpoint: " + child + " session: " + parent);
172            }
173        }
174    }