001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. 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 package org.apache.camel.management; 018 019 import java.util.Date; 020 import java.util.concurrent.atomic.AtomicLong; 021 022 import org.springframework.jmx.export.annotation.ManagedAttribute; 023 import org.springframework.jmx.export.annotation.ManagedOperation; 024 import org.springframework.jmx.export.annotation.ManagedResource; 025 026 @ManagedResource(description = "PerformanceCounter", currencyTimeLimit = 15) 027 public class PerformanceCounter extends Counter { 028 029 private AtomicLong numCompleted = new AtomicLong(0L); 030 private double minProcessingTime = -1.0; 031 private double maxProcessingTime; 032 private double totalProcessingTime; 033 private Date lastExchangeCompletionTime; 034 private Date firstExchangeCompletionTime; 035 036 @Override 037 @ManagedOperation(description = "Reset counters") 038 public synchronized void reset() { 039 super.reset(); 040 numCompleted.set(0L); 041 minProcessingTime = -1.0; 042 maxProcessingTime = 0.0; 043 totalProcessingTime = 0.0; 044 lastExchangeCompletionTime = null; 045 firstExchangeCompletionTime = null; 046 047 } 048 049 @ManagedAttribute(description = "Number of successful exchanges") 050 public long getNumCompleted() throws Exception { 051 return numCompleted.get(); 052 } 053 054 @ManagedAttribute(description = "Number of failed exchanges") 055 public long getNumFailed() throws Exception { 056 return numExchanges.get() - numCompleted.get(); 057 } 058 059 @ManagedAttribute(description = "Min Processing Time [milliseconds]") 060 public synchronized double getMinProcessingTimeMillis() throws Exception { 061 return minProcessingTime; 062 } 063 064 @ManagedAttribute(description = "Mean Processing Time [milliseconds]") 065 public synchronized double getMeanProcessingTimeMillis() throws Exception { 066 long count = numCompleted.get(); 067 return count > 0 ? totalProcessingTime / count : 0.0; 068 } 069 070 @ManagedAttribute(description = "Max Processing Time [milliseconds]") 071 public synchronized double getMaxProcessingTimeMillis() throws Exception { 072 return maxProcessingTime; 073 } 074 075 @ManagedAttribute(description = "Total Processing Time [milliseconds]") 076 public synchronized double getTotalProcessingTimeMillis() throws Exception { 077 return totalProcessingTime; 078 } 079 080 @ManagedAttribute(description = "Last Exchange Completed Timestamp") 081 public synchronized Date getLastExchangeCompletionTime() { 082 return lastExchangeCompletionTime; 083 } 084 085 @ManagedAttribute(description = "First Exchange Completed Timestamp") 086 public synchronized Date getFirstExchangeCompletionTime() { 087 return firstExchangeCompletionTime; 088 } 089 090 /** 091 * This method is called when an exchange has been processed successfully. 092 * 093 * @param time in milliseconds it spent on processing the exchange 094 */ 095 public synchronized void completedExchange(double time) { 096 increment(); 097 numCompleted.incrementAndGet(); 098 totalProcessingTime += time; 099 if (minProcessingTime < 0 || time < minProcessingTime) { 100 minProcessingTime = time; 101 } 102 if (time > maxProcessingTime) { 103 maxProcessingTime = time; 104 } 105 Date timestamp = new Date(); 106 if (firstExchangeCompletionTime == null) { 107 firstExchangeCompletionTime = timestamp; 108 } 109 lastExchangeCompletionTime = timestamp; 110 } 111 112 public void completedExchange() { 113 numExchanges.incrementAndGet(); 114 } 115 }