1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.messaging.jmx;
16  
17  import com.liferay.portal.kernel.messaging.Destination;
18  import com.liferay.portal.kernel.messaging.DestinationStatistics;
19  
20  import javax.management.MalformedObjectNameException;
21  import javax.management.ObjectName;
22  
23  /**
24   * <a href="DestinationStatisticsManager.java.html"><b><i>View Source</i></b>
25   * </a>
26   *
27   * @author Michael C. Han
28   * @author Brian Wing Shun Chan
29   */
30  public class DestinationStatisticsManager
31      implements DestinationStatisticsManagerMBean {
32  
33      public static ObjectName createObjectName(String destinationName) {
34          try {
35              return new ObjectName(_OBJECT_NAME_PREFIX + destinationName);
36          }
37          catch (MalformedObjectNameException mone) {
38              throw new IllegalStateException(mone);
39          }
40      }
41  
42      public DestinationStatisticsManager(Destination destination) {
43          _destination = destination;
44      }
45  
46      public int getActiveThreadCount() {
47          if (_autoRefresh) {
48              refresh();
49          }
50  
51          return _destinationStatistics.getActiveThreadCount();
52      }
53  
54      public int getCurrentThreadCount() {
55          if (_autoRefresh || (_destinationStatistics == null)) {
56              refresh();
57          }
58  
59          return _destinationStatistics.getCurrentThreadCount();
60      }
61  
62      public int getLargestThreadCount() {
63          if (_autoRefresh || (_destinationStatistics == null)) {
64              refresh();
65          }
66  
67          return _destinationStatistics.getLargestThreadCount();
68      }
69  
70      public String getLastRefresh() {
71          return String.valueOf(_lastRefresh);
72      }
73  
74      public int getMaxThreadPoolSize() {
75          if (_autoRefresh || (_destinationStatistics == null)) {
76              refresh();
77          }
78  
79          return _destinationStatistics.getMaxThreadPoolSize();
80      }
81  
82      public int getMinThreadPoolSize() {
83          if (_autoRefresh || (_destinationStatistics == null)) {
84              refresh();
85          }
86  
87          return _destinationStatistics.getMinThreadPoolSize();
88      }
89  
90      public long getPendingMessageCount() {
91          if (_autoRefresh || (_destinationStatistics == null)) {
92              refresh();
93          }
94  
95          return _destinationStatistics.getPendingMessageCount();
96      }
97  
98      public long getSentMessageCount() {
99          if (_autoRefresh || (_destinationStatistics == null)) {
100             refresh();
101         }
102 
103         return _destinationStatistics.getSentMessageCount();
104     }
105 
106     public boolean isAutoRefresh() {
107         return _autoRefresh;
108     }
109 
110     public void refresh() {
111         if (System.currentTimeMillis() > _lastRefresh) {
112             _lastRefresh = System.currentTimeMillis();
113             _destinationStatistics = _destination.getDestinationStatistics();
114         }
115     }
116 
117     public void setAutoRefresh(boolean autoRefresh) {
118         _autoRefresh = autoRefresh;
119     }
120 
121     private static final String _OBJECT_NAME_PREFIX =
122         "Liferay:product=Portal,type=MessagingDestinationStatistics,name=";
123 
124     private boolean _autoRefresh;
125     private Destination _destination;
126     private long _lastRefresh;
127     private DestinationStatistics _destinationStatistics;
128 
129 }