コード例 #1
0
  public void testRegisterNotificationListenerWithHandback() throws Exception {
    String objectName = "spring:name=Test";
    JmxTestBean bean = new JmxTestBean();

    Map<String, Object> beans = new HashMap<String, Object>();
    beans.put(objectName, bean);

    CountingAttributeChangeNotificationListener listener =
        new CountingAttributeChangeNotificationListener();
    Object handback = new Object();

    NotificationListenerBean listenerBean = new NotificationListenerBean();
    listenerBean.setNotificationListener(listener);
    listenerBean.setMappedObjectName("spring:name=Test");
    listenerBean.setHandback(handback);

    MBeanExporter exporter = new MBeanExporter();
    exporter.setServer(server);
    exporter.setBeans(beans);
    exporter.setNotificationListeners(new NotificationListenerBean[] {listenerBean});
    exporter.afterPropertiesSet();

    // update the attribute
    String attributeName = "Name";
    server.setAttribute(
        ObjectNameManager.getInstance("spring:name=Test"),
        new Attribute(attributeName, "Rob Harrop"));

    assertEquals("Listener not notified", 1, listener.getCount(attributeName));
    assertEquals(
        "Handback object not transmitted correctly",
        handback,
        listener.getLastHandback(attributeName));
  }
コード例 #2
0
  public void testRegisterNotificationListenerForAllMBeans() throws Exception {
    ObjectName objectName = ObjectName.getInstance("spring:name=Test");
    JmxTestBean bean = new JmxTestBean();

    Map<String, Object> beans = new HashMap<String, Object>();
    beans.put(objectName.getCanonicalName(), bean);

    CountingAttributeChangeNotificationListener listener =
        new CountingAttributeChangeNotificationListener();

    NotificationListenerBean listenerBean = new NotificationListenerBean();
    listenerBean.setNotificationListener(listener);

    MBeanExporter exporter = new MBeanExporter();
    exporter.setServer(server);
    exporter.setBeans(beans);
    exporter.setNotificationListeners(new NotificationListenerBean[] {listenerBean});
    exporter.afterPropertiesSet();

    // update the attribute
    String attributeName = "Name";
    server.setAttribute(objectName, new Attribute(attributeName, "Rob Harrop"));

    assertEquals("Listener not notified", 1, listener.getCount(attributeName));
  }
コード例 #3
0
  @SuppressWarnings("serial")
  public void testRegisterNotificationListenerWithFilter() throws Exception {
    ObjectName objectName = ObjectName.getInstance("spring:name=Test");
    JmxTestBean bean = new JmxTestBean();

    Map<String, Object> beans = new HashMap<String, Object>();
    beans.put(objectName.getCanonicalName(), bean);

    CountingAttributeChangeNotificationListener listener =
        new CountingAttributeChangeNotificationListener();

    NotificationListenerBean listenerBean = new NotificationListenerBean();
    listenerBean.setNotificationListener(listener);
    listenerBean.setNotificationFilter(
        new NotificationFilter() {
          public boolean isNotificationEnabled(Notification notification) {
            if (notification instanceof AttributeChangeNotification) {
              AttributeChangeNotification changeNotification =
                  (AttributeChangeNotification) notification;
              return "Name".equals(changeNotification.getAttributeName());
            } else {
              return false;
            }
          }
        });

    MBeanExporter exporter = new MBeanExporter();
    exporter.setServer(server);
    exporter.setBeans(beans);
    exporter.setNotificationListeners(new NotificationListenerBean[] {listenerBean});
    exporter.afterPropertiesSet();

    // update the attributes
    String nameAttribute = "Name";
    String ageAttribute = "Age";

    server.setAttribute(objectName, new Attribute(nameAttribute, "Rob Harrop"));
    server.setAttribute(objectName, new Attribute(ageAttribute, new Integer(90)));

    assertEquals("Listener not notified for Name", 1, listener.getCount(nameAttribute));
    assertEquals("Listener incorrectly notified for Age", 0, listener.getCount(ageAttribute));
  }