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.concurrent.atomic.AtomicLong;
020
021 import org.springframework.jmx.export.annotation.ManagedAttribute;
022 import org.springframework.jmx.export.annotation.ManagedOperation;
023 import org.springframework.jmx.export.annotation.ManagedResource;
024
025 @ManagedResource(description = "PerformanceCounter", currencyTimeLimit = 15)
026 public class PerformanceCounter extends Counter {
027
028 private AtomicLong numCompleted = new AtomicLong(0L);
029 private long minProcessingTime = -1L;
030 private long maxProcessingTime = -1L;
031 private double totalProcessingTime;
032
033 @Override
034 @ManagedOperation(description = "Reset counters")
035 public synchronized void reset() {
036 super.reset();
037 numCompleted.set(0L);
038 minProcessingTime = 0L;
039 maxProcessingTime = 0L;
040 totalProcessingTime = 0;
041 }
042
043 @ManagedAttribute(description = "Number of successful exchanges")
044 public long getNumCompleted() throws Exception {
045 return numCompleted.get();
046 }
047
048 @ManagedAttribute(description = "Number of failed exchanges")
049 public long getNumFailed() throws Exception {
050 return numExchanges.get() - numCompleted.get();
051 }
052
053 @ManagedAttribute(description = "Min Processing Time [usec]")
054 public synchronized long getMinProcessingTime() throws Exception {
055 return minProcessingTime;
056 }
057
058 @ManagedAttribute(description = "Mean Processing Time [usec]")
059 public synchronized long getMeanProcessingTime() throws Exception {
060 long count = numCompleted.get();
061 return count > 0 ? (long)totalProcessingTime / count : 0L;
062 }
063
064 @ManagedAttribute(description = "Max Processing Time [usec]")
065 public synchronized long getMaxProcessingTime() throws Exception {
066 return maxProcessingTime;
067 }
068
069 public synchronized void completedExchange(long time) {
070 increment();
071 numCompleted.incrementAndGet();
072 totalProcessingTime += time;
073 if (minProcessingTime < 0 || time < minProcessingTime) {
074 minProcessingTime = time;
075 }
076 if (time > maxProcessingTime) {
077 maxProcessingTime = time;
078 }
079 }
080
081 public void completedExchange() {
082 numExchanges.incrementAndGet();
083 }
084 }