|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2023 the original author or authors. |
| 2 | + * Copyright 2002-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
20 | 20 | import java.util.concurrent.TimeUnit;
|
21 | 21 |
|
22 | 22 | import io.micrometer.observation.tck.TestObservationRegistry;
|
| 23 | +import jakarta.jms.Destination; |
| 24 | +import jakarta.jms.JMSException; |
| 25 | +import jakarta.jms.Message; |
23 | 26 | import jakarta.jms.MessageConsumer;
|
| 27 | +import jakarta.jms.Session; |
24 | 28 | import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
|
25 | 29 | import org.apache.activemq.artemis.junit.EmbeddedActiveMQExtension;
|
| 30 | +import org.assertj.core.api.Assertions; |
26 | 31 | import org.junit.jupiter.api.AfterEach;
|
27 | 32 | import org.junit.jupiter.api.BeforeEach;
|
28 | 33 | import org.junit.jupiter.api.Test;
|
@@ -81,6 +86,54 @@ void shouldRecordJmsProcessObservations() {
|
81 | 86 | .hasHighCardinalityKeyValue("messaging.destination.name", "spring.test.observation");
|
82 | 87 | }
|
83 | 88 |
|
| 89 | + @Test |
| 90 | + void shouldRecordJmsPublishAndProcessObservations() throws Exception { |
| 91 | + JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory); |
| 92 | + jmsTemplate.setObservationRegistry(registry); |
| 93 | + |
| 94 | + new Thread(() -> { |
| 95 | + jmsTemplate.execute(session -> { |
| 96 | + try { |
| 97 | + CountDownLatch latch = new CountDownLatch(1); |
| 98 | + MessageConsumer mc = session.createConsumer(session.createQueue("spring.test.observation")); |
| 99 | + mc.setMessageListener(message -> { |
| 100 | + try { |
| 101 | + Destination jmsReplyTo = message.getJMSReplyTo(); |
| 102 | + jmsTemplate.send(jmsReplyTo, new MessageCreator() { |
| 103 | + @Override |
| 104 | + public Message createMessage(Session session) throws JMSException { |
| 105 | + latch.countDown(); |
| 106 | + return session.createTextMessage("response content"); |
| 107 | + } |
| 108 | + }); |
| 109 | + } |
| 110 | + catch (JMSException e) { |
| 111 | + throw new RuntimeException(e); |
| 112 | + } |
| 113 | + }); |
| 114 | + return latch.await(2, TimeUnit.SECONDS); |
| 115 | + } |
| 116 | + catch (InterruptedException e) { |
| 117 | + throw new RuntimeException(e); |
| 118 | + } |
| 119 | + }, true); |
| 120 | + |
| 121 | + }).start(); |
| 122 | + Message response = jmsTemplate.sendAndReceive("spring.test.observation", new MessageCreator() { |
| 123 | + @Override |
| 124 | + public Message createMessage(Session session) throws JMSException { |
| 125 | + return session.createTextMessage("request content"); |
| 126 | + } |
| 127 | + }); |
| 128 | + |
| 129 | + String responseBody = response.getBody(String.class); |
| 130 | + Assertions.assertThat(responseBody).isEqualTo("response content"); |
| 131 | + |
| 132 | + assertThat(registry).hasNumberOfObservationsWithNameEqualTo("jms.message.publish", 2); |
| 133 | + assertThat(registry).hasObservationWithNameEqualTo("jms.message.process").that() |
| 134 | + .hasHighCardinalityKeyValue("messaging.destination.name", "spring.test.observation"); |
| 135 | + } |
| 136 | + |
84 | 137 | @AfterEach
|
85 | 138 | void shutdownServer() {
|
86 | 139 | connectionFactory.close();
|
|
0 commit comments