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. 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 package jdk.jfr.api.recorder; 24 25 import java.util.concurrent.CountDownLatch; 26 27 import jdk.jfr.FlightRecorder; 28 import jdk.jfr.FlightRecorderListener; 29 import jdk.jfr.Recording; 30 import jdk.jfr.RecordingState; 31 32 /** 33 * @test TestRecorderListener 34 * 35 * @key jfr 36 * @requires vm.hasJFR 37 * @run main/othervm jdk.jfr.api.recorder.TestRecorderListener 38 */ 39 public class TestRecorderListener { 40 41 static class Listener implements FlightRecorderListener { 42 43 private final CountDownLatch latch = new CountDownLatch(1); 44 private final RecordingState waitFor; 45 Listener(RecordingState state)46 public Listener(RecordingState state) { 47 waitFor = state; 48 } 49 50 @Override recordingStateChanged(Recording recording)51 public void recordingStateChanged(Recording recording) { 52 System.out.println("Listener: recording=" + recording.getName() + " state=" + recording.getState()); 53 RecordingState rs = recording.getState(); 54 if (rs == waitFor) { 55 latch.countDown(); 56 } 57 } 58 waitFor()59 public void waitFor() throws InterruptedException { 60 latch.await(); 61 } 62 } 63 main(String... args)64 public static void main(String... args) throws Exception { 65 Listener recordingListener = new Listener(RecordingState.RUNNING); 66 FlightRecorder.addListener(recordingListener); 67 68 Listener stoppedListener = new Listener(RecordingState.STOPPED); 69 FlightRecorder.addListener(stoppedListener); 70 71 Listener finishedListener = new Listener(RecordingState.CLOSED); 72 FlightRecorder.addListener(finishedListener); 73 74 Recording recording = new Recording(); 75 if (recording.getState() != RecordingState.NEW) { 76 recording.close(); 77 throw new Exception("New recording should be in NEW state"); 78 } 79 80 recording.start(); 81 recordingListener.waitFor(); 82 83 recording.stop(); 84 stoppedListener.waitFor(); 85 86 recording.close(); 87 finishedListener.waitFor(); 88 89 testDefaultrecordingStateChangedListener(); 90 91 } 92 93 private static class DummyListener implements FlightRecorderListener { 94 95 } 96 testDefaultrecordingStateChangedListener()97 private static void testDefaultrecordingStateChangedListener() { 98 FlightRecorder.addListener(new DummyListener()); 99 Recording recording = new Recording(); 100 recording.start(); 101 recording.stop(); 102 recording.close(); 103 } 104 } 105