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.mbean;
018
019import java.util.Collection;
020
021import javax.management.openmbean.CompositeData;
022import javax.management.openmbean.CompositeDataSupport;
023import javax.management.openmbean.CompositeType;
024import javax.management.openmbean.TabularData;
025import javax.management.openmbean.TabularDataSupport;
026
027import org.apache.camel.CamelContext;
028import org.apache.camel.api.management.ManagedResource;
029import org.apache.camel.api.management.mbean.CamelOpenMBeanTypes;
030import org.apache.camel.api.management.mbean.ManagedInflightRepositoryMBean;
031import org.apache.camel.spi.InflightRepository;
032import org.apache.camel.util.ObjectHelper;
033
034/**
035 *
036 */
037@ManagedResource(description = "Managed InflightRepository")
038public class ManagedInflightRepository extends ManagedService implements ManagedInflightRepositoryMBean {
039
040    private final InflightRepository inflightRepository;
041
042    public ManagedInflightRepository(CamelContext context, InflightRepository inflightRepository) {
043        super(context, inflightRepository);
044        this.inflightRepository = inflightRepository;
045    }
046
047    public InflightRepository getInflightRepository() {
048        return inflightRepository;
049    }
050
051    @Override
052    public int getSize() {
053        return inflightRepository.size();
054    }
055
056    @Override
057    public int size(String routeId) {
058        return inflightRepository.size(routeId);
059    }
060
061    @Override
062    public TabularData browse() {
063        return browse(null, -1, false);
064    }
065
066    @Override
067    public TabularData browse(int limit, boolean sortByLongestDuration) {
068        return browse(null, limit, sortByLongestDuration);
069    }
070
071    @Override
072    public TabularData browse(String routeId, int limit, boolean sortByLongestDuration) {
073        try {
074            TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.listInflightExchangesTabularType());
075            Collection<InflightRepository.InflightExchange> exchanges = inflightRepository.browse(routeId, limit, sortByLongestDuration);
076
077            for (InflightRepository.InflightExchange entry : exchanges) {
078                CompositeType ct = CamelOpenMBeanTypes.listInflightExchangesCompositeType();
079                String exchangeId = entry.getExchange().getExchangeId();
080                String fromRouteId = entry.getFromRouteId();
081                String atRouteId = entry.getAtRouteId();
082                String nodeId = entry.getNodeId();
083                String elapsed = "" + entry.getElapsed();
084                String duration = "" + entry.getDuration();
085
086                CompositeData data = new CompositeDataSupport(ct,
087                        new String[]{"exchangeId", "fromRouteId", "routeId", "nodeId", "elapsed", "duration"},
088                        new Object[]{exchangeId, fromRouteId, atRouteId, nodeId, elapsed, duration});
089                answer.put(data);
090            }
091            return answer;
092        } catch (Exception e) {
093            throw ObjectHelper.wrapRuntimeCamelException(e);
094        }
095    }
096}