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.Exchange; 021 import org.apache.camel.api.management.PerformanceCounter; 022 import org.apache.camel.management.mbean.ManagedPerformanceCounter; 023 import org.apache.camel.processor.DelegateAsyncProcessor; 024 import org.apache.camel.util.StopWatch; 025 import org.slf4j.Logger; 026 import org.slf4j.LoggerFactory; 027 028 /** 029 * JMX enabled processor that uses the {@link org.apache.camel.management.mbean.ManagedCounter} for instrumenting 030 * processing of exchanges. 031 * 032 * @version 033 */ 034 public class InstrumentationProcessor extends DelegateAsyncProcessor { 035 036 private static final Logger LOG = LoggerFactory.getLogger(InstrumentationProcessor.class); 037 private PerformanceCounter counter; 038 private String type; 039 040 public InstrumentationProcessor() { 041 } 042 043 public InstrumentationProcessor(PerformanceCounter counter) { 044 this.counter = counter; 045 } 046 047 @Override 048 public String toString() { 049 return "Instrumentation" + (type != null ? ":" + type : "") + "[" + processor + "]"; 050 } 051 052 public void setCounter(Object counter) { 053 ManagedPerformanceCounter mpc = null; 054 if (counter instanceof ManagedPerformanceCounter) { 055 mpc = (ManagedPerformanceCounter) counter; 056 } 057 058 if (this.counter instanceof DelegatePerformanceCounter) { 059 ((DelegatePerformanceCounter) this.counter).setCounter(mpc); 060 } else if (mpc != null) { 061 this.counter = mpc; 062 } else if (counter instanceof PerformanceCounter) { 063 this.counter = (PerformanceCounter) counter; 064 } 065 } 066 067 @Override 068 public boolean process(final Exchange exchange, final AsyncCallback callback) { 069 // only record time if stats is enabled 070 final StopWatch watch = (counter != null && counter.isStatisticsEnabled()) ? new StopWatch() : null; 071 072 return processor.process(exchange, new AsyncCallback() { 073 public void done(boolean doneSync) { 074 try { 075 // record end time 076 if (watch != null) { 077 recordTime(exchange, watch.stop()); 078 } 079 } finally { 080 // and let the original callback know we are done as well 081 callback.done(doneSync); 082 } 083 } 084 085 @Override 086 public String toString() { 087 return InstrumentationProcessor.this.toString(); 088 } 089 }); 090 } 091 092 protected void recordTime(Exchange exchange, long duration) { 093 if (LOG.isTraceEnabled()) { 094 LOG.trace("{}Recording duration: {} millis for exchange: {}", new Object[]{type != null ? type + ": " : "", duration, exchange}); 095 } 096 097 if (!exchange.isFailed() && exchange.getException() == null) { 098 counter.completedExchange(exchange, duration); 099 } else { 100 counter.failedExchange(exchange); 101 } 102 } 103 104 public String getType() { 105 return type; 106 } 107 108 public void setType(String type) { 109 this.type = type; 110 } 111 }