diff --git a/spring-jms/src/main/java/org/springframework/jms/support/JmsAccessor.java b/spring-jms/src/main/java/org/springframework/jms/support/JmsAccessor.java index e7a2ff4f8c..c196a84b56 100644 --- a/spring-jms/src/main/java/org/springframework/jms/support/JmsAccessor.java +++ b/spring-jms/src/main/java/org/springframework/jms/support/JmsAccessor.java @@ -144,9 +144,9 @@ public abstract class JmsAccessor implements InitializingBean { */ public void setSessionAcknowledgeModeName(String constantName) { Assert.hasText(constantName, "'constantName' must not be null or blank"); - Integer acknowledgeMode = sessionConstants.get(constantName); - Assert.notNull(acknowledgeMode, "Only acknowledge mode constants allowed"); - setSessionAcknowledgeMode(acknowledgeMode); + Integer sessionAcknowledgeMode = sessionConstants.get(constantName); + Assert.notNull(sessionAcknowledgeMode, "Only acknowledge mode constants allowed"); + this.sessionAcknowledgeMode = sessionAcknowledgeMode; } /** @@ -167,6 +167,8 @@ public abstract class JmsAccessor implements InitializingBean { * @see jakarta.jms.Connection#createSession(boolean, int) */ public void setSessionAcknowledgeMode(int sessionAcknowledgeMode) { + Assert.isTrue(sessionConstants.containsValue(sessionAcknowledgeMode), + "Only values of acknowledge mode constants allowed"); this.sessionAcknowledgeMode = sessionAcknowledgeMode; } diff --git a/spring-jms/src/test/java/org/springframework/jms/support/JmsAccessorTests.java b/spring-jms/src/test/java/org/springframework/jms/support/JmsAccessorTests.java index 8e914e9915..69f4f9225a 100644 --- a/spring-jms/src/test/java/org/springframework/jms/support/JmsAccessorTests.java +++ b/spring-jms/src/test/java/org/springframework/jms/support/JmsAccessorTests.java @@ -96,11 +96,21 @@ class JmsAccessorTests { assertThat(uniqueValues).hasSize(4); } + @Test + void setSessionAcknowledgeMode() { + assertThatIllegalArgumentException().isThrownBy(() -> accessor.setSessionAcknowledgeMode(999)); - private static Stream streamAcknowledgeModeConstants() { - return Arrays.stream(Session.class.getFields()) - .filter(ReflectionUtils::isPublicStaticFinal) - .map(Field::getName); + accessor.setSessionAcknowledgeMode(Session.AUTO_ACKNOWLEDGE); + assertThat(accessor.getSessionAcknowledgeMode()).isEqualTo(Session.AUTO_ACKNOWLEDGE); + + accessor.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE); + assertThat(accessor.getSessionAcknowledgeMode()).isEqualTo(Session.CLIENT_ACKNOWLEDGE); + + accessor.setSessionAcknowledgeMode(Session.DUPS_OK_ACKNOWLEDGE); + assertThat(accessor.getSessionAcknowledgeMode()).isEqualTo(Session.DUPS_OK_ACKNOWLEDGE); + + accessor.setSessionAcknowledgeMode(Session.SESSION_TRANSACTED); + assertThat(accessor.getSessionAcknowledgeMode()).isEqualTo(Session.SESSION_TRANSACTED); } @Test @@ -110,4 +120,11 @@ class JmsAccessorTests { assertThat(accessor.isClientAcknowledge(session)).isTrue(); } + + private static Stream streamAcknowledgeModeConstants() { + return Arrays.stream(Session.class.getFields()) + .filter(ReflectionUtils::isPublicStaticFinal) + .map(Field::getName); + } + }