Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6598156

Notification: receive duplicated notifications

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Duplicate
    • Affects Version/s: 6u10
    • Fix Version/s: 7
    • Component/s: core-svc
    • Labels:
      None

      Description

      A singleton mbean is repeatedly registered/unregistered, a listener needs to receive notifications from the mbean so it is added to the mbean every time the mbean is registered, suppose that the mbean is to be registered 10th time, then each notification the mbean sends out will be received 10 times by the listener.

      Here is the test code:

      import java.util.HashMap;
      import java.util.Map;
      import javax.management.MBeanServer;
      import javax.management.MBeanServerFactory;
      import javax.management.Notification;
      import javax.management.NotificationBroadcasterSupport;
      import javax.management.NotificationListener;
      import javax.management.ObjectName;
      import javax.management.remote.JMXConnector;
      import javax.management.remote.JMXConnectorFactory;
      import javax.management.remote.JMXConnectorServer;
      import javax.management.remote.JMXConnectorServerFactory;
      import javax.management.remote.JMXServiceURL;

      public class Test {
          public static void main(String[] args) throws Exception {
              MBeanServer mbeanServer = MBeanServerFactory.createMBeanServer();
              ObjectName emitter = new ObjectName("Default:name=NotificationEmitter");
              
              JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
              JMXConnectorServer server =
                      JMXConnectorServerFactory.newJMXConnectorServer(url,
                      null, mbeanServer);
              server.start();
              
              url = server.getAddress();
              
      // Map env = new HashMap(1);
      // env.put("jmx.remote.x.enable.event.service", "true");
      // JMXConnector conn = JMXConnectorFactory.connect(url, env);
              JMXConnector conn = JMXConnectorFactory.connect(url);
              
              NotificationEmitter emitterImpl = new NotificationEmitter();
              
              mbeanServer.registerMBean(emitterImpl, emitter);
              
              MyListener directL = new MyListener();
              conn.getMBeanServerConnection().addNotificationListener(emitter,
                      directL, null, null);
              
              emitterImpl.sendNotif(1, null);
              
              for (int i=0; i<10; i++) {
                  mbeanServer.unregisterMBean(emitter);
                  mbeanServer.registerMBean(emitterImpl, emitter);
                  Thread.sleep(10);
              }

              conn.getMBeanServerConnection().addNotificationListener(emitter,
                      directL, null, null);
              
              emitterImpl.sendNotif(1, null);
              
              Thread.sleep(100);
              if (directL.received != 2) {
                  throw new RuntimeException("Expected to receive 2, but got "+
                          directL.received);
              }
              
              mbeanServer.unregisterMBean(emitter);
          }
          
      //--------------------------
      // private classes
      //--------------------------
          
          private static class MyListener implements NotificationListener {
              public void handleNotification(Notification notif, Object handback) {
                  System.out.println("---MyListener-handleNotification "+notif.getSequenceNumber());
                  received++;
              }
              
              private int received = 0;
          }
          
          public static class NotificationEmitter extends NotificationBroadcasterSupport
                  implements NotificationEmitterMBean {
              
              /**
               * Send Notification objects.
               *
               * @param nb The number of notifications to send
               */
              public void sendNotif(int nb, String userData) {
                  for (int i = 0; i<nb; i++) {
                      Notification notif = new Notification(myType, this, counter++);
                      notif.setUserData(userData);
                      sendNotification(notif);
                  }
              }
              
              public int counter = 0;
              private final String myType = "notification.my_notification";
          }
          
          public interface NotificationEmitterMBean {
              public void sendNotif(int nb, String userData);
          }
      }

      The test result is:

      ---MyListener-handleNotification 0
      ---MyListener-handleNotification 1
      ---MyListener-handleNotification 1
      ---MyListener-handleNotification 1
      ---MyListener-handleNotification 1
      ---MyListener-handleNotification 1
      ---MyListener-handleNotification 1
      ---MyListener-handleNotification 1
      ---MyListener-handleNotification 1
      ---MyListener-handleNotification 1
      ---MyListener-handleNotification 1
      ---MyListener-handleNotification 1
      ---MyListener-handleNotification 0
      ---MyListener-handleNotification 1
      ---MyListener-handleNotification 1
      ---MyListener-handleNotification 1
      ---MyListener-handleNotification 1
      ---MyListener-handleNotification 1
      ---MyListener-handleNotification 1
      ---MyListener-handleNotification 1
      ---MyListener-handleNotification 1
      ---MyListener-handleNotification 1
      ---MyListener-handleNotification 1
      ---MyListener-handleNotification 1
      Exception in thread "main" java.lang.RuntimeException: Expected to receive 2, but got 12
              at Test.main(Test.java:65)

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                sjiang Shanliang Jiang (Inactive)
                Reporter:
                sjiang Shanliang Jiang (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: