2007-12-01 00:00:00 +00:00
|
|
|
/*
|
2015-05-07 07:11:49 +00:00
|
|
|
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
|
2007-12-01 00:00:00 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
2010-05-25 22:58:33 +00:00
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
2007-12-01 00:00:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @test
|
|
|
|
* @bug 4911721
|
|
|
|
* @summary test on add/remove NotificationListener
|
|
|
|
* @author Shanliang JIANG
|
2017-03-16 05:48:59 +00:00
|
|
|
*
|
2007-12-01 00:00:00 +00:00
|
|
|
* @run clean DiffHBTest
|
|
|
|
* @run build DiffHBTest
|
|
|
|
* @run main DiffHBTest
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
import javax.management.*;
|
|
|
|
import javax.management.remote.*;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This test registeres an unique listener with two different handbacks,
|
|
|
|
* it expects to receive a same notification two times.
|
|
|
|
*/
|
|
|
|
public class DiffHBTest {
|
|
|
|
private static final String[] protocols = {"rmi", "iiop", "jmxmp"};
|
|
|
|
|
|
|
|
private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
|
|
|
|
private static ObjectName delegateName;
|
|
|
|
private static ObjectName timerName;
|
|
|
|
|
|
|
|
public static final String[] hbs = new String[] {"0", "1"};
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
System.out.println(">>> test on one listener with two different handbacks.");
|
|
|
|
|
|
|
|
delegateName = new ObjectName("JMImplementation:type=MBeanServerDelegate");
|
|
|
|
timerName = new ObjectName("MBean:name=Timer");
|
|
|
|
|
2008-07-31 13:31:13 +00:00
|
|
|
String errors = "";
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
for (int i = 0; i < protocols.length; i++) {
|
2008-07-31 13:31:13 +00:00
|
|
|
final String s = test(protocols[i]);
|
|
|
|
if (s != null) {
|
|
|
|
if ("".equals(errors)) {
|
|
|
|
errors = "Failed to " + protocols[i] + ": "+s;
|
2007-12-01 00:00:00 +00:00
|
|
|
} else {
|
2008-07-31 13:31:13 +00:00
|
|
|
errors = "\tFailed to " + protocols[i] + ": "+s;
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-31 13:31:13 +00:00
|
|
|
if ("".equals(errors)) {
|
|
|
|
System.out.println(">>> Passed!");
|
2007-12-01 00:00:00 +00:00
|
|
|
} else {
|
2008-07-31 13:31:13 +00:00
|
|
|
System.out.println(">>> Failed!");
|
|
|
|
|
|
|
|
throw new RuntimeException(errors);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-31 13:31:13 +00:00
|
|
|
private static String test(String proto) throws Exception {
|
2009-10-21 15:33:18 +00:00
|
|
|
System.out.println(">>> Test for protocol " + proto);
|
2007-12-01 00:00:00 +00:00
|
|
|
JMXServiceURL u = new JMXServiceURL(proto, null, 0);
|
|
|
|
JMXConnectorServer server;
|
|
|
|
JMXConnector client;
|
|
|
|
|
|
|
|
try {
|
2008-07-31 13:31:13 +00:00
|
|
|
server =
|
2009-10-21 15:33:18 +00:00
|
|
|
JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);
|
2007-12-01 00:00:00 +00:00
|
|
|
server.start();
|
2008-07-31 13:31:13 +00:00
|
|
|
JMXServiceURL addr = server.getAddress();
|
|
|
|
client = JMXConnectorFactory.connect(addr, null);
|
|
|
|
} catch (Exception e) {
|
|
|
|
// not support
|
|
|
|
System.out.println(">>> not support: " + proto);
|
|
|
|
return null;
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2008-07-31 13:31:13 +00:00
|
|
|
MBeanServerConnection mserver = client.getMBeanServerConnection();
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2008-07-31 13:31:13 +00:00
|
|
|
System.out.print(">>>\t");
|
|
|
|
for (int i=0; i<5; i++) {
|
|
|
|
System.out.print(i + "\t");
|
|
|
|
final MyListener dummyListener = new MyListener();
|
|
|
|
mserver.addNotificationListener(
|
|
|
|
delegateName, dummyListener, null, hbs[0]);
|
|
|
|
mserver.addNotificationListener(
|
|
|
|
delegateName, dummyListener, null, hbs[1]);
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2008-07-31 13:31:13 +00:00
|
|
|
mserver.createMBean("javax.management.timer.Timer", timerName);
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2008-07-31 13:31:13 +00:00
|
|
|
long remainingTime = waitingTime;
|
|
|
|
final long startTime = System.currentTimeMillis();
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2008-07-31 13:31:13 +00:00
|
|
|
try {
|
|
|
|
synchronized(dummyListener) {
|
|
|
|
while (!dummyListener.done && remainingTime > 0) {
|
|
|
|
dummyListener.wait(remainingTime);
|
|
|
|
remainingTime = waitingTime -
|
2007-12-01 00:00:00 +00:00
|
|
|
(System.currentTimeMillis() - startTime);
|
|
|
|
}
|
|
|
|
|
2008-07-31 13:31:13 +00:00
|
|
|
if (dummyListener.errorInfo != null) {
|
|
|
|
return dummyListener.errorInfo;
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
2008-07-31 13:31:13 +00:00
|
|
|
} finally {
|
|
|
|
//System.out.println("Unregister: "+i);
|
|
|
|
mserver.unregisterMBean(timerName);
|
|
|
|
mserver.removeNotificationListener(delegateName, dummyListener);
|
|
|
|
}
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2008-07-31 13:31:13 +00:00
|
|
|
System.out.println("");
|
|
|
|
client.close();
|
|
|
|
server.stop();
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2008-07-31 13:31:13 +00:00
|
|
|
return null;
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2008-07-31 13:31:13 +00:00
|
|
|
private static class MyListener implements NotificationListener {
|
|
|
|
public boolean done = false;
|
|
|
|
public String errorInfo = null;
|
|
|
|
|
|
|
|
private int received = 0;
|
|
|
|
private MBeanServerNotification receivedNotif = null;
|
|
|
|
private Object receivedHB = null;
|
|
|
|
public void handleNotification(Notification n, Object o) {
|
|
|
|
if (!(n instanceof MBeanServerNotification)) {
|
|
|
|
failed("Received an unexpected notification: "+n);
|
|
|
|
return;
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2008-07-31 13:31:13 +00:00
|
|
|
if (!hbs[0].equals(o) && !hbs[1].equals(o)) {
|
|
|
|
failed("Unkown handback: "+o);
|
|
|
|
return;
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2008-07-31 13:31:13 +00:00
|
|
|
// what we need
|
|
|
|
final MBeanServerNotification msn = (MBeanServerNotification)n;
|
|
|
|
if (!(MBeanServerNotification.REGISTRATION_NOTIFICATION.equals(
|
|
|
|
msn.getType())) ||
|
|
|
|
!msn.getMBeanName().equals(timerName)) {
|
|
|
|
return;
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2008-07-31 13:31:13 +00:00
|
|
|
synchronized(this) {
|
|
|
|
received++;
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2008-07-31 13:31:13 +00:00
|
|
|
if (received == 1) { // first time
|
|
|
|
receivedNotif = msn;
|
|
|
|
receivedHB = o;
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2008-07-31 13:31:13 +00:00
|
|
|
return;
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2008-07-31 13:31:13 +00:00
|
|
|
if (received > 2) {
|
|
|
|
failed("Expect to receive 2 notifs, but get "+received);
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2008-07-31 13:31:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2008-07-31 13:31:13 +00:00
|
|
|
// second time
|
|
|
|
if (receivedHB.equals(o)) {
|
|
|
|
failed("Got same handback twice: "+o);
|
|
|
|
} else if(!hbs[0].equals(o) && !hbs[1].equals(o)) {
|
|
|
|
failed("Unknown handback: "+o);
|
|
|
|
} else if (receivedNotif.getSequenceNumber() !=
|
|
|
|
msn.getSequenceNumber()) {
|
|
|
|
failed("expected to receive:\n"
|
|
|
|
+receivedNotif
|
|
|
|
+"\n but got\n"+msn);
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2008-07-31 13:31:13 +00:00
|
|
|
// passed
|
|
|
|
done = true;
|
|
|
|
this.notify();
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2008-07-31 13:31:13 +00:00
|
|
|
private void failed(String errorInfo) {
|
|
|
|
this.errorInfo = errorInfo;
|
|
|
|
done = true;
|
|
|
|
|
|
|
|
this.notify();
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2008-07-31 13:31:13 +00:00
|
|
|
private final static long waitingTime = 2000;
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|