View Javadoc

1   /*
2    * Copyright (C) The Apache Software Foundation. All rights reserved.
3    *
4    * This software is published under the terms of the Apache Software License
5    * version 1.1, a copy of which has been included with this distribution in
6    * the LICENSE file.
7    * 
8    * $Id: StopWatchMessageListener.java 155459 2005-02-26 13:24:44Z dirkv $
9    */
10  
11  package org.apache.commons.messenger.tool;
12  
13  import javax.jms.Destination;
14  import javax.jms.JMSException;
15  import javax.jms.Message;
16  import javax.jms.MessageListener;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  
21  /**
22   * A simple StopWatch Message Listener for wrapping another MessageListener
23   * to determine its performance.
24   *
25   * @author  James Strachan
26   */
27  public class StopWatchMessageListener implements MessageListener {
28  
29      /** The Log to which logging calls will be made. */
30      private Log log = LogFactory.getLog( StopWatchMessageListener.class );
31      
32      /** the underlying MessageListener */
33      private MessageListener messageListener;
34  
35      /** the number of messages processed */
36      private int count;
37          
38      /** the message group size */
39      private int groupSize = 1000;
40          
41      /** the time that the batch started processing */
42      private long startTime;
43      
44      public StopWatchMessageListener() {
45      }
46      
47      public StopWatchMessageListener(MessageListener messageListener) {
48          this.messageListener = messageListener;
49      }
50      
51      // MessageListener interface
52      //-------------------------------------------------------------------------                    
53      public void onMessage(Message message) {
54          if ( count == 0 ) {
55              startTime = System.currentTimeMillis();
56          }
57          if ( messageListener != null ) {
58              messageListener.onMessage(message);
59          }
60      
61          if ( ++count == groupSize ) {
62              long elapsed = System.currentTimeMillis() - startTime;            
63              double timePerMessage = elapsed;
64              timePerMessage /= count;
65              
66              double messagesPerSecond = 1000; 
67              messagesPerSecond /= timePerMessage;
68              
69              Destination destination = null;
70              try {
71                  destination = message.getJMSDestination();
72              }
73              catch (JMSException e) {
74                  // ignore
75              }            
76              log.info( "Time to process " + count + " messages: " + elapsed + " millis on: " + destination );
77              log.info( "Average number of messages per second: " + messagesPerSecond );
78              count = 0;
79          }
80      }
81  
82      
83      // Properties
84      //-------------------------------------------------------------------------                
85      
86      /**
87       * @return the number of messages in the group before the performance statistics are logged
88       */
89      public int getGroupSize() {
90          return groupSize;
91      }    
92          
93      /**
94       * Sets the number of messages in the group before the performance statistics are logged
95       */
96      public void setGroupSize(int groupSize) {
97          this.groupSize = groupSize;
98      }    
99      
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 }