001    /*
002     * Copyright (C) The Apache Software Foundation. All rights reserved.
003     *
004     * This software is published under the terms of the Apache Software License
005     * version 1.1, a copy of which has been included with this distribution in
006     * the LICENSE file.
007     * 
008     * $Id: StopWatchMessageListener.java 155459 2005-02-26 13:24:44Z dirkv $
009     */
010    
011    package org.apache.commons.messenger.tool;
012    
013    import javax.jms.Destination;
014    import javax.jms.JMSException;
015    import javax.jms.Message;
016    import javax.jms.MessageListener;
017    
018    import org.apache.commons.logging.Log;
019    import org.apache.commons.logging.LogFactory;
020    
021    /**
022     * A simple StopWatch Message Listener for wrapping another MessageListener
023     * to determine its performance.
024     *
025     * @author  James Strachan
026     */
027    public class StopWatchMessageListener implements MessageListener {
028    
029        /** The Log to which logging calls will be made. */
030        private Log log = LogFactory.getLog( StopWatchMessageListener.class );
031        
032        /** the underlying MessageListener */
033        private MessageListener messageListener;
034    
035        /** the number of messages processed */
036        private int count;
037            
038        /** the message group size */
039        private int groupSize = 1000;
040            
041        /** the time that the batch started processing */
042        private long startTime;
043        
044        public StopWatchMessageListener() {
045        }
046        
047        public StopWatchMessageListener(MessageListener messageListener) {
048            this.messageListener = messageListener;
049        }
050        
051        // MessageListener interface
052        //-------------------------------------------------------------------------                    
053        public void onMessage(Message message) {
054            if ( count == 0 ) {
055                startTime = System.currentTimeMillis();
056            }
057            if ( messageListener != null ) {
058                messageListener.onMessage(message);
059            }
060        
061            if ( ++count == groupSize ) {
062                long elapsed = System.currentTimeMillis() - startTime;            
063                double timePerMessage = elapsed;
064                timePerMessage /= count;
065                
066                double messagesPerSecond = 1000; 
067                messagesPerSecond /= timePerMessage;
068                
069                Destination destination = null;
070                try {
071                    destination = message.getJMSDestination();
072                }
073                catch (JMSException e) {
074                    // ignore
075                }            
076                log.info( "Time to process " + count + " messages: " + elapsed + " millis on: " + destination );
077                log.info( "Average number of messages per second: " + messagesPerSecond );
078                count = 0;
079            }
080        }
081    
082        
083        // Properties
084        //-------------------------------------------------------------------------                
085        
086        /**
087         * @return the number of messages in the group before the performance statistics are logged
088         */
089        public int getGroupSize() {
090            return groupSize;
091        }    
092            
093        /**
094         * Sets the number of messages in the group before the performance statistics are logged
095         */
096        public void setGroupSize(int groupSize) {
097            this.groupSize = groupSize;
098        }    
099        
100        
101        /**
102         * @return the logger to which statistic messages will be sent
103         */
104        public Log getLog() {
105            return log;
106        }
107        
108        /**
109         * Sets the logger to which statistic messages will be sent
110         */
111        public void setLog(Log log) {
112            this.log = log;
113        }
114            
115        /**
116         * @return the MessageListener which this listener delegates to
117         */
118        public MessageListener getMessageListener() {
119            return messageListener;    
120        }
121        
122        /**
123         * Sets the MessageListener which this listener delegates to, which can be null.
124         */
125        public void setMessageListener(MessageListener messageListener) {
126            this.messageListener = messageListener;
127        }
128        
129    }