Path: blob/master/test/jdk/com/sun/jmx/remote/NotificationMarshalVersions/Client/Client.java
41161 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.Collections;24import java.util.HashSet;25import java.util.Set;26import java.util.concurrent.CountDownLatch;27import java.util.concurrent.TimeUnit;28import java.util.concurrent.atomic.AtomicBoolean;29import javax.management.MBeanServerConnection;30import javax.management.Notification;31import javax.management.NotificationListener;32import javax.management.ObjectName;33import javax.management.remote.JMXConnectionNotification;34import javax.management.remote.JMXConnector;35import javax.management.remote.JMXConnectorFactory;36import javax.management.remote.JMXServiceURL;3738public class Client {39public static void run(String url) throws Exception {40final int notifEmittedCnt = 10;41final CountDownLatch counter = new CountDownLatch(notifEmittedCnt);42final Set<Long> seqSet = Collections.synchronizedSet(new HashSet<Long>());43final AtomicBoolean duplNotification = new AtomicBoolean();4445JMXServiceURL serverUrl = new JMXServiceURL(url);4647ObjectName name = new ObjectName("test", "foo", "bar");48JMXConnector jmxConnector = JMXConnectorFactory.connect(serverUrl);49System.out.println("client connected");50jmxConnector.addConnectionNotificationListener(new NotificationListener() {51@Override52public void handleNotification(Notification notification, Object handback) {53System.out.println("connection notification: " + notification);54if (!seqSet.add(notification.getSequenceNumber())) {55duplNotification.set(true);56}57if (notification.getType().equals(JMXConnectionNotification.NOTIFS_LOST)) {58long lostNotifs = ((Long)((JMXConnectionNotification)notification).getUserData()).longValue();59for(int i=0;i<lostNotifs;i++) {60counter.countDown();61}62}63}64}, null, null);65MBeanServerConnection jmxServer = jmxConnector.getMBeanServerConnection();6667jmxServer.addNotificationListener(name, new NotificationListener() {68@Override69public void handleNotification(Notification notification, Object handback) {70System.out.println("client got: " + notification);71if (!seqSet.add(notification.getSequenceNumber())) {72duplNotification.set(true);73}74counter.countDown();75}76}, null, null);7778System.out.println("client invoking foo (" + notifEmittedCnt + " times)");79for(int i=0;i<notifEmittedCnt;i++) {80System.out.print(".");81jmxServer.invoke(name, "foo", new Object[]{}, new String[]{});82}83System.out.println();84try {85System.out.println("waiting for " + notifEmittedCnt + " notifications to arrive");86if (!counter.await(30, TimeUnit.SECONDS)) {87throw new InterruptedException();88}89if (duplNotification.get()) {90System.out.println("ERROR: received duplicated notifications");91throw new Error("received duplicated notifications");92}93System.out.println("\nshutting down client");94} catch (InterruptedException e) {95System.out.println("ERROR: notification processing thread interrupted");96throw new Error("notification thread interrupted unexpectedly");97}98}99}100101102