-
Notifications
You must be signed in to change notification settings - Fork 1.5k
AMQ-8464: Implement receiveBody methods in the Consumer #1543
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
Draft
jbonofre
wants to merge
1
commit into
apache:main
Choose a base branch
from
jbonofre:AMQ-8464
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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
210 changes: 210 additions & 0 deletions
210
activemq-client/src/test/java/org/apache/activemq/ActiveMQConsumerTest.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,210 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF 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. | ||
| */ | ||
| package org.apache.activemq; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertNotNull; | ||
| import static org.junit.Assert.assertNull; | ||
| import static org.junit.Assert.assertTrue; | ||
| import static org.junit.Assert.fail; | ||
|
|
||
| import jakarta.jms.JMSContext; | ||
| import jakarta.jms.JMSRuntimeException; | ||
| import jakarta.jms.MessageFormatException; | ||
| import jakarta.jms.Queue; | ||
|
|
||
| import org.apache.activemq.ActiveMQConnectionFactory; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| public class ActiveMQConsumerTest { | ||
|
|
||
| private ActiveMQConnectionFactory connectionFactory; | ||
| private JMSContext jmsContext; | ||
| private Queue testQueue; | ||
|
|
||
| @Before | ||
| public void setUp() throws Exception { | ||
| connectionFactory = new ActiveMQConnectionFactory("vm://localhost?marshal=false&broker.persistent=false"); | ||
| jmsContext = connectionFactory.createContext(); | ||
| jmsContext.start(); | ||
| testQueue = jmsContext.createQueue("test.queue.receiveBody"); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| if (jmsContext != null) { | ||
| jmsContext.close(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testReceiveBody() { | ||
| // Send a message | ||
| String testMessage = "Test message body"; | ||
| jmsContext.createProducer().send(testQueue, testMessage); | ||
|
|
||
| // Receive body using receiveBody(Class) | ||
| try (jakarta.jms.JMSConsumer consumer = jmsContext.createConsumer(testQueue)) { | ||
| String body = consumer.receiveBody(String.class); | ||
| assertNotNull("Received body should not be null", body); | ||
| assertEquals("Received body should match sent message", testMessage, body); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testReceiveBodyWithTimeout() { | ||
| // Send a message | ||
| String testMessage = "Test message body with timeout"; | ||
| jmsContext.createProducer().send(testQueue, testMessage); | ||
|
|
||
| // Receive body using receiveBody(Class, long) | ||
| try (jakarta.jms.JMSConsumer consumer = jmsContext.createConsumer(testQueue)) { | ||
| String body = consumer.receiveBody(String.class, 5000L); | ||
| assertNotNull("Received body should not be null", body); | ||
| assertEquals("Received body should match sent message", testMessage, body); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testReceiveBodyNoWait() { | ||
| // Send a message | ||
| String testMessage = "Test message body no wait"; | ||
| jmsContext.createProducer().send(testQueue, testMessage); | ||
|
|
||
| // Receive body using receiveBodyNoWait(Class) | ||
| try (jakarta.jms.JMSConsumer consumer = jmsContext.createConsumer(testQueue)) { | ||
| String body = consumer.receiveBodyNoWait(String.class); | ||
| assertNotNull("Received body should not be null", body); | ||
| assertEquals("Received body should match sent message", testMessage, body); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testReceiveBodyReturnsNullWhenNoMessage() { | ||
| // Don't send any message | ||
| try (jakarta.jms.JMSConsumer consumer = jmsContext.createConsumer(testQueue)) { | ||
| // Use a short timeout to avoid blocking too long | ||
| String body = consumer.receiveBody(String.class, 100L); | ||
| assertNull("Received body should be null when no message available", body); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testReceiveBodyNoWaitReturnsNullWhenNoMessage() { | ||
| // Don't send any message | ||
| try (jakarta.jms.JMSConsumer consumer = jmsContext.createConsumer(testQueue)) { | ||
| String body = consumer.receiveBodyNoWait(String.class); | ||
| assertNull("Received body should be null when no message available", body); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testReceiveBodyWithWrongType() { | ||
| // Send a text message | ||
| String testMessage = "Test message"; | ||
| jmsContext.createProducer().send(testQueue, testMessage); | ||
|
|
||
| // Try to receive as wrong type | ||
| try (jakarta.jms.JMSConsumer consumer = jmsContext.createConsumer(testQueue)) { | ||
| try { | ||
| consumer.receiveBody(Integer.class); | ||
| fail("Should throw JMSRuntimeException for wrong type"); | ||
| } catch (JMSRuntimeException e) { | ||
| // Expected - MessageFormatException wrapped in JMSRuntimeException | ||
| assertNotNull("Exception should have a cause", e.getCause()); | ||
| assertTrue("Cause should be MessageFormatException", | ||
| e.getCause() instanceof MessageFormatException); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testReceiveBodyWithTimeoutWithWrongType() { | ||
| // Send a text message | ||
| String testMessage = "Test message"; | ||
| jmsContext.createProducer().send(testQueue, testMessage); | ||
|
|
||
| // Try to receive as wrong type | ||
| try (jakarta.jms.JMSConsumer consumer = jmsContext.createConsumer(testQueue)) { | ||
| try { | ||
| consumer.receiveBody(Integer.class, 5000L); | ||
| fail("Should throw JMSRuntimeException for wrong type"); | ||
| } catch (JMSRuntimeException e) { | ||
| // Expected - MessageFormatException wrapped in JMSRuntimeException | ||
| assertNotNull("Exception should have a cause", e.getCause()); | ||
| assertTrue("Cause should be MessageFormatException", | ||
| e.getCause() instanceof MessageFormatException); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testReceiveBodyNoWaitWithWrongType() { | ||
| // Send a text message | ||
| String testMessage = "Test message"; | ||
| jmsContext.createProducer().send(testQueue, testMessage); | ||
|
|
||
| // Try to receive as wrong type | ||
| try (jakarta.jms.JMSConsumer consumer = jmsContext.createConsumer(testQueue)) { | ||
| try { | ||
| consumer.receiveBodyNoWait(Integer.class); | ||
| fail("Should throw JMSRuntimeException for wrong type"); | ||
| } catch (JMSRuntimeException e) { | ||
| // Expected - MessageFormatException wrapped in JMSRuntimeException | ||
| assertNotNull("Exception should have a cause", e.getCause()); | ||
| assertTrue("Cause should be MessageFormatException", | ||
| e.getCause() instanceof MessageFormatException); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testReceiveBodyMultipleMessages() { | ||
| // Send multiple messages | ||
| String[] messages = {"Message 1", "Message 2", "Message 3"}; | ||
| for (String msg : messages) { | ||
| jmsContext.createProducer().send(testQueue, msg); | ||
| } | ||
|
|
||
| // Receive all messages using receiveBody | ||
| try (jakarta.jms.JMSConsumer consumer = jmsContext.createConsumer(testQueue)) { | ||
| for (String expected : messages) { | ||
| String body = consumer.receiveBody(String.class); | ||
| assertNotNull("Received body should not be null", body); | ||
| assertEquals("Received body should match sent message", expected, body); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testReceiveBodyWithTimeoutExpires() { | ||
| // Don't send any message | ||
| try (jakarta.jms.JMSConsumer consumer = jmsContext.createConsumer(testQueue)) { | ||
| // Use a short timeout | ||
| long startTime = System.currentTimeMillis(); | ||
| String body = consumer.receiveBody(String.class, 200L); | ||
| long elapsed = System.currentTimeMillis() - startTime; | ||
|
|
||
| assertNull("Received body should be null when timeout expires", body); | ||
| // Verify it actually waited (at least 100ms, but not too long) | ||
| assertTrue("Should have waited at least some time", elapsed >= 100); | ||
| } | ||
| } | ||
| } | ||
|
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would violate the JMS specification in that it does not return the message to the head of the prefetch queue to be read again on the next receive call for the AUTO and DUPS_OK ack modes. Same for the other methods below.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tabish121 yes, for sure. It's a first draft. Gonna work on this.