001 /** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one or more 004 * contributor license agreements. See the NOTICE file distributed with 005 * this work for additional information regarding copyright ownership. 006 * The ASF licenses this file to You under the Apache License, Version 2.0 007 * (the "License"); you may not use this file except in compliance with 008 * the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 package org.apache.camel.processor; 019 020 import java.util.concurrent.atomic.AtomicInteger; 021 import java.text.MessageFormat; 022 import java.text.NumberFormat; 023 024 import org.apache.camel.Exchange; 025 import org.apache.commons.logging.Log; 026 027 /** 028 * @version $Revision: 1.1 $ 029 */ 030 public class ThroughputLogger extends Logger { 031 private int groupSize = 100; 032 private long startTime; 033 private AtomicInteger receivedCounter = new AtomicInteger(); 034 private NumberFormat numberFormat = NumberFormat.getNumberInstance(); 035 036 public ThroughputLogger() { 037 } 038 039 public ThroughputLogger(Log log) { 040 super(log); 041 } 042 043 public ThroughputLogger(Log log, LoggingLevel level) { 044 super(log, level); 045 } 046 047 public ThroughputLogger(String logName) { 048 super(logName); 049 } 050 051 public ThroughputLogger(String logName, LoggingLevel level) { 052 super(logName, level); 053 } 054 055 public ThroughputLogger(String logName, LoggingLevel level, int groupSize) { 056 super(logName, level); 057 setGroupSize(groupSize); 058 } 059 060 public ThroughputLogger(String logName, int groupSize) { 061 super(logName); 062 setGroupSize(groupSize); 063 } 064 065 public ThroughputLogger(int groupSize) { 066 setGroupSize(groupSize); 067 } 068 069 @Override 070 public void process(Exchange exchange) { 071 if (startTime == 0) { 072 startTime = System.currentTimeMillis(); 073 } 074 int receivedCount = receivedCounter.incrementAndGet(); 075 if (receivedCount % groupSize == 0) { 076 super.process(exchange); 077 } 078 } 079 080 public int getGroupSize() { 081 return groupSize; 082 } 083 084 public void setGroupSize(int groupSize) { 085 if (groupSize == 0) { 086 throw new IllegalArgumentException("groupSize cannot be zero!"); 087 } 088 this.groupSize = groupSize; 089 } 090 091 public NumberFormat getNumberFormat() { 092 return numberFormat; 093 } 094 095 public void setNumberFormat(NumberFormat numberFormat) { 096 this.numberFormat = numberFormat; 097 } 098 099 @Override 100 protected Object logMessage(Exchange exchange) { 101 long time = System.currentTimeMillis(); 102 long elapsed = time - startTime; 103 startTime = time; 104 105 // timeOneMessage = time / group 106 // messagePerSend = 1000 / timeOneMessage 107 double rate = groupSize * 1000.0; 108 rate /= elapsed; 109 110 return "Received: " + receivedCounter.get() + " messages so far. Last group took: " + elapsed + " millis which is: " + numberFormat.format(rate) + " messages per second"; 111 } 112 }