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.AsyncCallback; 020 import org.apache.camel.AsyncProcessor; 021 import org.apache.camel.Exchange; 022 import org.apache.camel.processor.DelegateProcessor; 023 import org.apache.camel.util.AsyncProcessorHelper; 024 import org.apache.commons.logging.Log; 025 import org.apache.commons.logging.LogFactory; 026 027 /** 028 * JMX enabled processor that uses the {@link Counter} for instrumenting 029 * processing of exchanges. 030 * 031 * @version $Revision: 45204 $ 032 */ 033 public class InstrumentationProcessor extends DelegateProcessor implements AsyncProcessor { 034 035 private static final transient Log LOG = LogFactory.getLog(InstrumentationProcessor.class); 036 private PerformanceCounter counter; 037 038 public InstrumentationProcessor(PerformanceCounter counter) { 039 this.counter = counter; 040 } 041 042 public InstrumentationProcessor() { 043 } 044 045 public void setCounter(PerformanceCounter counter) { 046 this.counter = counter; 047 } 048 049 public void process(Exchange exchange) throws Exception { 050 AsyncProcessorHelper.process(this, exchange); 051 } 052 053 public boolean process(final Exchange exchange, final AsyncCallback callback) { 054 final long startTime = System.nanoTime(); 055 056 if (processor instanceof AsyncProcessor) { 057 return ((AsyncProcessor)processor).process(exchange, new AsyncCallback() { 058 public void done(boolean doneSynchronously) { 059 if (counter != null) { 060 // convert nanoseconds to milliseconds 061 recordTime(exchange, (System.nanoTime() - startTime) / 1000000.0); 062 } 063 callback.done(doneSynchronously); 064 } 065 }); 066 } 067 068 try { 069 processor.process(exchange); 070 } catch (Throwable e) { 071 exchange.setException(e); 072 } 073 074 if (counter != null) { 075 // convert nanoseconds to milliseconds 076 recordTime(exchange, (System.nanoTime() - startTime) / 1000000.0); 077 } 078 callback.done(true); 079 return true; 080 } 081 082 protected void recordTime(Exchange exchange, double duration) { 083 if (LOG.isTraceEnabled()) { 084 LOG.trace("Recording duration: " + duration + " millis for exchange: " + exchange); 085 } 086 087 if (!exchange.isFailed() && exchange.getException() == null) { 088 counter.completedExchange(duration); 089 } else { 090 counter.failedExchange(); 091 } 092 } 093 094 }