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 org.apache.camel.Exchange;
020 import org.apache.camel.management.mbean.ManagedPerformanceCounter;
021 import org.apache.camel.processor.DelegateProcessor;
022 import org.apache.commons.logging.Log;
023 import org.apache.commons.logging.LogFactory;
024
025 /**
026 * JMX enabled processor that uses the {@link org.apache.camel.management.mbean.ManagedCounter} for instrumenting
027 * processing of exchanges.
028 *
029 * @version $Revision: 836213 $
030 */
031 public class InstrumentationProcessor extends DelegateProcessor {
032
033 private static final transient Log LOG = LogFactory.getLog(InstrumentationProcessor.class);
034 private PerformanceCounter counter;
035 private String type;
036
037 public InstrumentationProcessor() {
038 }
039
040 public InstrumentationProcessor(PerformanceCounter counter) {
041 this.counter = counter;
042 }
043
044 @Override
045 public String toString() {
046 return "Instrumentation" + (type != null ? ":" + type : "") + "[" + processor + "]";
047 }
048
049 public void setCounter(ManagedPerformanceCounter counter) {
050 if (this.counter instanceof DelegatePerformanceCounter) {
051 ((DelegatePerformanceCounter) this.counter).setCounter(counter);
052 } else {
053 this.counter = counter;
054 }
055 }
056
057 public void process(Exchange exchange) throws Exception {
058 if (processor != null) {
059
060 // use nano time as its more accurate
061 long startTime = -1;
062 if (counter != null && counter.isStatisticsEnabled()) {
063 startTime = System.nanoTime();
064 }
065
066 try {
067 processor.process(exchange);
068 } catch (Exception e) {
069 exchange.setException(e);
070 }
071
072 if (startTime != -1) {
073 long diff = (System.nanoTime() - startTime) / 1000000;
074 recordTime(exchange, diff);
075 }
076 }
077 }
078
079 protected void recordTime(Exchange exchange, long duration) {
080 if (LOG.isTraceEnabled()) {
081 LOG.trace((type != null ? type + ": " : "") + "Recording duration: " + duration + " millis for exchange: " + exchange);
082 }
083
084 if (!exchange.isFailed() && exchange.getException() == null) {
085 counter.completedExchange(duration);
086 } else {
087 counter.failedExchange();
088 }
089 }
090
091 public String getType() {
092 return type;
093 }
094
095 public void setType(String type) {
096 this.type = type;
097 }
098 }