Skip to content

Commit 8052cfc

Browse files
committed
fix(client): add missing recover() restriction check in CompletionListener callback
1 parent 7a18729 commit 8052cfc

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

activemq-client/src/main/java/org/apache/activemq/ActiveMQSession.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,7 @@ public boolean isClosed() {
845845
public void recover() throws JMSException {
846846

847847
checkClosed();
848+
checkNotInCompletionListenerCallback("recover");
848849
if (getTransacted()) {
849850
throw new IllegalStateException("This session is transacted");
850851
}

activemq-unit-tests/src/test/java/org/apache/activemq/jms2/ActiveMQJMS2ContextTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import static org.junit.Assert.fail;
2424

2525
import java.util.Enumeration;
26+
import java.util.concurrent.atomic.AtomicReference;
2627

2728
import jakarta.jms.CompletionListener;
2829
import jakarta.jms.Destination;
@@ -316,6 +317,34 @@ public void testProducerSendDestinationMessageQosParamsCompletionListener() thro
316317
messageProducer.send(session.createQueue(methodNameDestinationName), null, 1, 4, 0l, null);
317318
}
318319

320+
/**
321+
* Jakarta Messaging 3.1 spec section 7.3.8: calling recover() from within a CompletionListener
322+
* callback must throw IllegalStateException.
323+
*/
324+
@Test
325+
public void testRecoverThrowsIllegalStateFromCompletionListenerCallback() throws JMSException {
326+
final AtomicReference<Exception> callbackException = new AtomicReference<>();
327+
328+
messageProducer.send(session.createTextMessage("test"), new CompletionListener() {
329+
@Override
330+
public void onCompletion(final Message message) {
331+
try {
332+
session.recover();
333+
} catch (final Exception e) {
334+
callbackException.set(e);
335+
}
336+
}
337+
338+
@Override
339+
public void onException(final Message message, final Exception exception) {
340+
}
341+
});
342+
343+
assertNotNull("recover() must throw from within CompletionListener callback", callbackException.get());
344+
assertTrue("recover() must throw IllegalStateException from within CompletionListener callback",
345+
callbackException.get() instanceof jakarta.jms.IllegalStateException);
346+
}
347+
319348
protected static void sendMessage(JMSContext jmsContext, Destination testDestination, String textBody) {
320349
assertNotNull(jmsContext);
321350
JMSProducer jmsProducer = jmsContext.createProducer();

0 commit comments

Comments
 (0)