课程:通知

原文: https://docs.oracle.com/javase/tutorial/jmx/notifs/index.html

JMX API 定义了一种机制,使 MBean 能够生成通知,例如,指示状态更改,检测到的事件或问题。

要生成通知,MBean 必须实现接口 NotificationEmitter 或扩展 NotificationBroadcasterSupport 。要发送通知,您需要构建类 javax.management.Notification 或子类(例如 AttributeChangedNotification )的实例,并将实例传递给 NotificationBroadcasterSupport.sendNotification

每个通知都有一个来源。源是生成通知的 MBean 的对象名称。

每个通知都有一个序列号。当订单很重要时,此号码可用于订购来自同一来源的通知,并且存在以错误的顺序处理通知的风险。序列号可以是零,但优选地,来自给定 MBean 的每个通知的数量增量。

标准 MBean 中的 Hello MBean 实现实际上实现了通知机制。但是,为了简单起见,该课程中省略了此代码。 Hello的完整代码如下:

  1. package com.example;
  2. import javax.management.*;
  3. public class Hello
  4. extends NotificationBroadcasterSupport
  5. implements HelloMBean {
  6. public void sayHello() {
  7. System.out.println("hello, world");
  8. }
  9. public int add(int x, int y) {
  10. return x + y;
  11. }
  12. public String getName() {
  13. return this.name;
  14. }
  15. public int getCacheSize() {
  16. return this.cacheSize;
  17. }
  18. public synchronized void setCacheSize(int size) {
  19. int oldSize = this.cacheSize;
  20. this.cacheSize = size;
  21. System.out.println("Cache size now " + this.cacheSize);
  22. Notification n = new AttributeChangeNotification(this,
  23. sequenceNumber++, System.currentTimeMillis(),
  24. "CacheSize changed", "CacheSize", "int",
  25. oldSize, this.cacheSize);
  26. sendNotification(n);
  27. }
  28. @Override
  29. public MBeanNotificationInfo[] getNotificationInfo() {
  30. String[] types = new String[]{
  31. AttributeChangeNotification.ATTRIBUTE_CHANGE
  32. };
  33. String name = AttributeChangeNotification.class.getName();
  34. String description = "An attribute of this MBean has changed";
  35. MBeanNotificationInfo info =
  36. new MBeanNotificationInfo(types, name, description);
  37. return new MBeanNotificationInfo[]{info};
  38. }
  39. private final String name = "Reginald";
  40. private int cacheSize = DEFAULT_CACHE_SIZE;
  41. private static final int DEFAULT_CACHE_SIZE = 200;
  42. private long sequenceNumber = 1;
  43. }

这个Hello MBean 实现扩展了NotificationBroadcasterSupport类。 NotificationBroadcasterSupport实现NotificationEmitter接口。

操作和属性的设置方式与标准 MBean 示例相同,但CacheSize属性的 setter 方法现在定义了oldSize的值。此值记录设置操作之前的CacheSize属性值。

通知由 JMX 类AttributeChangeNotification的实例n构成,它扩展了javax.management.Notification。通知根据以下信息在setCacheSize()方法的定义内构建。该信息作为参数传递给AttributeChangeNotification

  • 通知源的对象名称,即Hello MBean,由this表示
  • 序号,即sequenceNumber,设置为 1 并逐渐增加
  • 时间戳
  • 通知消息的内容
  • 已更改的属性的名称,在本例中为CacheSize
  • 已更改的属性类型
  • 旧属性值,在本例中为oldSize
  • 新属性值,在本例中为this.cacheSize

然后将通知n传递给NotificationBroadcasterSupport.sendNotification()方法。

最后, MBeanNotificationInfo 实例被定义为描述 MBean 为给定类型的通知生成的不同通知实例的特征。在这种情况下,发送的通知类型是AttributeChangeNotification通知。

运行 MBean 通知示例

再次,您将使用 JConsole 与Hello MBean 进行交互,这次是发送和接收通知。此示例需要 Java SE 平台的第 6 版。

  1. 如果尚未这样做,请将 jmx_examples.zip 保存到work_dir目录中。
  2. 在终端窗口中使用以下命令解压缩示例类包。

    1. unzip jmx_examples.zip
  3. 编译work_dir目录中的示例 Java 类。

    1. javac com/example/*.java
  4. Start the Main application.

    1. java com.example.Main

    生成Main正在等待发生某事的确认。

  5. Start JConsole in a different terminal window on the same machine.

    1. jconsole

    将显示“新建连接”对话框,其中列出了可以连接的正在运行的 JMX 代理的列表。

  6. In the New Connection dialog box, select com.example.Main from the list and click Connect.

    将显示平台当前活动的摘要。

  7. Click the MBeans tab.

    此面板显示当前在 MBean 服务器中注册的所有 MBean。

  8. In the left frame, expand the com.example node in the MBean tree.

    您会看到Hello创建并注册的示例 MBean Hello。如果单击Hello,则会在 MBean 树中看到其 Notifications 节点。

  9. Expand the Notifications node of the Hello MBean in the MBean tree.

    请注意,面板为空白。

  10. Click the Subscribe button.

    接收的当前通知数(0)显示在“通知”节点标签中。

  11. Expand the Attributes node of the Hello MBean in the MBean tree, and change the value of the CacheSize attribute to 150.

    在您启动Main的终端窗口中,显示此属性更改的确认。请注意,“通知”节点中显示的已接收通知数已更改为 1。

  12. Expand again the Notifications node of the Hello MBean in the MBean tree.

    将显示通知的详细信息。

  13. 要关闭 JConsole,请选择 Connection - >出口。