-
Notifications
You must be signed in to change notification settings - Fork 327
Supporting SpringMapMessageListener #714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fe1534c
Supporting SpringMapMessageListener
eyalkoren 007f541
Removing leftover
eyalkoren 671a502
Applying review comments
eyalkoren 87434f7
Adjusting pre type matchers based on suggestions
eyalkoren 00964bc
Adding to CHANGELOG
eyalkoren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
134 changes: 134 additions & 0 deletions
134
...t-plugins/apm-jms-plugin/src/test/java/co/elastic/apm/agent/jms/spring/SpringJmsTest.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| /*- | ||
| * #%L | ||
| * Elastic APM Java agent | ||
| * %% | ||
| * Copyright (C) 2018 - 2019 Elastic and contributors | ||
| * %% | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. 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. | ||
| * #L% | ||
| */ | ||
| package co.elastic.apm.agent.jms.spring; | ||
|
|
||
| import co.elastic.apm.agent.AbstractInstrumentationTest; | ||
| import co.elastic.apm.agent.impl.transaction.Id; | ||
| import co.elastic.apm.agent.impl.transaction.Span; | ||
| import co.elastic.apm.agent.impl.transaction.TraceContext; | ||
| import co.elastic.apm.agent.impl.transaction.Transaction; | ||
| import org.apache.activemq.ActiveMQConnectionFactory; | ||
| import org.apache.activemq.command.ActiveMQMapMessage; | ||
| import org.junit.AfterClass; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
| import org.springframework.context.support.ClassPathXmlApplicationContext; | ||
|
|
||
| import javax.jms.Connection; | ||
| import javax.jms.ConnectionFactory; | ||
| import javax.jms.JMSException; | ||
| import javax.jms.MapMessage; | ||
| import javax.jms.MessageProducer; | ||
| import javax.jms.Queue; | ||
| import javax.jms.Session; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.ArrayBlockingQueue; | ||
| import java.util.concurrent.BlockingQueue; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| public class SpringJmsTest extends AbstractInstrumentationTest { | ||
|
|
||
| private static final String SPRING_TEST_QUEUE = "Spring-Test-Queue"; | ||
| static final BlockingQueue<Map> resultQueue = new ArrayBlockingQueue<>(5); | ||
|
|
||
| private static Connection connection; | ||
| private static ClassPathXmlApplicationContext ctx; | ||
|
|
||
| @BeforeClass | ||
| public static void setup() throws JMSException { | ||
| ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false"); | ||
| connection = connectionFactory.createConnection(); | ||
| connection.start(); | ||
| ctx = new ClassPathXmlApplicationContext("app-context.xml"); | ||
| } | ||
|
|
||
| @AfterClass | ||
| public static void teardown() throws JMSException { | ||
| ctx.close(); | ||
| connection.stop(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSendListenSpringQueue() throws JMSException, InterruptedException { | ||
| reporter.reset(); | ||
| try (Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE)) { | ||
|
|
||
| Transaction transaction = tracer.startTransaction(TraceContext.asRoot(), null, null).activate(); | ||
| transaction.setName("JMS-Spring-Test Transaction"); | ||
| transaction.withType("request"); | ||
| transaction.withResult("success"); | ||
|
|
||
| final String key1 = "key1"; | ||
| final String key2 = "key2"; | ||
| final String value1 = UUID.randomUUID().toString(); | ||
| final String value2 = UUID.randomUUID().toString(); | ||
| MapMessage mapMessage = new ActiveMQMapMessage(); | ||
| mapMessage.setString(key1, value1); | ||
| mapMessage.setString(key2, value2); | ||
|
|
||
| Queue queue = session.createQueue(SPRING_TEST_QUEUE); | ||
| MessageProducer producer = session.createProducer(queue); | ||
| producer.send(mapMessage); | ||
|
|
||
| // Let the onMessage instrumentation end the transaction then end the base transaction | ||
| Thread.sleep(500); | ||
| transaction.deactivate().end(); | ||
|
|
||
| Map result = resultQueue.poll(1, TimeUnit.SECONDS); | ||
|
|
||
| assertThat(result).isNotNull(); | ||
| assertThat(result.size()).isEqualTo(2); | ||
| assertThat(result.get(key1).toString()).isEqualTo(value1); | ||
| assertThat(result.get(key2).toString()).isEqualTo(value2); | ||
|
|
||
| List<Transaction> transactions = reporter.getTransactions(); | ||
| // The way the Spring framework works is polling through a standard JMS receive API in order to get the | ||
| // message, with which it then invokes the SpringMapMessageListener.onMessage() implementation, so we expect | ||
| // two JMS receive transactions (one for the receive and one for the onMessage), both with same parent and traceId | ||
| assertThat(transactions).hasSize(3); | ||
| Transaction baseTransaction = transactions.get(2); | ||
| Id traceId = baseTransaction.getTraceContext().getTraceId(); | ||
|
|
||
| List<Span> spans = reporter.getSpans(); | ||
| assertThat(spans).hasSize(1); | ||
| Span sendSpan = spans.get(0); | ||
| assertThat(sendSpan.getName().toString()).isEqualTo("JMS SEND to queue " + SPRING_TEST_QUEUE); | ||
| assertThat(sendSpan.getTraceContext().getTraceId()).isEqualTo(traceId); | ||
|
|
||
| Transaction receiveTransaction = transactions.get(0); | ||
| verifyReceiveTransaction(traceId, sendSpan, receiveTransaction); | ||
| } | ||
| } | ||
|
|
||
| private void verifyReceiveTransaction(Id traceId, Span sendSpan, Transaction receiveTransaction) { | ||
| assertThat(receiveTransaction.getName().toString()).isEqualTo("JMS RECEIVE from queue " + SPRING_TEST_QUEUE); | ||
| assertThat(receiveTransaction.getTraceContext().getTraceId()).isEqualTo(traceId); | ||
| assertThat(receiveTransaction.getTraceContext().getParentId()).isEqualTo(sendSpan.getTraceContext().getId()); | ||
| } | ||
| } |
46 changes: 46 additions & 0 deletions
46
...pm-jms-plugin/src/test/java/co/elastic/apm/agent/jms/spring/SpringMapMessageListener.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /*- | ||
| * #%L | ||
| * Elastic APM Java agent | ||
| * %% | ||
| * Copyright (C) 2018 - 2019 Elastic and contributors | ||
| * %% | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. 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. | ||
| * #L% | ||
| */ | ||
| package co.elastic.apm.agent.jms.spring; | ||
|
|
||
| import org.apache.activemq.command.ActiveMQMapMessage; | ||
| import org.springframework.jms.listener.SessionAwareMessageListener; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import javax.jms.JMSException; | ||
| import javax.jms.MapMessage; | ||
| import javax.jms.Session; | ||
| import java.util.Map; | ||
|
|
||
| @Service | ||
| public class SpringMapMessageListener implements SessionAwareMessageListener<MapMessage> { | ||
|
|
||
| @Override | ||
| public void onMessage(MapMessage mapMessage, Session session) throws JMSException { | ||
| Map map = ((ActiveMQMapMessage) mapMessage).getContentMap(); | ||
| System.out.println("Received map message: "); | ||
| map.forEach((key, value) -> System.out.println(" " + key + " --> " + value)); | ||
| SpringJmsTest.resultQueue.offer(map); | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
apm-agent-plugins/apm-jms-plugin/src/test/resources/app-context.xml
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <beans xmlns="http://www.springframework.org/schema/beans" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xmlns:context="http://www.springframework.org/schema/context" | ||
| xsi:schemaLocation=" | ||
| http://www.springframework.org/schema/beans | ||
| http://www.springframework.org/schema/beans/spring-beans.xsd | ||
| http://www.springframework.org/schema/context | ||
| http://www.springframework.org/schema/context/spring-context.xsd"> | ||
|
|
||
|
|
||
| <context:component-scan base-package="co.elastic.apm.agent.jms"/> | ||
|
|
||
| <!-- =============================================== --> | ||
| <!-- JMS Common,Define JMS connection Factory --> | ||
| <!-- =============================================== --> | ||
| <!-- Activemq connection factory --> | ||
| <bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> | ||
| <!-- brokerURL --> | ||
| <constructor-arg index="0" value="vm://localhost?broker.persistent=false"/> | ||
| </bean> | ||
|
|
||
| <!-- Pooled Spring connection factory --> | ||
| <bean id="connectionFactory" | ||
| class="org.springframework.jms.connection.CachingConnectionFactory"> | ||
| <constructor-arg ref="amqConnectionFactory"/> | ||
| </bean> | ||
|
|
||
|
|
||
| <!-- ============================================================= --> | ||
| <!-- JMS Receive,Define MessageListenerContainer --> | ||
| <!-- ============================================================= --> | ||
| <bean id="messageListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> | ||
| <property name="connectionFactory" ref="connectionFactory"/> | ||
| <property name="destinationName" value="Spring-Test-Queue"/> | ||
| <property name="messageListener" ref="springMapMessageListener"/> | ||
| </bean> | ||
|
|
||
| </beans> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.