jdk/test/javax/management/notification/NotifExecutorTest.java
author darcy
Fri, 19 Aug 2011 17:42:24 -0700
changeset 10350 6d009f117062
parent 5506 202f599c92aa
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
4748706: typos in java.util.Observable documentation Reviewed-by: iris
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, 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: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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 4661545
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Tests to use an executor to send notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Shanliang JIANG
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @run clean NotifExecutorTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run build NotifExecutorTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run main NotifExecutorTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
// java imports
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
// JMX imports
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.remote.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class NotifExecutorTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        System.out.println("Tests to use an executor to send notifications.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        final ObjectName mbean = new ObjectName ("Default:name=NotificationEmitter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        final MyListener myLister = new MyListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        final NotificationListener nullListener = new NotificationListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                public void handleNotification(Notification n, Object hb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                    // nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        final Object[] params = new Object[] {new Integer(nb)};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        final String[] signatures = new String[] {"java.lang.Integer"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        // test with null executor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        System.out.println(">>> Test with a null executor.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        mbs.registerMBean(new NotificationEmitter(null), mbean);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        mbs.addNotificationListener(mbean, myLister, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        mbs.addNotificationListener(mbean, nullListener, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        mbs.invoke(mbean, "sendNotifications", params, signatures);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        check(nb, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        mbs.unregisterMBean(mbean);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        // test with an executor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        System.out.println(">>> Test with a executor.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        mbs.registerMBean(new NotificationEmitter(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                           new NotifExecutorTest.MyExecutor()), mbean);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        mbs.addNotificationListener(mbean, myLister, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        mbs.addNotificationListener(mbean, nullListener, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        mbs.invoke(mbean, "sendNotifications", params, signatures);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        check(nb, nb*2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        // test without listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        System.out.println(">>> Test without listener.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        mbs.removeNotificationListener(mbean, myLister);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        mbs.removeNotificationListener(mbean, nullListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        mbs.invoke(mbean, "sendNotifications", params, signatures);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        check(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private static void check(int notifs, int called) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        // Waiting...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            for (int i = 0; i < 10; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                if (receivedNotifs < notifs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    lock.wait(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        // Waiting again to ensure no more notifs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        Thread.sleep(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        // checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            if (receivedNotifs != notifs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                throw new RuntimeException("The listener expected to receive " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                                           notifs + " notifs, but got " + receivedNotifs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                System.out.println(">>> The listener recieved as expected: "+receivedNotifs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            if (calledTimes != called) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                throw new RuntimeException("The notif executor should be called " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                           called + " times, but got " + calledTimes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                System.out.println(">>> The executor was called as expected: "+calledTimes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        // clean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        receivedNotifs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        calledTimes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    //--------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    // private classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    //--------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private static class MyListener implements NotificationListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        public void handleNotification(Notification notif, Object handback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                if(++receivedNotifs >= nb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    lock.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public static class NotificationEmitter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        extends NotificationBroadcasterSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        implements NotificationEmitterMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        public NotificationEmitter(Executor executor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            super(executor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
         * Send a Notification object with the specified times.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
         * The sequence number will be from zero to times-1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
         * @param nb The number of notifications to send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        public void sendNotifications(Integer nb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            System.out.println(">>> NotificationEmitter: asked to send " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                               "notifications: " + nb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            Notification notif;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            for (int i = 1; i <= nb.intValue(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                notif = new Notification(null, this, ++seqno);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                super.sendNotification(notif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public interface NotificationEmitterMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        public void sendNotifications(Integer nb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public static class MyExecutor extends ThreadPoolExecutor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        public MyExecutor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            super(1, 1, 1L, TimeUnit.MILLISECONDS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                  new ArrayBlockingQueue(nb*5));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        public synchronized void execute(Runnable job) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                calledTimes++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            super.execute(job);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    private static int nb = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    private static int receivedNotifs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    private static int[] lock = new int[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    private static volatile long seqno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    private static int calledTimes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
}