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