jdk/test/javax/management/remote/mandatory/notif/EmptyDomainNotificationTest.java
author dfuchs
Thu, 02 Feb 2017 16:50:46 +0000
changeset 43503 bc7f8619ab70
parent 42677 d2c15c981e72
child 44423 306c020eb154
permissions -rw-r--r--
8173607: JMX RMI connector should be in its own module Summary: The JMX RMI connector is moved to a new java.management.rmi module. Reviewed-by: mchung, erikj
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
42677
d2c15c981e72 8168141: javax/management/remote/mandatory/notif/EmptyDomainNotificationTest.java: No notif received!
vtewari
parents: 30376
diff changeset
     2
 * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6238731
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Check that the expected notification is received by the JMX
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          client even when the domain in the ObjectName is not specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Shanliang JIANG
43503
bc7f8619ab70 8173607: JMX RMI connector should be in its own module
dfuchs
parents: 42677
diff changeset
    30
 * @modules java.management.rmi
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run clean EmptyDomainNotificationTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run build EmptyDomainNotificationTest
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
    33
 * @run main EmptyDomainNotificationTest
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
    36
import java.util.ArrayList;
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
    37
import java.util.List;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.MBeanServerConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.MBeanServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.Notification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.NotificationBroadcasterSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.NotificationListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.remote.JMXConnector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.remote.JMXConnectorFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.management.remote.JMXConnectorServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.management.remote.JMXConnectorServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.management.remote.JMXServiceURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public class EmptyDomainNotificationTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public static interface SimpleMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        public void emitNotification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public static class Simple
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        extends NotificationBroadcasterSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        implements SimpleMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        public void emitNotification() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            sendNotification(new Notification("simple", this, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public static class Listener implements NotificationListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        public void handleNotification(Notification n, Object h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
          "EmptyDomainNotificationTest-Listener-handleNotification: receives:" + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            if (n.getType().equals("simple")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    received++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    this.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        public int received;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        final JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
    88
        JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        server.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        JMXConnector client = JMXConnectorFactory.connect(server.getAddress(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        final MBeanServerConnection mbsc = client.getMBeanServerConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        final ObjectName mbean = ObjectName.getInstance(":type=Simple");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        mbsc.createMBean(Simple.class.getName(), mbean);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        System.out.println("EmptyDomainNotificationTest-main: add a listener ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        final Listener li = new Listener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        mbsc.addNotificationListener(mbean, li, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        System.out.println("EmptyDomainNotificationTest-main: ask to send a notif ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        mbsc.invoke(mbean, "emitNotification", null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        System.out.println("EmptyDomainNotificationTest-main: waiting notif...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        synchronized(li) {
42677
d2c15c981e72 8168141: javax/management/remote/mandatory/notif/EmptyDomainNotificationTest.java: No notif received!
vtewari
parents: 30376
diff changeset
   107
            while (li.received < 1) {
d2c15c981e72 8168141: javax/management/remote/mandatory/notif/EmptyDomainNotificationTest.java: No notif received!
vtewari
parents: 30376
diff changeset
   108
                li.wait();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
42677
d2c15c981e72 8168141: javax/management/remote/mandatory/notif/EmptyDomainNotificationTest.java: No notif received!
vtewari
parents: 30376
diff changeset
   112
        if (li.received != 1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            throw new RuntimeException("Wait one notif but got: "+li.received);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        System.out.println("EmptyDomainNotificationTest-main: Got the expected notif!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        System.out.println("EmptyDomainNotificationTest-main: remove the listener.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        mbsc.removeNotificationListener(mbean, li);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        // clean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        client.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        server.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        System.out.println("EmptyDomainNotificationTest-main: Bye.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
}