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