-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JmsTrace holds a reference to the Jms message
- Loading branch information
1 parent
caae6f8
commit 0459ff4
Showing
8 changed files
with
80 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 0 additions & 2 deletions
2
...-jms/src/main/java/io/smallrye/reactive/messaging/jms/tracing/JmsAttributesExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 43 additions & 11 deletions
54
...tive-messaging-jms/src/main/java/io/smallrye/reactive/messaging/jms/tracing/JmsTrace.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,73 @@ | ||
package io.smallrye.reactive.messaging.jms.tracing; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.ArrayList; | ||
import java.util.Enumeration; | ||
import java.util.List; | ||
|
||
import jakarta.jms.JMSException; | ||
import jakarta.jms.Message; | ||
|
||
public class JmsTrace { | ||
private final String queue; | ||
private final Map<String, Object> messageProperties; | ||
private final Message jmsMessage; | ||
|
||
private JmsTrace(final String queue, Map<String, Object> messageProperties) { | ||
private JmsTrace(final String queue, Message jmsMessage) { | ||
this.queue = queue; | ||
this.messageProperties = messageProperties; | ||
this.jmsMessage = jmsMessage; | ||
} | ||
|
||
public String getQueue() { | ||
return queue; | ||
} | ||
|
||
public Map<String, Object> getMessageProperties() { | ||
return messageProperties; | ||
public Message getMessage() { | ||
return jmsMessage; | ||
} | ||
|
||
public List<String> getPropertyNames() { | ||
List<String> keys = new ArrayList<>(); | ||
Enumeration propertyNames = null; | ||
try { | ||
propertyNames = jmsMessage.getPropertyNames(); | ||
while (propertyNames.hasMoreElements()) { | ||
keys.add(propertyNames.nextElement().toString()); | ||
} | ||
} catch (JMSException ignored) { | ||
} | ||
return keys; | ||
} | ||
|
||
public String getProperty(final String key) { | ||
try { | ||
return jmsMessage.getStringProperty(key); | ||
} catch (JMSException ignored) { | ||
return null; | ||
} | ||
} | ||
|
||
public void setProperty(final String key, final String value) { | ||
try { | ||
jmsMessage.setStringProperty(key, value); | ||
} catch (JMSException ignored) { | ||
} | ||
} | ||
|
||
public static class Builder { | ||
private String queue; | ||
private Map<String, Object> properties; | ||
private Message jmsMessage; | ||
|
||
public Builder withQueue(final String queue) { | ||
this.queue = queue; | ||
return this; | ||
} | ||
|
||
public Builder withProperties(Map<String, Object> properties) { | ||
this.properties = new HashMap<>(properties); | ||
public Builder withMessage(final Message message) { | ||
this.jmsMessage = message; | ||
return this; | ||
} | ||
|
||
public JmsTrace build() { | ||
return new JmsTrace(queue, properties); | ||
return new JmsTrace(queue, jmsMessage); | ||
} | ||
} | ||
} |
16 changes: 2 additions & 14 deletions
16
...g-jms/src/main/java/io/smallrye/reactive/messaging/jms/tracing/JmsTraceTextMapGetter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 17 additions & 2 deletions
19
.../java/io/smallrye/reactive/messaging/jms/tracing/MessagePropertiesExtractAdapterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,37 @@ | ||
package io.smallrye.reactive.messaging.jms.tracing; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.doAnswer; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import jakarta.jms.JMSException; | ||
import jakarta.jms.Message; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class MessagePropertiesExtractAdapterTest { | ||
@Test | ||
public void verifyNullHeaderHandled() { | ||
public void verifyNullHeaderHandled() throws JMSException { | ||
Map<String, Object> messageProperties = new HashMap<>(); | ||
messageProperties.put("test_null_header", null); | ||
|
||
JmsTrace jmsTrace = new JmsTrace.Builder().withProperties(messageProperties).build(); | ||
// Create a mock JMS message | ||
Message msg = mock(Message.class); | ||
doAnswer(i -> messageProperties.get(i.getArgument(0, String.class))) | ||
.when(msg).getStringProperty(anyString()); | ||
doAnswer(i -> messageProperties.put(i.getArgument(0, String.class), i.getArgument(1, String.class))) | ||
.when(msg).setStringProperty(anyString(), anyString()); | ||
|
||
JmsTrace jmsTrace = new JmsTrace.Builder().withMessage(msg).build(); | ||
|
||
String headerValue = JmsTraceTextMapGetter.INSTANCE.get(jmsTrace, "test_null_header"); | ||
JmsTraceTextMapSetter.INSTANCE.set(jmsTrace, "test_other_header", "value"); | ||
|
||
assertThat(headerValue).isNull(); | ||
assertThat(messageProperties.get("test_other_header")).isEqualTo("value"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters