001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.messaging.jmx;
016    
017    import com.liferay.portal.kernel.messaging.Destination;
018    import com.liferay.portal.kernel.messaging.DestinationStatistics;
019    
020    import javax.management.MalformedObjectNameException;
021    import javax.management.ObjectName;
022    
023    /**
024     * @author Michael C. Han
025     * @author Brian Wing Shun Chan
026     */
027    public class DestinationStatisticsManager
028            implements DestinationStatisticsManagerMBean {
029    
030            public static ObjectName createObjectName(String destinationName) {
031                    try {
032                            return new ObjectName(_OBJECT_NAME_PREFIX + destinationName);
033                    }
034                    catch (MalformedObjectNameException mone) {
035                            throw new IllegalStateException(mone);
036                    }
037            }
038    
039            public DestinationStatisticsManager(Destination destination) {
040                    _destination = destination;
041            }
042    
043            @Override
044            public int getActiveThreadCount() {
045                    if (_autoRefresh) {
046                            refresh();
047                    }
048    
049                    return _destinationStatistics.getActiveThreadCount();
050            }
051    
052            @Override
053            public int getCurrentThreadCount() {
054                    if (_autoRefresh || (_destinationStatistics == null)) {
055                            refresh();
056                    }
057    
058                    return _destinationStatistics.getCurrentThreadCount();
059            }
060    
061            @Override
062            public int getLargestThreadCount() {
063                    if (_autoRefresh || (_destinationStatistics == null)) {
064                            refresh();
065                    }
066    
067                    return _destinationStatistics.getLargestThreadCount();
068            }
069    
070            @Override
071            public String getLastRefresh() {
072                    return String.valueOf(_lastRefresh);
073            }
074    
075            @Override
076            public int getMaxThreadPoolSize() {
077                    if (_autoRefresh || (_destinationStatistics == null)) {
078                            refresh();
079                    }
080    
081                    return _destinationStatistics.getMaxThreadPoolSize();
082            }
083    
084            @Override
085            public int getMinThreadPoolSize() {
086                    if (_autoRefresh || (_destinationStatistics == null)) {
087                            refresh();
088                    }
089    
090                    return _destinationStatistics.getMinThreadPoolSize();
091            }
092    
093            @Override
094            public long getPendingMessageCount() {
095                    if (_autoRefresh || (_destinationStatistics == null)) {
096                            refresh();
097                    }
098    
099                    return _destinationStatistics.getPendingMessageCount();
100            }
101    
102            @Override
103            public long getSentMessageCount() {
104                    if (_autoRefresh || (_destinationStatistics == null)) {
105                            refresh();
106                    }
107    
108                    return _destinationStatistics.getSentMessageCount();
109            }
110    
111            @Override
112            public boolean isAutoRefresh() {
113                    return _autoRefresh;
114            }
115    
116            @Override
117            public void refresh() {
118                    if (System.currentTimeMillis() > _lastRefresh) {
119                            _lastRefresh = System.currentTimeMillis();
120                            _destinationStatistics = _destination.getDestinationStatistics();
121                    }
122            }
123    
124            @Override
125            public void setAutoRefresh(boolean autoRefresh) {
126                    _autoRefresh = autoRefresh;
127            }
128    
129            private static final String _OBJECT_NAME_PREFIX =
130                    "Liferay:product=Portal,type=MessagingDestinationStatistics,name=";
131    
132            private boolean _autoRefresh;
133            private Destination _destination;
134            private DestinationStatistics _destinationStatistics;
135            private long _lastRefresh;
136    
137    }