Created
May 4, 2022 22:33
-
-
Save linux-china/5e0e960f8564bd778b49e44d68105a86 to your computer and use it in GitHub Desktop.
JfrOverRSocketController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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