Skip to content

Commit 3d61d9e

Browse files
authored
Handle custom JMS acknowledgment modes as client acknowledge (#30619)
This commit updates JmsAccessor to handle custom JMS acknowledgment modes as client acknowledge, which is useful when working with JMS providers that provide non-standard variations of CLIENT_ACKNOWLEDGE, such as AWS SQS and its UNORDERED_ACKNOWLEDGE mode.
1 parent 1a26e17 commit 3d61d9e

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

spring-jms/src/main/java/org/springframework/jms/support/JmsAccessor.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -223,7 +223,10 @@ protected Session createSession(Connection con) throws JMSException {
223223
* @see jakarta.jms.Session#CLIENT_ACKNOWLEDGE
224224
*/
225225
protected boolean isClientAcknowledge(Session session) throws JMSException {
226-
return (session.getAcknowledgeMode() == Session.CLIENT_ACKNOWLEDGE);
226+
int mode = session.getAcknowledgeMode();
227+
return (mode != Session.SESSION_TRANSACTED &&
228+
mode != Session.AUTO_ACKNOWLEDGE &&
229+
mode != Session.DUPS_OK_ACKNOWLEDGE);
227230
}
228231

229232
}

spring-jms/src/test/java/org/springframework/jms/support/JmsAccessorTests.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,44 +21,54 @@
2121

2222
import static org.assertj.core.api.Assertions.assertThat;
2323
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
24+
import static org.mockito.BDDMockito.given;
25+
import static org.mockito.Mockito.mock;
2426

2527
/**
2628
* Unit tests for the {@link JmsAccessor} class.
2729
*
2830
* @author Rick Evans
2931
* @author Chris Beams
32+
* @author Vedran Pavic
3033
*/
31-
public class JmsAccessorTests {
34+
class JmsAccessorTests {
3235

3336
@Test
34-
public void testChokesIfConnectionFactoryIsNotSupplied() throws Exception {
37+
void testChokesIfConnectionFactoryIsNotSupplied() {
3538
JmsAccessor accessor = new StubJmsAccessor();
3639
assertThatIllegalArgumentException().isThrownBy(
3740
accessor::afterPropertiesSet);
3841
}
3942

4043
@Test
41-
public void testSessionTransactedModeReallyDoesDefaultToFalse() throws Exception {
44+
void testSessionTransactedModeReallyDoesDefaultToFalse() {
4245
JmsAccessor accessor = new StubJmsAccessor();
4346
assertThat(accessor.isSessionTransacted()).as("The [sessionTransacted] property of JmsAccessor must default to " +
4447
"false. Change this test (and the attendant Javadoc) if you have " +
4548
"changed the default.").isFalse();
4649
}
4750

4851
@Test
49-
public void testAcknowledgeModeReallyDoesDefaultToAutoAcknowledge() throws Exception {
52+
void testAcknowledgeModeReallyDoesDefaultToAutoAcknowledge() {
5053
JmsAccessor accessor = new StubJmsAccessor();
5154
assertThat(accessor.getSessionAcknowledgeMode()).as("The [sessionAcknowledgeMode] property of JmsAccessor must default to " +
5255
"[Session.AUTO_ACKNOWLEDGE]. Change this test (and the attendant " +
5356
"Javadoc) if you have changed the default.").isEqualTo(Session.AUTO_ACKNOWLEDGE);
5457
}
5558

5659
@Test
57-
public void testSetAcknowledgeModeNameChokesIfBadAckModeIsSupplied() throws Exception {
60+
void testSetAcknowledgeModeNameChokesIfBadAckModeIsSupplied() {
5861
assertThatIllegalArgumentException().isThrownBy(() ->
5962
new StubJmsAccessor().setSessionAcknowledgeModeName("Tally ho chaps!"));
6063
}
6164

65+
@Test
66+
void testCustomAcknowledgeModeIsConsideredClientAcknowledge() throws Exception {
67+
Session session = mock(Session.class);
68+
given(session.getAcknowledgeMode()).willReturn(100);
69+
JmsAccessor accessor = new StubJmsAccessor();
70+
assertThat(accessor.isClientAcknowledge(session)).isTrue();
71+
}
6272

6373
/**
6474
* Crummy, stub, do-nothing subclass of the JmsAccessor class for use in testing.

0 commit comments

Comments
 (0)