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.RuntimeCamelException;
029import org.apache.camel.api.management.ManagedResource;
030import org.apache.camel.api.management.mbean.CamelOpenMBeanTypes;
031import org.apache.camel.api.management.mbean.ManagedInflightRepositoryMBean;
032import org.apache.camel.spi.InflightRepository;
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 boolean isInflightBrowseEnabled() {
058        return inflightRepository.isInflightBrowseEnabled();
059    }
060
061    @Override
062    public int size(String routeId) {
063        return inflightRepository.size(routeId);
064    }
065
066    @Override
067    public TabularData browse() {
068        return browse(null, -1, false);
069    }
070
071    @Override
072    public TabularData browse(int limit, boolean sortByLongestDuration) {
073        return browse(null, limit, sortByLongestDuration);
074    }
075
076    @Override
077    public TabularData browse(String routeId, int limit, boolean sortByLongestDuration) {
078        try {
079            TabularData answer = new TabularDataSupport(CamelOpenMBeanTypes.listInflightExchangesTabularType());
080            Collection<InflightRepository.InflightExchange> exchanges
081                    = inflightRepository.browse(routeId, limit, sortByLongestDuration);
082
083            for (InflightRepository.InflightExchange entry : exchanges) {
084                CompositeType ct = CamelOpenMBeanTypes.listInflightExchangesCompositeType();
085                String exchangeId = entry.getExchange().getExchangeId();
086                String fromRouteId = entry.getFromRouteId();
087                String atRouteId = entry.getAtRouteId();
088                String nodeId = entry.getNodeId();
089                String elapsed = Long.toString(entry.getElapsed());
090                String duration = Long.toString(entry.getDuration());
091
092                CompositeData data = new CompositeDataSupport(
093                        ct,
094                        new String[] { "exchangeId", "fromRouteId", "routeId", "nodeId", "elapsed", "duration" },
095                        new Object[] { exchangeId, fromRouteId, atRouteId, nodeId, elapsed, duration });
096                answer.put(data);
097            }
098            return answer;
099        } catch (Exception e) {
100            throw RuntimeCamelException.wrapRuntimeCamelException(e);
101        }
102    }
103}