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: 699782 $ 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 if (processor == null) { 055 // no processor so nothing to process, so return 056 callback.done(true); 057 return true; 058 } 059 060 final long startTime = System.nanoTime(); 061 062 if (processor instanceof AsyncProcessor) { 063 return ((AsyncProcessor)processor).process(exchange, new AsyncCallback() { 064 public void done(boolean doneSynchronously) { 065 if (counter != null) { 066 // convert nanoseconds to milliseconds 067 recordTime(exchange, (System.nanoTime() - startTime) / 1000000.0); 068 } 069 callback.done(doneSynchronously); 070 } 071 }); 072 } 073 074 try { 075 processor.process(exchange); 076 } catch (Throwable e) { 077 exchange.setException(e); 078 } 079 080 if (counter != null) { 081 // convert nanoseconds to milliseconds 082 recordTime(exchange, (System.nanoTime() - startTime) / 1000000.0); 083 } 084 callback.done(true); 085 return true; 086 } 087 088 protected void recordTime(Exchange exchange, double duration) { 089 if (LOG.isTraceEnabled()) { 090 LOG.trace("Recording duration: " + duration + " millis for exchange: " + exchange); 091 } 092 093 if (!exchange.isFailed() && exchange.getException() == null) { 094 counter.completedExchange(duration); 095 } else { 096 counter.failedExchange(); 097 } 098 } 099 100 }