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 */
017package org.apache.camel.management;
018
019import org.apache.camel.Exchange;
020
021/**
022 * A composite {@link PerformanceCounter} is used for tracking performance statistics on both a per context and route
023 * level, by issuing callbacks on both when an event happens.
024 * <p/>
025 * This implementation is used so the {@link org.apache.camel.management.mbean.ManagedCamelContext} can aggregate all
026 * stats from the routes.
027 */
028public class CompositePerformanceCounter implements PerformanceCounter {
029
030    private final PerformanceCounter counter1;
031    private final PerformanceCounter counter2;
032
033    public CompositePerformanceCounter(PerformanceCounter counter1, PerformanceCounter counter2) {
034        this.counter1 = counter1;
035        this.counter2 = counter2;
036    }
037
038    @Override
039    public void processExchange(Exchange exchange) {
040        if (counter1.isStatisticsEnabled()) {
041            counter1.processExchange(exchange);
042        }
043        if (counter2.isStatisticsEnabled()) {
044            counter2.processExchange(exchange);
045        }
046    }
047
048    @Override
049    public void completedExchange(Exchange exchange, long time) {
050        if (counter1.isStatisticsEnabled()) {
051            counter1.completedExchange(exchange, time);
052        }
053        if (counter2.isStatisticsEnabled()) {
054            counter2.completedExchange(exchange, time);
055        }
056    }
057
058    @Override
059    public void failedExchange(Exchange exchange) {
060        if (counter1.isStatisticsEnabled()) {
061            counter1.failedExchange(exchange);
062        }
063        if (counter2.isStatisticsEnabled()) {
064            counter2.failedExchange(exchange);
065        }
066    }
067
068    @Override
069    public boolean isStatisticsEnabled() {
070        // this method is not used
071        return true;
072    }
073
074    @Override
075    public void setStatisticsEnabled(boolean statisticsEnabled) {
076        // this method is not used
077    }
078}