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