davsclaus commented on code in PR #24663: URL: https://github.com/apache/camel/pull/24663#discussion_r3571682438
########## core/camel-console/src/main/java/org/apache/camel/impl/console/HeapDumpDevConsole.java: ########## @@ -0,0 +1,88 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.impl.console; + +import java.io.File; +import java.lang.management.ManagementFactory; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Map; + +import com.sun.management.HotSpotDiagnosticMXBean; +import org.apache.camel.spi.Configurer; Review Comment: Fixed — replaced the direct `com.sun.management.HotSpotDiagnosticMXBean` import with string-based `MBeanServer.invoke()` via `ObjectName("com.sun.management:type=HotSpotDiagnostic")`, matching the pattern used by `HeapHistogramDevConsole`. ########## core/camel-console/src/main/java/org/apache/camel/impl/console/HeapDumpDevConsole.java: ########## @@ -0,0 +1,88 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.impl.console; + +import java.io.File; +import java.lang.management.ManagementFactory; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Map; + +import com.sun.management.HotSpotDiagnosticMXBean; +import org.apache.camel.spi.Configurer; +import org.apache.camel.spi.Metadata; +import org.apache.camel.spi.annotations.DevConsole; +import org.apache.camel.support.console.AbstractDevConsole; +import org.apache.camel.util.json.JsonObject; + +@DevConsole(name = "heap-dump", displayName = "Heap Dump", + description = "Write a heap dump (.hprof) file for deep memory analysis") +@Configurer(extended = true) +public class HeapDumpDevConsole extends AbstractDevConsole { + + private static final DateTimeFormatter TIMESTAMP = DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss"); + + @Metadata(label = "action", description = "File name for the heap dump (without .hprof extension)", + javaType = "java.lang.String") + public static final String NAME = "name"; + + @Metadata(label = "action", description = "Whether to dump only live objects (default true)", + defaultValue = "true", javaType = "java.lang.Boolean") + public static final String LIVE = "live"; + + public HeapDumpDevConsole() { + super("jvm", "heap-dump", "Heap Dump", "Write a heap dump (.hprof) file for deep memory analysis"); + } + + @Override + protected String doCallText(Map<String, Object> options) { + JsonObject json = doCallJson(options); + String error = json.getString("error"); + if (error != null) { + return "Heap dump failed: " + error; + } + return "Heap dump written to: " + json.getString("file") + " (" + json.getLong("size") + " bytes)"; + } + + @Override + protected JsonObject doCallJson(Map<String, Object> options) { + JsonObject root = new JsonObject(); + + String name = optionString(options, NAME); + if (name == null || name.isBlank()) { Review Comment: Fixed — added `name = Path.of(name).getFileName().toString()` to strip path separators before the name is passed to `dumpHeap()`. ########## dsl/camel-jbang/camel-jbang-plugin-tui/src/main/java/org/apache/camel/dsl/jbang/core/commands/tui/MemoryTab.java: ########## @@ -437,6 +441,47 @@ private static Span computeTrendSpan(LinkedList<Long> hist, long heapCeiling) { public void renderFooter(List<Span> spans) { hint(spans, "Esc", "back"); hint(spans, "g", "gc"); + hint(spans, "h", "heap dump"); + } + + private void triggerHeapDump() { + IntegrationInfo info = ctx.findSelectedIntegration(); + if (info == null) { + return; + } + notify("Writing heap dump...", false); + String pid = info.pid; + Thread t = new Thread(() -> { + Path outputFile = ctx.getOutputFile(pid); + PathUtils.deleteFile(outputFile); + JsonObject root = new JsonObject(); + root.put("action", "heap-dump"); + Path actionFile = ctx.getActionFile(pid); + PathUtils.writeTextSafely(root.toJson(), actionFile); Review Comment: Fixed — extracted the shared logic into `TuiHelper.triggerHeapDump(MonitorContext, BiConsumer<String, Boolean>)` and both `MemoryTab` and `MemoryLeakTab` now delegate to it. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
