From d8fbfc707fbae7d47eb1e16c52f628f4911af4f3 Mon Sep 17 00:00:00 2001 From: cfredri4 Date: Wed, 18 Dec 2024 14:32:44 +0100 Subject: [PATCH] Handle RuntimeException when getting/setting JMS headers Currently JMSException is handled when getting/setting JMS headers. Some JMS providers will throw RuntimeException instead of JMSException when failing to get/set JMS headers. This change adds so that RuntimeException is also handled. --- .../jms/JmsProcessObservationContext.java | 6 +++++- .../jms/JmsPublishObservationContext.java | 13 ++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/micrometer-jakarta9/src/main/java/io/micrometer/jakarta9/instrument/jms/JmsProcessObservationContext.java b/micrometer-jakarta9/src/main/java/io/micrometer/jakarta9/instrument/jms/JmsProcessObservationContext.java index 4d8d29a892..3c28fe457f 100644 --- a/micrometer-jakarta9/src/main/java/io/micrometer/jakarta9/instrument/jms/JmsProcessObservationContext.java +++ b/micrometer-jakarta9/src/main/java/io/micrometer/jakarta9/instrument/jms/JmsProcessObservationContext.java @@ -16,6 +16,7 @@ package io.micrometer.jakarta9.instrument.jms; +import io.micrometer.common.util.internal.logging.WarnThenDebugLogger; import io.micrometer.observation.transport.ReceiverContext; import jakarta.jms.JMSException; import jakarta.jms.Message; @@ -33,12 +34,15 @@ */ public class JmsProcessObservationContext extends ReceiverContext { + private static final WarnThenDebugLogger logger = new WarnThenDebugLogger(JmsProcessObservationContext.class); + public JmsProcessObservationContext(Message receivedMessage) { super((message, key) -> { try { return message.getStringProperty(key); } - catch (JMSException exc) { + catch (JMSException | RuntimeException exc) { + logger.log("Failed get message property.", exc); return null; } }); diff --git a/micrometer-jakarta9/src/main/java/io/micrometer/jakarta9/instrument/jms/JmsPublishObservationContext.java b/micrometer-jakarta9/src/main/java/io/micrometer/jakarta9/instrument/jms/JmsPublishObservationContext.java index f16d2154d9..64e015c74f 100644 --- a/micrometer-jakarta9/src/main/java/io/micrometer/jakarta9/instrument/jms/JmsPublishObservationContext.java +++ b/micrometer-jakarta9/src/main/java/io/micrometer/jakarta9/instrument/jms/JmsPublishObservationContext.java @@ -17,6 +17,7 @@ package io.micrometer.jakarta9.instrument.jms; import io.micrometer.common.lang.Nullable; +import io.micrometer.common.util.internal.logging.WarnThenDebugLogger; import io.micrometer.observation.transport.SenderContext; import jakarta.jms.JMSException; import jakarta.jms.Message; @@ -33,15 +34,17 @@ */ public class JmsPublishObservationContext extends SenderContext { + private static final WarnThenDebugLogger logger = new WarnThenDebugLogger(JmsPublishObservationContext.class); + public JmsPublishObservationContext(@Nullable Message sendMessage) { super((message, key, value) -> { - try { - if (message != null) { + if (message != null) { + try { message.setStringProperty(key, value); } - } - catch (JMSException exc) { - // ignore + catch (JMSException | RuntimeException exc) { + logger.log("Failed set message property.", exc); + } } }); setCarrier(sendMessage);