The example program of the HappyCoders.eu article "Java Flight Recorder (JFR)" (English, German).
Eight worker threads process randomly generated orders for a given number of seconds. A few spots in the code are written clumsily on purpose, the way they turn up in grown code – the article records the program with the Java Flight Recorder and finds each of them with jfr view and jfr print.
The example uses Java 25 language features and the JFR annotation @Contextual, so it needs Java 25 or newer. Build once with mvn compile, then record a run of 60 seconds from the repository root:
java -XX:StartFlightRecording:filename=orders.jfr,settings=profile \
-cp target/classes eu.happycoders.jfr.OrderProcessing 60
and open the recording, for example with:
jfr view hot-methods orders.jfr
The article shows many more ways to record and read: jcmd, JFR.view, report-on-exit, the disk and memory ring buffers, method timing, and -XX:+DebugNonSafepoints for exact line numbers in the samples.
OrderProcessing– the main class; its only argument is the running time in seconds (default 60)OrderService– processes one order: parses the quantity, checks the product against a list of blocked products, asks the shipping service for the shipping cost, gets an invoice number, formats the receipt, and keeps it in a cacheOrderGenerator– creates random orders with 50 to 149 lines; on average every fifth quantity comes with a unit, such as"3 pcs"ShippingService– an HTTP server in the same process that simulates a remote shipping-cost service answering after 20 msShippingClient– asks the shipping service withURL.openStream()InvoiceNumberGenerator– asynchronizedgenerator whose database round trip is simulated with a sleep of 5 msOrderEvent– the custom JFR eventeu.happycoders.Order, one per order, with the order ID as@ContextualfieldMonitoredOrderProcessing– runs the order processing and prints every order that takes longer than 80 ms, read live with aRecordingStream
results/outputs-jdk27.txt holds the outputs of the recording the article analyzes – jfr summary and the jfr view tables – recorded on JDK 27 on an Apple M5 Pro.
Stay up-to-date with the latest Java features with this free PDF Cheat Sheet!
- Avoid lengthy research with this concise overview of all Java versions from Java 10 to Java 27.
- Discover the innovative features of each new Java version, summarized on a single page.
- Impress your team with your up-to-date knowledge of the latest Java version.
👉 Download the free Java Versions PDF
(Hier geht's zur deutschen Version → Java-Versionen PDF)
With this free 1-page PDF cheat sheet, you'll always have the 7 most important complexity classes at a glance.
- Always choose the most efficient data structures and thus increase the performance of your applications.
- Be prepared for technical interviews and confidently present your algorithm knowledge.
- Become a sought-after problem solver and be known for systematically tackling complex problems.
👉 Download the free Big O Cheat Sheet
(Hier geht's zur deutschen Version → O-Notation Cheat Sheet)
👉 Want to stay on top of modern Java? Sign up for the HappyCoders newsletter – Modern Java: new versions & features, performance, and JVM insights – once a month.
(Hier geht's zur deutschen Version → HappyCoders-Newsletter deutsch)
🇩🇪 An alle Java-Entwickler:innen, die durch fundierte Kenntnisse über Datenstrukturen besseren Code schreiben wollen
Trage dich jetzt unverbindlich auf die Warteliste von „Mastering Data Structures in Java“ ein, und erhalte das beste Angebot!


