Path: blob/master/test/jdk/javax/management/notification/NotifExecutorTest.java
41149 views
/*1* Copyright (c) 2005, 2015, 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*/2223/*24* @test25* @bug 466154526* @summary Tests to use an executor to send notifications.27* @author Shanliang JIANG28*29* @run clean NotifExecutorTest30* @run build NotifExecutorTest31* @run main NotifExecutorTest32*/3334// java imports35//36import java.io.IOException;37import java.util.concurrent.*;3839// JMX imports40//41import javax.management.*;42import javax.management.remote.*;4344public class NotifExecutorTest {4546public static void main(String[] args) throws Exception {47System.out.println("Tests to use an executor to send notifications.");4849final MBeanServer mbs = MBeanServerFactory.createMBeanServer();50final ObjectName mbean = new ObjectName ("Default:name=NotificationEmitter");51final MyListener myLister = new MyListener();52final NotificationListener nullListener = new NotificationListener() {53public void handleNotification(Notification n, Object hb) {54// nothing55}56};57final Object[] params = new Object[] {new Integer(nb)};58final String[] signatures = new String[] {"java.lang.Integer"};5960// test with null executor61System.out.println(">>> Test with a null executor.");62mbs.registerMBean(new NotificationEmitter(null), mbean);6364mbs.addNotificationListener(mbean, myLister, null, null);65mbs.addNotificationListener(mbean, nullListener, null, null);6667mbs.invoke(mbean, "sendNotifications", params, signatures);68check(nb, 0);6970mbs.unregisterMBean(mbean);7172// test with an executor73System.out.println(">>> Test with a executor.");74mbs.registerMBean(new NotificationEmitter(75new NotifExecutorTest.MyExecutor()), mbean);76mbs.addNotificationListener(mbean, myLister, null, null);77mbs.addNotificationListener(mbean, nullListener, null, null);7879mbs.invoke(mbean, "sendNotifications", params, signatures);8081check(nb, nb*2);8283// test without listener84System.out.println(">>> Test without listener.");8586mbs.removeNotificationListener(mbean, myLister);87mbs.removeNotificationListener(mbean, nullListener);8889mbs.invoke(mbean, "sendNotifications", params, signatures);90check(0, 0);91}9293private static void check(int notifs, int called) throws Exception {94// Waiting...95synchronized (lock) {96for (int i = 0; i < 10; i++) {97if (receivedNotifs < notifs) {98lock.wait(1000);99}100}101}102103// Waiting again to ensure no more notifs104//105Thread.sleep(1000);106107// checking108synchronized (lock) {109if (receivedNotifs != notifs) {110throw new RuntimeException("The listener expected to receive " +111notifs + " notifs, but got " + receivedNotifs);112} else {113System.out.println(">>> The listener recieved as expected: "+receivedNotifs);114}115116if (calledTimes != called) {117throw new RuntimeException("The notif executor should be called " +118called + " times, but got " + calledTimes);119} else {120System.out.println(">>> The executor was called as expected: "+calledTimes);121}122}123124// clean125receivedNotifs = 0;126calledTimes = 0;127}128129130//--------------------------131// private classes132//--------------------------133private static class MyListener implements NotificationListener {134public void handleNotification(Notification notif, Object handback) {135synchronized(lock) {136if(++receivedNotifs >= nb) {137lock.notifyAll();138}139}140}141}142143public static class NotificationEmitter144extends NotificationBroadcasterSupport145implements NotificationEmitterMBean {146147public NotificationEmitter(Executor executor) {148super(executor);149}150151/**152* Send a Notification object with the specified times.153* The sequence number will be from zero to times-1.154*155* @param nb The number of notifications to send156*/157public void sendNotifications(Integer nb) {158System.out.println(">>> NotificationEmitter: asked to send " +159"notifications: " + nb);160161Notification notif;162for (int i = 1; i <= nb.intValue(); i++) {163notif = new Notification(null, this, ++seqno);164super.sendNotification(notif);165}166}167}168169public interface NotificationEmitterMBean {170public void sendNotifications(Integer nb);171}172173public static class MyExecutor extends ThreadPoolExecutor {174public MyExecutor() {175super(1, 1, 1L, TimeUnit.MILLISECONDS,176new ArrayBlockingQueue(nb*5));177}178179public synchronized void execute(Runnable job) {180synchronized(lock) {181calledTimes++;182}183184super.execute(job);185}186}187188private static int nb = 10;189private static int receivedNotifs = 0;190private static int[] lock = new int[0];191private static volatile long seqno;192193private static int calledTimes = 0;194}195196197