Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/notification/NotifExecutorTest.java
38840 views
1
/*
2
* Copyright (c) 2005, 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
24
/*
25
* @test
26
* @bug 4661545
27
* @summary Tests to use an executor to send notifications.
28
* @author Shanliang JIANG
29
* @run clean NotifExecutorTest
30
* @run build NotifExecutorTest
31
* @run main NotifExecutorTest
32
*/
33
34
// java imports
35
//
36
import java.io.IOException;
37
import java.util.concurrent.*;
38
39
// JMX imports
40
//
41
import javax.management.*;
42
import javax.management.remote.*;
43
44
public class NotifExecutorTest {
45
46
public static void main(String[] args) throws Exception {
47
System.out.println("Tests to use an executor to send notifications.");
48
49
final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
50
final ObjectName mbean = new ObjectName ("Default:name=NotificationEmitter");
51
final MyListener myLister = new MyListener();
52
final NotificationListener nullListener = new NotificationListener() {
53
public void handleNotification(Notification n, Object hb) {
54
// nothing
55
}
56
};
57
final Object[] params = new Object[] {new Integer(nb)};
58
final String[] signatures = new String[] {"java.lang.Integer"};
59
60
// test with null executor
61
System.out.println(">>> Test with a null executor.");
62
mbs.registerMBean(new NotificationEmitter(null), mbean);
63
64
mbs.addNotificationListener(mbean, myLister, null, null);
65
mbs.addNotificationListener(mbean, nullListener, null, null);
66
67
mbs.invoke(mbean, "sendNotifications", params, signatures);
68
check(nb, 0);
69
70
mbs.unregisterMBean(mbean);
71
72
// test with an executor
73
System.out.println(">>> Test with a executor.");
74
mbs.registerMBean(new NotificationEmitter(
75
new NotifExecutorTest.MyExecutor()), mbean);
76
mbs.addNotificationListener(mbean, myLister, null, null);
77
mbs.addNotificationListener(mbean, nullListener, null, null);
78
79
mbs.invoke(mbean, "sendNotifications", params, signatures);
80
81
check(nb, nb*2);
82
83
// test without listener
84
System.out.println(">>> Test without listener.");
85
86
mbs.removeNotificationListener(mbean, myLister);
87
mbs.removeNotificationListener(mbean, nullListener);
88
89
mbs.invoke(mbean, "sendNotifications", params, signatures);
90
check(0, 0);
91
}
92
93
private static void check(int notifs, int called) throws Exception {
94
// Waiting...
95
synchronized (lock) {
96
for (int i = 0; i < 10; i++) {
97
if (receivedNotifs < notifs) {
98
lock.wait(1000);
99
}
100
}
101
}
102
103
// Waiting again to ensure no more notifs
104
//
105
Thread.sleep(1000);
106
107
// checking
108
synchronized (lock) {
109
if (receivedNotifs != notifs) {
110
throw new RuntimeException("The listener expected to receive " +
111
notifs + " notifs, but got " + receivedNotifs);
112
} else {
113
System.out.println(">>> The listener recieved as expected: "+receivedNotifs);
114
}
115
116
if (calledTimes != called) {
117
throw new RuntimeException("The notif executor should be called " +
118
called + " times, but got " + calledTimes);
119
} else {
120
System.out.println(">>> The executor was called as expected: "+calledTimes);
121
}
122
}
123
124
// clean
125
receivedNotifs = 0;
126
calledTimes = 0;
127
}
128
129
130
//--------------------------
131
// private classes
132
//--------------------------
133
private static class MyListener implements NotificationListener {
134
public void handleNotification(Notification notif, Object handback) {
135
synchronized(lock) {
136
if(++receivedNotifs >= nb) {
137
lock.notifyAll();
138
}
139
}
140
}
141
}
142
143
public static class NotificationEmitter
144
extends NotificationBroadcasterSupport
145
implements NotificationEmitterMBean {
146
147
public NotificationEmitter(Executor executor) {
148
super(executor);
149
}
150
151
/**
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 send
156
*/
157
public void sendNotifications(Integer nb) {
158
System.out.println(">>> NotificationEmitter: asked to send " +
159
"notifications: " + nb);
160
161
Notification notif;
162
for (int i = 1; i <= nb.intValue(); i++) {
163
notif = new Notification(null, this, ++seqno);
164
super.sendNotification(notif);
165
}
166
}
167
}
168
169
public interface NotificationEmitterMBean {
170
public void sendNotifications(Integer nb);
171
}
172
173
public static class MyExecutor extends ThreadPoolExecutor {
174
public MyExecutor() {
175
super(1, 1, 1L, TimeUnit.MILLISECONDS,
176
new ArrayBlockingQueue(nb*5));
177
}
178
179
public synchronized void execute(Runnable job) {
180
synchronized(lock) {
181
calledTimes++;
182
}
183
184
super.execute(job);
185
}
186
}
187
188
private static int nb = 10;
189
private static int receivedNotifs = 0;
190
private static int[] lock = new int[0];
191
private static volatile long seqno;
192
193
private static int calledTimes = 0;
194
}
195
196