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;
031 private double maxProcessingTime;
032 private double totalProcessingTime;
033 private Date lastExchangeCompletionTime;
034 private Date lastExchangeFailureTime;
035 private Date firstExchangeCompletionTime;
036 private Date firstExchangeFailureTime;
037
038 @Override
039 @ManagedOperation(description = "Reset counters")
040 public synchronized void reset() {
041 super.reset();
042 numCompleted.set(0L);
043 minProcessingTime = 0.0;
044 maxProcessingTime = 0.0;
045 totalProcessingTime = 0.0;
046 lastExchangeCompletionTime = null;
047 lastExchangeFailureTime = null;
048 firstExchangeCompletionTime = null;
049 firstExchangeFailureTime = null;
050 }
051
052 @ManagedAttribute(description = "Number of successful exchanges")
053 public long getNumCompleted() throws Exception {
054 return numCompleted.get();
055 }
056
057 @ManagedAttribute(description = "Number of failed exchanges")
058 public long getNumFailed() throws Exception {
059 return numExchanges.get() - numCompleted.get();
060 }
061
062 @ManagedAttribute(description = "Min Processing Time [milliseconds]")
063 public synchronized double getMinProcessingTimeMillis() throws Exception {
064 return minProcessingTime;
065 }
066
067 @ManagedAttribute(description = "Mean Processing Time [milliseconds]")
068 public synchronized double getMeanProcessingTimeMillis() throws Exception {
069 long count = numCompleted.get();
070 return count > 0 ? totalProcessingTime / count : 0.0;
071 }
072
073 @ManagedAttribute(description = "Max Processing Time [milliseconds]")
074 public synchronized double getMaxProcessingTimeMillis() throws Exception {
075 return maxProcessingTime;
076 }
077
078 @ManagedAttribute(description = "Total Processing Time [milliseconds]")
079 public synchronized double getTotalProcessingTimeMillis() throws Exception {
080 return totalProcessingTime;
081 }
082
083 @ManagedAttribute(description = "Last Exchange Completed Timestamp")
084 public synchronized Date getLastExchangeCompletionTime() {
085 return lastExchangeCompletionTime;
086 }
087
088 @ManagedAttribute(description = "First Exchange Completed Timestamp")
089 public synchronized Date getFirstExchangeCompletionTime() {
090 return firstExchangeCompletionTime;
091 }
092
093 @ManagedAttribute(description = "Last Exchange Failed Timestamp")
094 public synchronized Date getLastExchangeFailureTime() {
095 return lastExchangeFailureTime;
096 }
097
098 @ManagedAttribute(description = "First Exchange Failed Timestamp")
099 public synchronized Date getFirstExchangeFailureTime() {
100 return firstExchangeFailureTime;
101 }
102
103 /**
104 * This method is called when an exchange has been processed successfully.
105 *
106 * @param time in milliseconds it spent on processing the exchange
107 */
108 public synchronized void completedExchange(double time) {
109 increment();
110 numCompleted.incrementAndGet();
111
112 totalProcessingTime += time;
113 if (minProcessingTime <= 0 || time < minProcessingTime) {
114 minProcessingTime = time;
115 }
116 if (time > maxProcessingTime) {
117 maxProcessingTime = time;
118 }
119
120 Date timestamp = new Date();
121 if (firstExchangeCompletionTime == null) {
122 firstExchangeCompletionTime = timestamp;
123 }
124 lastExchangeCompletionTime = timestamp;
125 }
126
127 /**
128 * This method is called when an exchange has been processed and failed.
129 */
130 public synchronized void failedExchange() {
131 increment();
132
133 Date timestamp = new Date();
134 if (firstExchangeFailureTime == null) {
135 firstExchangeFailureTime = timestamp;
136 }
137 lastExchangeFailureTime = timestamp;
138 }
139 }