Skip to content

Commit

Permalink
Handle all exceptions when getting/setting JMS headers
Browse files Browse the repository at this point in the history
Some JMS providers will throw RuntimeException instead of JMSException when failing to get/set JMS headers.

Closes gh-5746

Co-authored-by: Jonatan Ivanov <[email protected]>
  • Loading branch information
cfredri4 and jonatan-ivanov committed Jan 10, 2025
1 parent 318faae commit be6f962
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

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;

/**
Expand All @@ -33,12 +33,16 @@
*/
public class JmsProcessObservationContext extends ReceiverContext<Message> {

private static final WarnThenDebugLogger logger = new WarnThenDebugLogger(JmsProcessObservationContext.class);

public JmsProcessObservationContext(Message receivedMessage) {
super((message, key) -> {
try {
return message.getStringProperty(key);
}
catch (JMSException exc) {
// Some JMS providers throw exceptions other than JMSException
catch (Exception exc) {
logger.log("Failed to get message property.", exc);
return null;
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
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;

/**
Expand All @@ -33,15 +33,18 @@
*/
public class JmsPublishObservationContext extends SenderContext<Message> {

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
// Some JMS providers throw exceptions other than JMSException
catch (Exception exc) {
logger.log("Failed to set message property.", exc);
}
}
});
setCarrier(sendMessage);
Expand Down

0 comments on commit be6f962

Please sign in to comment.