001    /**
002     * Copyright (c) 2000-2010 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            public int getActiveThreadCount() {
044                    if (_autoRefresh) {
045                            refresh();
046                    }
047    
048                    return _destinationStatistics.getActiveThreadCount();
049            }
050    
051            public int getCurrentThreadCount() {
052                    if (_autoRefresh || (_destinationStatistics == null)) {
053                            refresh();
054                    }
055    
056                    return _destinationStatistics.getCurrentThreadCount();
057            }
058    
059            public int getLargestThreadCount() {
060                    if (_autoRefresh || (_destinationStatistics == null)) {
061                            refresh();
062                    }
063    
064                    return _destinationStatistics.getLargestThreadCount();
065            }
066    
067            public String getLastRefresh() {
068                    return String.valueOf(_lastRefresh);
069            }
070    
071            public int getMaxThreadPoolSize() {
072                    if (_autoRefresh || (_destinationStatistics == null)) {
073                            refresh();
074                    }
075    
076                    return _destinationStatistics.getMaxThreadPoolSize();
077            }
078    
079            public int getMinThreadPoolSize() {
080                    if (_autoRefresh || (_destinationStatistics == null)) {
081                            refresh();
082                    }
083    
084                    return _destinationStatistics.getMinThreadPoolSize();
085            }
086    
087            public long getPendingMessageCount() {
088                    if (_autoRefresh || (_destinationStatistics == null)) {
089                            refresh();
090                    }
091    
092                    return _destinationStatistics.getPendingMessageCount();
093            }
094    
095            public long getSentMessageCount() {
096                    if (_autoRefresh || (_destinationStatistics == null)) {
097                            refresh();
098                    }
099    
100                    return _destinationStatistics.getSentMessageCount();
101            }
102    
103            public boolean isAutoRefresh() {
104                    return _autoRefresh;
105            }
106    
107            public void refresh() {
108                    if (System.currentTimeMillis() > _lastRefresh) {
109                            _lastRefresh = System.currentTimeMillis();
110                            _destinationStatistics = _destination.getDestinationStatistics();
111                    }
112            }
113    
114            public void setAutoRefresh(boolean autoRefresh) {
115                    _autoRefresh = autoRefresh;
116            }
117    
118            private static final String _OBJECT_NAME_PREFIX =
119                    "Liferay:product=Portal,type=MessagingDestinationStatistics,name=";
120    
121            private boolean _autoRefresh;
122            private Destination _destination;
123            private long _lastRefresh;
124            private DestinationStatistics _destinationStatistics;
125    
126    }