diff --git a/spring-jms/src/main/java/org/springframework/jms/listener/DefaultMessageListenerContainer.java b/spring-jms/src/main/java/org/springframework/jms/listener/DefaultMessageListenerContainer.java index 7d2c7bce47d5..01d29c2449eb 100644 --- a/spring-jms/src/main/java/org/springframework/jms/listener/DefaultMessageListenerContainer.java +++ b/spring-jms/src/main/java/org/springframework/jms/listener/DefaultMessageListenerContainer.java @@ -280,7 +280,7 @@ public void setCacheLevelName(String constantName) throws IllegalArgumentExcepti Assert.hasText(constantName, "'constantName' must not be null or blank"); Integer cacheLevel = constants.get(constantName); Assert.notNull(cacheLevel, "Only cache constants allowed"); - setCacheLevel(cacheLevel); + this.cacheLevel = cacheLevel; } /** @@ -304,6 +304,7 @@ public void setCacheLevelName(String constantName) throws IllegalArgumentExcepti * @see #setTransactionManager */ public void setCacheLevel(int cacheLevel) { + Assert.isTrue(constants.containsValue(cacheLevel), "Only values of cache constants allowed"); this.cacheLevel = cacheLevel; } diff --git a/spring-jms/src/test/java/org/springframework/jms/listener/DefaultMessageListenerContainerTests.java b/spring-jms/src/test/java/org/springframework/jms/listener/DefaultMessageListenerContainerTests.java index 089a526b83f3..4157cc4f174e 100644 --- a/spring-jms/src/test/java/org/springframework/jms/listener/DefaultMessageListenerContainerTests.java +++ b/spring-jms/src/test/java/org/springframework/jms/listener/DefaultMessageListenerContainerTests.java @@ -172,6 +172,28 @@ void setCacheLevelNameToAllSupportedValues() { assertThat(uniqueValues).hasSize(5); } + @Test + void setCacheLevel() { + DefaultMessageListenerContainer container = new DefaultMessageListenerContainer(); + + assertThatIllegalArgumentException().isThrownBy(() -> container.setCacheLevel(999)); + + container.setCacheLevel(DefaultMessageListenerContainer.CACHE_NONE); + assertThat(container.getCacheLevel()).isEqualTo(DefaultMessageListenerContainer.CACHE_NONE); + + container.setCacheLevel(DefaultMessageListenerContainer.CACHE_CONNECTION); + assertThat(container.getCacheLevel()).isEqualTo(DefaultMessageListenerContainer.CACHE_CONNECTION); + + container.setCacheLevel(DefaultMessageListenerContainer.CACHE_SESSION); + assertThat(container.getCacheLevel()).isEqualTo(DefaultMessageListenerContainer.CACHE_SESSION); + + container.setCacheLevel(DefaultMessageListenerContainer.CACHE_CONSUMER); + assertThat(container.getCacheLevel()).isEqualTo(DefaultMessageListenerContainer.CACHE_CONSUMER); + + container.setCacheLevel(DefaultMessageListenerContainer.CACHE_AUTO); + assertThat(container.getCacheLevel()).isEqualTo(DefaultMessageListenerContainer.CACHE_AUTO); + } + private static Stream streamCacheConstants() { return Arrays.stream(DefaultMessageListenerContainer.class.getFields())