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, route, and
023 * route group levels, 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    private final PerformanceCounter counter3; // counter 3 is optional
033
034    public CompositePerformanceCounter(PerformanceCounter counter1, PerformanceCounter counter2, PerformanceCounter counter3) {
035        this.counter1 = counter1;
036        this.counter2 = counter2;
037        this.counter3 = counter3;
038    }
039
040    @Override
041    public void processExchange(Exchange exchange, String type) {
042        if (counter1.isStatisticsEnabled()) {
043            counter1.processExchange(exchange, type);
044        }
045        if (counter2.isStatisticsEnabled()) {
046            counter2.processExchange(exchange, type);
047        }
048        if (counter3 != null && counter3.isStatisticsEnabled()) {
049            counter3.processExchange(exchange, type);
050        }
051    }
052
053    @Override
054    public void completedExchange(Exchange exchange, long time) {
055        if (counter1.isStatisticsEnabled()) {
056            counter1.completedExchange(exchange, time);
057        }
058        if (counter2.isStatisticsEnabled()) {
059            counter2.completedExchange(exchange, time);
060        }
061        if (counter3 != null && counter3.isStatisticsEnabled()) {
062            counter3.completedExchange(exchange, time);
063        }
064    }
065
066    @Override
067    public void failedExchange(Exchange exchange) {
068        if (counter1.isStatisticsEnabled()) {
069            counter1.failedExchange(exchange);
070        }
071        if (counter2.isStatisticsEnabled()) {
072            counter2.failedExchange(exchange);
073        }
074        if (counter3 != null && counter3.isStatisticsEnabled()) {
075            counter3.failedExchange(exchange);
076        }
077    }
078
079    @Override
080    public boolean isStatisticsEnabled() {
081        // this method is not used
082        return true;
083    }
084
085    @Override
086    public void setStatisticsEnabled(boolean statisticsEnabled) {
087        // this method is not used
088    }
089}