> For the complete documentation index, see [llms.txt](https://potoyang.gitbook.io/spring-in-action-v4/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://potoyang.gitbook.io/spring-in-action-v4/untitled-13/untitled-1/untitled.md).

# 20.3.1　监听通知

接收 MBean 通知的标准方法是实现  javax.management.NotificationListener 接口。例如，考虑一下 PagingNotificationListener：

```java
package com.habuma.spittr.jmx;

import javax.management.Notification;
import javax.management.NotificationListener;

public class PagingNotificationListener implements NotificationListener {
  public void handleNotification(Notification notification, Object handback) {
    // ...
  }
}
```

PagingNotificationListener 是一个典型的 JMX 通知监听器。当接收到通知时，将会调用 handleNotification() 方法处理通知。大概的逻辑可能是，PagingNotificationListener 的 handleNotification() 方法将向寻呼机或手机上发送消息来告知 Spittle 数量又到了一个新的百万级别（我把实际的实现留给读者自己完成）。 剩下的工作只需要使用 MBeanExporter 注册 PagingNotificationListener：

```java
@Bean
public MBeanExporter mbeanExporter() {
  MBeanExporter exporter = new MBeanExporter();
  Map<?, NotificationListener> mappings = new HashMap<?, NotificationListener>();
  mappings.put("Spitter:name=PagingNotificationListener", new PagingNotificationListener());
  exporter.setNotificationListenerMappings(mappings);
  return exporter;
}
```

MBeanExporter的notificationListenerMappings 属性用于 在监听器和监听器所希望监听的 MBean 之间建立映射。在本示例中，我们建立了 PagingNotificationListener 来监听由 SpittleNotifier MBean 所发布的通知。
