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.activemq.broker.jmx;
018
019import org.apache.activemq.management.TimeStatisticImpl;
020import org.slf4j.Logger;
021import org.slf4j.LoggerFactory;
022
023public class NetworkDestinationView implements NetworkDestinationViewMBean {
024    private static final Logger LOG = LoggerFactory.getLogger(NetworkDestinationViewMBean.class);
025    private TimeStatisticImpl timeStatistic = new TimeStatisticImpl("networkEnqueue","network messages enqueued");
026
027    private final String name;
028    private final NetworkBridgeView networkBridgeView;
029    private long lastTime = -1;
030
031    public NetworkDestinationView(NetworkBridgeView networkBridgeView, String name){
032       this.networkBridgeView = networkBridgeView;
033       this.name=name;
034    }
035    /**
036     * Returns the name of this destination
037     */
038    @Override
039    public String getName() {
040        return name;
041    }
042
043    /**
044     * Resets the managment counters.
045     */
046    @Override
047    public void resetStats() {
048        timeStatistic.reset();
049        lastTime = -1;
050    }
051
052
053    @Override
054    public long getCount() {
055        return timeStatistic.getCount();
056    }
057
058    @Override
059    public double getRate() {
060        return timeStatistic.getAveragePerSecond();
061    }
062
063    public void messageSent(){
064        long currentTime = System.currentTimeMillis();
065        long time = 0;
066        if (lastTime < 0){
067            time = 0;
068            lastTime = currentTime;
069        }else{
070            time = currentTime-lastTime;
071        }
072        timeStatistic.addTime(time);
073        lastTime=currentTime;
074    }
075
076    public long getLastAccessTime(){
077        return timeStatistic.getLastSampleTime();
078    }
079
080    public void close(){
081        networkBridgeView.removeNetworkDestinationView(this);
082    }
083
084
085}