1 /*
2  * Copyright (c) 2016, 2018, 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.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package jdk.jfr.api.recorder;
26 
27 import java.util.concurrent.CountDownLatch;
28 
29 import jdk.jfr.FlightRecorder;
30 import jdk.jfr.FlightRecorderListener;
31 import jdk.jfr.Recording;
32 import jdk.jfr.RecordingState;
33 
34 /**
35  * @test TestRecorderListener
36  *
37  * @key jfr
38  * @requires vm.hasJFR
39  * @run main/othervm jdk.jfr.api.recorder.TestRecorderListener
40  */
41 public class TestRecorderListener {
42 
43     static class Listener implements FlightRecorderListener {
44 
45         private final CountDownLatch latch = new CountDownLatch(1);
46         private final RecordingState waitFor;
47 
Listener(RecordingState state)48         public Listener(RecordingState state) {
49             waitFor = state;
50         }
51 
52         @Override
recordingStateChanged(Recording recording)53         public void recordingStateChanged(Recording recording) {
54             System.out.println("Listener: recording=" + recording.getName() + " state=" + recording.getState());
55             RecordingState rs = recording.getState();
56             if (rs == waitFor) {
57                 latch.countDown();
58             }
59         }
60 
waitFor()61         public void waitFor() throws InterruptedException {
62             latch.await();
63         }
64     }
65 
main(String... args)66     public static void main(String... args) throws Exception {
67         Listener recordingListener = new Listener(RecordingState.RUNNING);
68         FlightRecorder.addListener(recordingListener);
69 
70         Listener stoppedListener = new Listener(RecordingState.STOPPED);
71         FlightRecorder.addListener(stoppedListener);
72 
73         Listener finishedListener = new Listener(RecordingState.CLOSED);
74         FlightRecorder.addListener(finishedListener);
75 
76         Recording recording = new Recording();
77         if (recording.getState() != RecordingState.NEW) {
78             recording.close();
79             throw new Exception("New recording should be in NEW state");
80         }
81 
82         recording.start();
83         recordingListener.waitFor();
84 
85         recording.stop();
86         stoppedListener.waitFor();
87 
88         recording.close();
89         finishedListener.waitFor();
90 
91         testDefaultrecordingStateChangedListener();
92 
93     }
94 
95     private static class DummyListener implements FlightRecorderListener {
96 
97     }
98 
testDefaultrecordingStateChangedListener()99     private static void testDefaultrecordingStateChangedListener() {
100         FlightRecorder.addListener(new DummyListener());
101         Recording recording = new Recording();
102         recording.start();
103         recording.stop();
104         recording.close();
105     }
106 }
107