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