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     */
017    package org.apache.servicemix.jbi.monitoring;
018    
019    import javax.management.JMException;
020    import javax.management.MBeanAttributeInfo;
021    import javax.management.MBeanOperationInfo;
022    
023    import org.apache.servicemix.jbi.management.AttributeInfoHelper;
024    import org.apache.servicemix.jbi.management.BaseLifeCycle;
025    import org.apache.servicemix.jbi.management.OperationInfoHelper;
026    import org.apache.servicemix.jbi.servicedesc.AbstractServiceEndpoint;
027    import org.apache.servicemix.jbi.servicedesc.EndpointSupport;
028    
029    public class EndpointStats extends BaseLifeCycle implements EndpointStatsMBean {
030    
031        private AbstractServiceEndpoint endpoint;
032        private MessagingStats stats;
033        
034        public EndpointStats(AbstractServiceEndpoint endpoint, MessagingStats componentStats) {
035            this.endpoint = endpoint;
036            this.stats = new MessagingStats(EndpointSupport.getUniqueKey(endpoint), componentStats);
037        }
038        
039        MessagingStats getMessagingStats() {
040            return stats;
041        }
042        
043        void incrementInbound() {
044            stats.getInboundExchanges().increment();
045            stats.getInboundExchangeRate().addTime();
046        }
047        
048        void incrementOutbound() {
049            stats.getOutboundExchanges().increment();
050            stats.getOutboundExchangeRate().addTime();
051        }
052        
053        /**
054         * Get the type of the item
055         * @return the type
056         */
057        public String getType() {
058            return "Statistics";
059        }
060    
061        public String getSubType() {
062            return "Endpoint";
063        }
064        
065        /**
066         * Get the name of the item
067         * @return the name
068         */
069        public String getName() {
070            return EndpointSupport.getUniqueKey(endpoint);
071        }
072    
073        /**
074         * Get the Description of the item
075         * @return the description
076         */
077        public String getDescription() {
078            return "Statistics for endpoint " + EndpointSupport.getUniqueKey(endpoint);
079        }
080    
081        public long getInboundExchangeCount() {
082            return stats.getInboundExchanges().getCount();
083        }
084    
085        public double getInboundExchangeRate() {
086            return stats.getInboundExchangeRate().getAveragePerSecond();
087        }
088    
089        public long getOutboundExchangeCount() {
090            return stats.getOutboundExchanges().getCount();
091        }
092    
093        public double getOutboundExchangeRate() {
094            return stats.getOutboundExchangeRate().getAveragePerSecond();
095        }
096    
097        /**
098         * Reset all stats counters
099         */
100        public void reset() {
101            stats.reset();
102        }
103    
104        /**
105         * Get an array of MBeanAttributeInfo
106         * 
107         * @return array of AttributeInfos
108         * @throws JMException
109         */
110        public MBeanAttributeInfo[] getAttributeInfos() throws JMException {
111            AttributeInfoHelper helper = new AttributeInfoHelper();
112            helper.addAttribute(getObjectToManage(), "inboundExchangeCount", "count of inbound exchanges");
113            helper.addAttribute(getObjectToManage(), "outboundExchangeCount", "count of outbound exchanges");
114            helper.addAttribute(getObjectToManage(), "inboundExchangeRate", "rate of inbound exchanges/sec");
115            helper.addAttribute(getObjectToManage(), "outboundExchangeRate", "rate of outbound exchanges/sec");
116            return helper.getAttributeInfos();
117        }
118    
119        /**
120         * Get an array of MBeanOperationInfo
121         * 
122         * @return array of OperationInfos
123         */
124        public MBeanOperationInfo[] getOperationInfos() {
125            OperationInfoHelper helper = new OperationInfoHelper();
126            helper.addOperation(getObjectToManage(), "reset", "reset statistic counters");
127            return helper.getOperationInfos();
128        }
129    
130    }