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(null, -1, false);
063    }
064
065    @Override
066    public TabularData browse(int limit, boolean sortByLongestDuration) {
067        return browse(null, limit, sortByLongestDuration);
068    }
069
070    @Override
071    public TabularData browse(String routeId, int limit, boolean sortByLongestDuration) {
072        try {
073            TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.listInflightExchangesTabularType());
074            Collection<InflightRepository.InflightExchange> exchanges = inflightRepository.browse(routeId, limit, sortByLongestDuration);
075
076            for (InflightRepository.InflightExchange entry : exchanges) {
077                CompositeType ct = CamelOpenMBeanTypes.listInflightExchangesCompositeType();
078                String exchangeId = entry.getExchange().getExchangeId();
079                String fromRouteId = entry.getFromRouteId();
080                String atRouteId = entry.getAtRouteId();
081                String nodeId = entry.getNodeId();
082                String elapsed = "" + entry.getElapsed();
083                String duration = "" + entry.getDuration();
084
085                CompositeData data = new CompositeDataSupport(ct,
086                        new String[]{"exchangeId", "fromRouteId", "routeId", "nodeId", "elapsed", "duration"},
087                        new Object[]{exchangeId, fromRouteId, atRouteId, nodeId, elapsed, duration});
088                answer.put(data);
089            }
090            return answer;
091        } catch (Exception e) {
092            throw ObjectHelper.wrapRuntimeCamelException(e);
093        }
094    }
095}