
import java.time.Instant;
import java.util.concurrent.atomic.AtomicLong;

import jdk.jfr.Configuration;
import jdk.jfr.Recording;
import jdk.jfr.consumer.EventStream;

public class Repro {

	public static void main(String[] args) throws Exception {
		Configuration c = Configuration.getConfiguration("default");
		Instant start = Instant.now().minusSeconds(100);
		long max = 0;
		try (Recording r = new Recording(c)) {
			r.start();
			while (true) {
				Instant end = Instant.now();
				AtomicLong counter = new AtomicLong();
				try (EventStream s = EventStream.openRepository()) {
					s.setStartTime(start);
					s.setEndTime(end);
					s.onEvent(e -> {
						counter.incrementAndGet();
					});
					s.start();
				}
				if (counter.get() >= max) {
					max = counter.get();
				} else {
					throw new Exception("Expected number of events to increase " + counter.get());
				}
				System.out.println(max);
				Thread.sleep(1000);
			}
		}
	}
}
