Skip to content

Instantly share code, notes, and snippets.

@linux-china
Created May 4, 2022 22:33
Show Gist options
  • Save linux-china/5e0e960f8564bd778b49e44d68105a86 to your computer and use it in GitHub Desktop.
Save linux-china/5e0e960f8564bd778b49e44d68105a86 to your computer and use it in GitHub Desktop.
JfrOverRSocketController.java
import jdk.jfr.consumer.RecordingStream;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.stereotype.Controller;
import reactor.core.publisher.Flux;
import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
@Controller
public class JfrOverRSocketController {
@MessageMapping("jfr")
public Flux<String> jfr(JfrRequest request) {
return Flux.create(sink -> {
var rs = new RecordingStream();
rs.setEndTime(Instant.now().plus(request.getDuration(), ChronoUnit.SECONDS));
rs.onClose(sink::complete);
rs.onError(sink::error);
for (String type : request.getTypes()) {
rs.enable(type).withPeriod(Duration.ofSeconds(request.getPeriod()));
}
rs.onEvent(event -> {
sink.next(event.toString());
});
rs.startAsync();
});
}
}
public class JfrRequest {
private List<String> types;
private int duration = 15;
private int period = 1;
public List<String> getTypes() {
return types;
}
public void setTypes(List<String> types) {
this.types = types;
}
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
public int getPeriod() {
return period;
}
public void setPeriod(int period) {
this.period = period;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment