1. /*
  2. * @(#)MemoryNotificationInfo.java 1.6 04/04/18
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.lang.management;
  8. import javax.management.openmbean.CompositeData;
  9. import sun.management.MemoryNotifInfoCompositeData;
  10. /**
  11. * The information about a memory notification.
  12. *
  13. * <p>
  14. * A memory notification is emitted by {@link MemoryMXBean}
  15. * when the Java virtual machine detects that the memory usage
  16. * of a memory pool is exceeding a threshold value.
  17. * The notification emitted will contain the memory notification
  18. * information about the detected condition:
  19. * <ul>
  20. * <li>The name of the memory pool.</li>
  21. * <li>The memory usage of the memory pool when the notification
  22. * was constructed.</li>
  23. * <li>The number of times that the memory usage has crossed
  24. * a threshold when the notification was constructed.
  25. * For usage threshold notifications, this count will be the
  26. * {@link MemoryPoolMXBean#getUsageThresholdCount usage threshold
  27. * count}. For collection threshold notifications,
  28. * this count will be the
  29. * {@link MemoryPoolMXBean#getCollectionUsageThresholdCount
  30. * collection usage threshold count}.
  31. * </li>
  32. * </ul>
  33. *
  34. * <p>
  35. * A {@link CompositeData CompositeData} representing
  36. * the <tt>MemoryNotificationInfo</tt> object
  37. * is stored in the
  38. * {@link javax.management.Notification#setUserData user data}
  39. * of a {@link javax.management.Notification notification}.
  40. * The {@link #from from} method is provided to convert from
  41. * a <tt>CompositeData</tt> to a <tt>MemoryNotificationInfo</tt>
  42. * object. For example:
  43. *
  44. * <blockquote><pre>
  45. * Notification notif;
  46. *
  47. * // receive the notification emitted by MemoryMXBean and set to notif
  48. * ...
  49. *
  50. * String notifType = notif.getType();
  51. * if (notifType.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED) ||
  52. * notifType.equals(MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED)) {
  53. * // retrieve the memory notification information
  54. * CompositeData cd = (CompositeData) notif.getUserData();
  55. * MemoryNotificationInfo info = MemoryNotificationInfo.from(cd);
  56. * ....
  57. * }
  58. * </pre></blockquote>
  59. *
  60. * <p>
  61. * The types of notifications emitted by <tt>MemoryMXBean</tt> are:
  62. * <ul>
  63. * <li>A {@link #MEMORY_THRESHOLD_EXCEEDED
  64. * usage threshold exceeded notification}.
  65. * <br>This notification will be emitted when
  66. * the memory usage of a memory pool is increased and has reached
  67. * or exceeded its
  68. * <a href="MemoryPoolMXBean#UsageThreshold"> usage threshold</a> value.
  69. * Subsequent crossing of the usage threshold value does not cause
  70. * further notification until the memory usage has returned
  71. * to become less than the usage threshold value.
  72. * <p></li>
  73. * <li>A {@link #MEMORY_COLLECTION_THRESHOLD_EXCEEDED
  74. * collection usage threshold exceeded notification}.
  75. * <br>This notification will be emitted when
  76. * the memory usage of a memory pool is greater than or equal to its
  77. * <a href="MemoryPoolMXBean#CollectionThreshold">
  78. * collection usage threshold</a> after the Java virtual machine
  79. * has expended effort in recycling unused objects in that
  80. * memory pool.</li>
  81. * </ul>
  82. *
  83. * @author Mandy Chung
  84. * @version 1.6, 04/18/04
  85. * @since 1.5
  86. *
  87. */
  88. public class MemoryNotificationInfo {
  89. private final String poolName;
  90. private final MemoryUsage usage;
  91. private final long count;
  92. /**
  93. * Notification type denoting that
  94. * the memory usage of a memory pool has
  95. * reached or exceeded its
  96. * <a href="MemoryPoolMXBean#UsageThreshold"> usage threshold</a> value.
  97. * This notification is emitted by {@link MemoryMXBean}.
  98. * Subsequent crossing of the usage threshold value does not cause
  99. * further notification until the memory usage has returned
  100. * to become less than the usage threshold value.
  101. * The value of this notification type is
  102. * <tt>java.management.memory.threshold.exceeded</tt>.
  103. */
  104. public static final String MEMORY_THRESHOLD_EXCEEDED =
  105. "java.management.memory.threshold.exceeded";
  106. /**
  107. * Notification type denoting that
  108. * the memory usage of a memory pool is greater than or equal to its
  109. * <a href="MemoryPoolMXBean#CollectionThreshold">
  110. * collection usage threshold</a> after the Java virtual machine
  111. * has expended effort in recycling unused objects in that
  112. * memory pool.
  113. * This notification is emitted by {@link MemoryMXBean}.
  114. * The value of this notification type is
  115. * <tt>java.management.memory.collection.threshold.exceeded</tt>.
  116. */
  117. public static final String MEMORY_COLLECTION_THRESHOLD_EXCEEDED =
  118. "java.management.memory.collection.threshold.exceeded";
  119. /**
  120. * Constructs a <tt>MemoryNotificationInfo</tt> object.
  121. *
  122. * @param poolName The name of the memory pool which triggers this notification.
  123. * @param usage Memory usage of the memory pool.
  124. * @param count The threshold crossing count.
  125. */
  126. public MemoryNotificationInfo(String poolName,
  127. MemoryUsage usage,
  128. long count) {
  129. if (poolName == null) {
  130. throw new NullPointerException("Null poolName");
  131. }
  132. if (usage == null) {
  133. throw new NullPointerException("Null usage");
  134. }
  135. this.poolName = poolName;
  136. this.usage = usage;
  137. this.count = count;
  138. }
  139. MemoryNotificationInfo(CompositeData cd) {
  140. MemoryNotifInfoCompositeData.validateCompositeData(cd);
  141. this.poolName = MemoryNotifInfoCompositeData.getPoolName(cd);
  142. this.usage = MemoryNotifInfoCompositeData.getUsage(cd);
  143. this.count = MemoryNotifInfoCompositeData.getCount(cd);
  144. }
  145. /**
  146. * Returns the name of the memory pool that triggers this notification.
  147. * The memory pool usage has crossed a threshold.
  148. *
  149. * @return the name of the memory pool that triggers this notification.
  150. */
  151. public String getPoolName() {
  152. return poolName;
  153. }
  154. /**
  155. * Returns the memory usage of the memory pool
  156. * when this notification was constructed.
  157. *
  158. * @return the memory usage of the memory pool
  159. * when this notification was constructed.
  160. */
  161. public MemoryUsage getUsage() {
  162. return usage;
  163. }
  164. /**
  165. * Returns the number of times that the memory usage has crossed
  166. * a threshold when the notification was constructed.
  167. * For usage threshold notifications, this count will be the
  168. * {@link MemoryPoolMXBean#getUsageThresholdCount threshold
  169. * count}. For collection threshold notifications,
  170. * this count will be the
  171. * {@link MemoryPoolMXBean#getCollectionUsageThresholdCount
  172. * collection usage threshold count}.
  173. *
  174. * @return the number of times that the memory usage has crossed
  175. * a threshold when the notification was constructed.
  176. */
  177. public long getCount() {
  178. return count;
  179. }
  180. /**
  181. * Returns a <tt>MemoryNotificationInfo</tt> object represented by the
  182. * given <tt>CompositeData</tt>.
  183. * The given <tt>CompositeData</tt> must contain
  184. * the following attributes:
  185. * <blockquote>
  186. * <table border>
  187. * <tr>
  188. * <th align=left>Attribute Name</th>
  189. * <th align=left>Type</th>
  190. * </tr>
  191. * <tr>
  192. * <td>poolName</td>
  193. * <td><tt>java.lang.String</tt></td>
  194. * </tr>
  195. * <tr>
  196. * <td>usage</td>
  197. * <td><tt>javax.management.openmbean.CompositeData</tt></td>
  198. * </tr>
  199. * <tr>
  200. * <td>count</td>
  201. * <td><tt>java.lang.Long</tt></td>
  202. * </tr>
  203. * </table>
  204. * </blockquote>
  205. *
  206. * @param cd <tt>CompositeData</tt> representing a
  207. * <tt>MemoryNotificationInfo</tt>
  208. *
  209. * @throws IllegalArgumentException if <tt>cd</tt> does not
  210. * represent a <tt>MemoryNotificationInfo</tt> object.
  211. *
  212. * @return a <tt>MemoryNotificationInfo</tt> object represented
  213. * by <tt>cd</tt> if <tt>cd</tt> is not <tt>null</tt>
  214. * <tt>null</tt> otherwise.
  215. */
  216. public static MemoryNotificationInfo from(CompositeData cd) {
  217. if (cd == null) {
  218. return null;
  219. }
  220. if (cd instanceof MemoryNotifInfoCompositeData) {
  221. return ((MemoryNotifInfoCompositeData) cd).getMemoryNotifInfo();
  222. } else {
  223. return new MemoryNotificationInfo(cd);
  224. }
  225. }
  226. }