Skip to content

Commit cf2fa9b

Browse files
committed
fix(client): add missing recover() restriction check in CompletionListener callback
1 parent 9e21d33 commit cf2fa9b

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;
@@ -319,6 +320,34 @@ public void testProducerSendDestinationMessageQosParamsCompletionListener() thro
319320
messageProducer.send(session.createQueue(methodNameDestinationName), null, 1, 4, 0l, null);
320321
}
321322

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

0 commit comments

Comments
 (0)