From bcfaad2cb8bef5066dabe2900201e3a5556bfd5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Sat, 5 Aug 2023 22:12:47 -0600 Subject: [PATCH 1/2] Configure ActiveMQConnectionFactory properly without spring-jms Previously, if spring-jms is not available and a non-pool connection factory is requested (the default), no `CachingConnectionFactory` is created. This commit makes sure to separate the two conditions, so that a connection factory can be obtained even if spring-jms is not on the classpath. See gh-36767 --- ...ArtemisConnectionFactoryConfiguration.java | 49 +++++++++---------- .../ArtemisAutoConfigurationTests.java | 15 ++++++ 2 files changed, 39 insertions(+), 25 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryConfiguration.java index ec58238b9a..94e2b5580d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryConfiguration.java @@ -44,41 +44,40 @@ import org.springframework.jms.connection.CachingConnectionFactory; class ArtemisConnectionFactoryConfiguration { @Configuration(proxyBeanMethods = false) - @ConditionalOnClass(CachingConnectionFactory.class) @ConditionalOnProperty(prefix = "spring.artemis.pool", name = "enabled", havingValue = "false", matchIfMissing = true) static class SimpleConnectionFactoryConfiguration { - private final ArtemisProperties properties; - - private final ListableBeanFactory beanFactory; + @Bean(name = "jmsConnectionFactory") + @ConditionalOnProperty(prefix = "spring.jms.cache", name = "enabled", havingValue = "false") + ActiveMQConnectionFactory jmsConnectionFactory(ArtemisProperties properties, ListableBeanFactory beanFactory) { + return createConnectionFactory(properties, beanFactory); + } - SimpleConnectionFactoryConfiguration(ArtemisProperties properties, ListableBeanFactory beanFactory) { - this.properties = properties; - this.beanFactory = beanFactory; + private static ActiveMQConnectionFactory createConnectionFactory(ArtemisProperties properties, + ListableBeanFactory beanFactory) { + return new ArtemisConnectionFactoryFactory(beanFactory, properties) + .createConnectionFactory(ActiveMQConnectionFactory.class); } - @Bean(name = "jmsConnectionFactory") + @Configuration(proxyBeanMethods = false) + @ConditionalOnClass(CachingConnectionFactory.class) @ConditionalOnProperty(prefix = "spring.jms.cache", name = "enabled", havingValue = "true", matchIfMissing = true) - CachingConnectionFactory cachingJmsConnectionFactory(JmsProperties jmsProperties) { - JmsProperties.Cache cacheProperties = jmsProperties.getCache(); - CachingConnectionFactory connectionFactory = new CachingConnectionFactory(createConnectionFactory()); - connectionFactory.setCacheConsumers(cacheProperties.isConsumers()); - connectionFactory.setCacheProducers(cacheProperties.isProducers()); - connectionFactory.setSessionCacheSize(cacheProperties.getSessionCacheSize()); - return connectionFactory; - } - - @Bean(name = "jmsConnectionFactory") - @ConditionalOnProperty(prefix = "spring.jms.cache", name = "enabled", havingValue = "false") - ActiveMQConnectionFactory jmsConnectionFactory() { - return createConnectionFactory(); - } + static class CachingConnectionFactoryConfiguration { + + @Bean(name = "jmsConnectionFactory") + CachingConnectionFactory cachingJmsConnectionFactory(JmsProperties jmsProperties, + ArtemisProperties properties, ListableBeanFactory beanFactory) { + JmsProperties.Cache cacheProperties = jmsProperties.getCache(); + CachingConnectionFactory connectionFactory = new CachingConnectionFactory( + createConnectionFactory(properties, beanFactory)); + connectionFactory.setCacheConsumers(cacheProperties.isConsumers()); + connectionFactory.setCacheProducers(cacheProperties.isProducers()); + connectionFactory.setSessionCacheSize(cacheProperties.getSessionCacheSize()); + return connectionFactory; + } - private ActiveMQConnectionFactory createConnectionFactory() { - return new ArtemisConnectionFactoryFactory(this.beanFactory, this.properties) - .createConnectionFactory(ActiveMQConnectionFactory.class); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisAutoConfigurationTests.java index 7179e0bf2c..de390d779a 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisAutoConfigurationTests.java @@ -47,6 +47,7 @@ import org.messaginghub.pooled.jms.JmsPoolConnectionFactory; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration; +import org.springframework.boot.test.context.FilteredClassLoader; import org.springframework.boot.test.context.assertj.AssertableApplicationContext; import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.ApplicationContext; @@ -357,6 +358,20 @@ class ArtemisAutoConfigurationTests { }); } + @Test + void cachingConnectionFactoryNotOnTheClasspathThenSimpleConnectionFactoryAutoConfigured() { + this.contextRunner.withClassLoader(new FilteredClassLoader(CachingConnectionFactory.class)) + .withPropertyValues("spring.artemis.pool.enabled=false", "spring.jms.cache.enabled=false") + .run((context) -> assertThat(context).hasSingleBean(ActiveMQConnectionFactory.class)); + } + + @Test + void cachingConnectionFactoryNotOnTheClasspathAndCacheEnabledThenSimpleConnectionFactoryNotConfigured() { + this.contextRunner.withClassLoader(new FilteredClassLoader(CachingConnectionFactory.class)) + .withPropertyValues("spring.artemis.pool.enabled=false", "spring.jms.cache.enabled=true") + .run((context) -> assertThat(context).doesNotHaveBean(ActiveMQConnectionFactory.class)); + } + private ConnectionFactory getConnectionFactory(AssertableApplicationContext context) { assertThat(context).hasSingleBean(ConnectionFactory.class).hasBean("jmsConnectionFactory"); ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class); From 5e11bac1da9be0422fca33654c899492a9dd69b7 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 7 Aug 2023 14:05:31 +0200 Subject: [PATCH 2/2] Polish "Configure ActiveMQConnectionFactory properly without spring-jms" See gh-36767 --- .../jms/artemis/ArtemisConnectionFactoryConfiguration.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryConfiguration.java index 94e2b5580d..9212e623ad 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryConfiguration.java @@ -51,10 +51,10 @@ class ArtemisConnectionFactoryConfiguration { @Bean(name = "jmsConnectionFactory") @ConditionalOnProperty(prefix = "spring.jms.cache", name = "enabled", havingValue = "false") ActiveMQConnectionFactory jmsConnectionFactory(ArtemisProperties properties, ListableBeanFactory beanFactory) { - return createConnectionFactory(properties, beanFactory); + return createJmsConnectionFactory(properties, beanFactory); } - private static ActiveMQConnectionFactory createConnectionFactory(ArtemisProperties properties, + private static ActiveMQConnectionFactory createJmsConnectionFactory(ArtemisProperties properties, ListableBeanFactory beanFactory) { return new ArtemisConnectionFactoryFactory(beanFactory, properties) .createConnectionFactory(ActiveMQConnectionFactory.class); @@ -71,7 +71,7 @@ class ArtemisConnectionFactoryConfiguration { ArtemisProperties properties, ListableBeanFactory beanFactory) { JmsProperties.Cache cacheProperties = jmsProperties.getCache(); CachingConnectionFactory connectionFactory = new CachingConnectionFactory( - createConnectionFactory(properties, beanFactory)); + createJmsConnectionFactory(properties, beanFactory)); connectionFactory.setCacheConsumers(cacheProperties.isConsumers()); connectionFactory.setCacheProducers(cacheProperties.isProducers()); connectionFactory.setSessionCacheSize(cacheProperties.getSessionCacheSize());