This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 3760ca78dab42211c82a9d7d838bff02af9137fe
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Thu Jan 21 11:05:01 2021 +0100

    CAMEL-16056: Added camel-jfr for java flight recorder integration
---
 .../jfr/FlightRecorderStartupStepRecorder.java     | 61 +++++++++-------------
 .../camel/impl/engine/AbstractCamelContext.java    |  3 +-
 2 files changed, 28 insertions(+), 36 deletions(-)

diff --git 
a/components/camel-jfr/src/main/java/org/apache/camel/startup/jfr/FlightRecorderStartupStepRecorder.java
 
b/components/camel-jfr/src/main/java/org/apache/camel/startup/jfr/FlightRecorderStartupStepRecorder.java
index 40f0f08..333a882 100644
--- 
a/components/camel-jfr/src/main/java/org/apache/camel/startup/jfr/FlightRecorderStartupStepRecorder.java
+++ 
b/components/camel-jfr/src/main/java/org/apache/camel/startup/jfr/FlightRecorderStartupStepRecorder.java
@@ -53,15 +53,21 @@ public class FlightRecorderStartupStepRecorder extends 
DefaultStartupStepRecorde
             Configuration config = 
Configuration.getConfiguration(getRecordingProfile());
             rec = new Recording(config);
             rec.setName("Camel Recording");
-            if (getStartupRecorderDuration() == 0) {
+
+            if (!"false".equals(getRecordingDir())) {
+                // recording to disk can be turned off by setting to false
                 Path dir = getRecordingDir() != null ? 
Paths.get(getRecordingDir()) : Paths.get(System.getenv().get("HOME"));
                 Path file = Files.createTempFile(dir, "camel-recording", 
".jfr");
-                rec.setDumpOnExit(true);
+                // when stopping then the recording is automatic dumped by 
flight recorder
                 rec.setDestination(file);
-                LOG.info("Java flight recorder will be saved to file on JVM 
exit: {}", file);
             }
 
-            if (getStartupRecorderDuration() > 0) {
+            if (getStartupRecorderDuration() == 0) {
+                if (rec.getDestination() != null) {
+                    rec.setDumpOnExit(true);
+                    LOG.info("Java flight recorder will be saved to file on 
JVM exit: {}", rec.getDestination());
+                }
+            } else if (getStartupRecorderDuration() > 0) {
                 
rec.setDuration(Duration.ofSeconds(getStartupRecorderDuration()));
                 LOG.info("Starting Java flight recorder with profile: {} and 
duration: {} seconds", getRecordingProfile(),
                         getStartupRecorderDuration());
@@ -71,8 +77,8 @@ public class FlightRecorderStartupStepRecorder extends 
DefaultStartupStepRecorde
                     @Override
                     public void recordingStateChanged(Recording recording) {
                         if (recording == rec && 
recording.getState().equals(RecordingState.STOPPED)) {
-                            LOG.info("Stopping Java flight recorder after {} 
seconds elapsed", getStartupRecorderDuration());
-                            dumpRecording();
+                            LOG.info("Java flight recorder stopped after {} 
seconds and saved to file: {}",
+                                    getStartupRecorderDuration(), 
rec.getDestination());
                         }
                     }
                 };
@@ -88,37 +94,22 @@ public class FlightRecorderStartupStepRecorder extends 
DefaultStartupStepRecorde
     public void doStop() throws Exception {
         super.doStop();
 
-        if (rec != null && getStartupRecorderDuration() != 0) {
-            dumpRecording();
-        }
-    }
-
-    protected void dumpRecording() {
-        if (!"false".equals(getRecordingDir())) {
-            try {
-                Path dir = getRecordingDir() != null ? 
Paths.get(getRecordingDir()) : Paths.get(System.getenv().get("HOME"));
-                Path file = Files.createTempFile(dir, "camel-recording-", 
".jfr");
-                if (rec.getState().equals(RecordingState.RUNNING)) {
-                    // need to do GC to capture details to the recording 
(specially when its short running)
-                    LOG.info("Stopping Java flight recorder");
-                    System.gc();
-                    rec.stop();
-                }
-                if (rec.getState().equals(RecordingState.STOPPED)) {
-                    rec.dump(file);
-                    LOG.info("Java flight recorder saved to file: {}", file);
-                }
-            } catch (Exception e) {
-                LOG.warn("Error saving Java flight recorder recording to 
file", e);
+        if (rec != null) {
+            // if < 0 then manual stop the recording
+            if (getStartupRecorderDuration() < 0) {
+                LOG.debug("Stopping Java flight recorder");
+                // do GC before stopping to force flushing data into the 
recording
+                System.gc();
+                rec.stop();
+                LOG.info("Java flight recorder stopped and saved to file: {}", 
rec.getDestination());
             }
+            FlightRecorder.unregister(FlightRecorderStartupStep.class);
+            if (frl != null) {
+                FlightRecorder.removeListener(frl);
+            }
+            rec = null;
+            frl = null;
         }
-        FlightRecorder.unregister(FlightRecorderStartupStep.class);
-        if (frl != null) {
-            FlightRecorder.removeListener(frl);
-        }
-
-        rec = null;
-        frl = null;
     }
 
     public FlightRecorderStartupStepRecorder() {
diff --git 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
index 91a1b36..e5fa150 100644
--- 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
+++ 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
@@ -2742,7 +2742,8 @@ public abstract class AbstractCamelContext extends 
BaseService
 
         startupStepRecorder.endStep(step);
 
-        if (startupStepRecorder.getStartupRecorderDuration() == 0) {
+        // if we should only record the startup process then stop it right 
after started
+        if (startupStepRecorder.getStartupRecorderDuration() < 0) {
             startupStepRecorder.stop();
         }
     }

Reply via email to