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