Coverage Report - org.apache.commons.messenger.tool.StopWatchMessageListener
 
Classes in this File Line Coverage Branch Coverage Complexity
StopWatchMessageListener
0%
0/34
0%
0/8
1.444
 
 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  0
     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  0
     private int groupSize = 1000;
 40  
         
 41  
     /** the time that the batch started processing */
 42  
     private long startTime;
 43  
     
 44  0
     public StopWatchMessageListener() {
 45  0
     }
 46  
     
 47  0
     public StopWatchMessageListener(MessageListener messageListener) {
 48  0
         this.messageListener = messageListener;
 49  0
     }
 50  
     
 51  
     // MessageListener interface
 52  
     //-------------------------------------------------------------------------                    
 53  
     public void onMessage(Message message) {
 54  0
         if ( count == 0 ) {
 55  0
             startTime = System.currentTimeMillis();
 56  
         }
 57  0
         if ( messageListener != null ) {
 58  0
             messageListener.onMessage(message);
 59  
         }
 60  
     
 61  0
         if ( ++count == groupSize ) {
 62  0
             long elapsed = System.currentTimeMillis() - startTime;            
 63  0
             double timePerMessage = elapsed;
 64  0
             timePerMessage /= count;
 65  
             
 66  0
             double messagesPerSecond = 1000; 
 67  0
             messagesPerSecond /= timePerMessage;
 68  
             
 69  0
             Destination destination = null;
 70  
             try {
 71  0
                 destination = message.getJMSDestination();
 72  
             }
 73  0
             catch (JMSException e) {
 74  
                 // ignore
 75  0
             }            
 76  0
             log.info( "Time to process " + count + " messages: " + elapsed + " millis on: " + destination );
 77  0
             log.info( "Average number of messages per second: " + messagesPerSecond );
 78  0
             count = 0;
 79  
         }
 80  0
     }
 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  0
         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  0
         this.groupSize = groupSize;
 98  0
     }    
 99  
     
 100  
     
 101  
     /**
 102  
      * @return the logger to which statistic messages will be sent
 103  
      */
 104  
     public Log getLog() {
 105  0
         return log;
 106  
     }
 107  
     
 108  
     /**
 109  
      * Sets the logger to which statistic messages will be sent
 110  
      */
 111  
     public void setLog(Log log) {
 112  0
         this.log = log;
 113  0
     }
 114  
         
 115  
     /**
 116  
      * @return the MessageListener which this listener delegates to
 117  
      */
 118  
     public MessageListener getMessageListener() {
 119  0
         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  0
         this.messageListener = messageListener;
 127  0
     }
 128  
     
 129  
 }