Skip to content

Commit 77b52b9

Browse files
nosansnicoll
authored andcommitted
Configure ActiveMQConnectionFactory properly without spring-jms
See gh-17531
1 parent 260acd0 commit 77b52b9

File tree

2 files changed

+84
-35
lines changed

2 files changed

+84
-35
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQConnectionFactoryConfiguration.java

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -48,47 +48,45 @@
4848
@ConditionalOnMissingBean(ConnectionFactory.class)
4949
class ActiveMQConnectionFactoryConfiguration {
5050

51+
private static ActiveMQConnectionFactory createConnectionFactory(ActiveMQProperties properties,
52+
List<ActiveMQConnectionFactoryCustomizer> connectionFactoryCustomizers) {
53+
return new ActiveMQConnectionFactoryFactory(properties, connectionFactoryCustomizers)
54+
.createConnectionFactory(ActiveMQConnectionFactory.class);
55+
}
56+
5157
@Configuration
52-
@ConditionalOnClass(CachingConnectionFactory.class)
5358
@ConditionalOnProperty(prefix = "spring.activemq.pool", name = "enabled", havingValue = "false",
5459
matchIfMissing = true)
5560
static class SimpleConnectionFactoryConfiguration {
5661

57-
private final JmsProperties jmsProperties;
58-
59-
private final ActiveMQProperties properties;
60-
61-
private final List<ActiveMQConnectionFactoryCustomizer> connectionFactoryCustomizers;
62-
63-
SimpleConnectionFactoryConfiguration(JmsProperties jmsProperties, ActiveMQProperties properties,
62+
@Bean
63+
@ConditionalOnProperty(prefix = "spring.jms.cache", name = "enabled", havingValue = "false")
64+
public ActiveMQConnectionFactory jmsConnectionFactory(ActiveMQProperties properties,
6465
ObjectProvider<ActiveMQConnectionFactoryCustomizer> connectionFactoryCustomizers) {
65-
this.jmsProperties = jmsProperties;
66-
this.properties = properties;
67-
this.connectionFactoryCustomizers = connectionFactoryCustomizers.orderedStream()
68-
.collect(Collectors.toList());
66+
return createConnectionFactory(properties,
67+
connectionFactoryCustomizers.orderedStream().collect(Collectors.toList()));
6968
}
7069

71-
@Bean
70+
@ConditionalOnClass(CachingConnectionFactory.class)
7271
@ConditionalOnProperty(prefix = "spring.jms.cache", name = "enabled", havingValue = "true",
7372
matchIfMissing = true)
74-
public CachingConnectionFactory cachingJmsConnectionFactory() {
75-
JmsProperties.Cache cacheProperties = this.jmsProperties.getCache();
76-
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(createConnectionFactory());
77-
connectionFactory.setCacheConsumers(cacheProperties.isConsumers());
78-
connectionFactory.setCacheProducers(cacheProperties.isProducers());
79-
connectionFactory.setSessionCacheSize(cacheProperties.getSessionCacheSize());
80-
return connectionFactory;
81-
}
82-
83-
@Bean
84-
@ConditionalOnProperty(prefix = "spring.jms.cache", name = "enabled", havingValue = "false")
85-
public ActiveMQConnectionFactory jmsConnectionFactory() {
86-
return createConnectionFactory();
87-
}
73+
static class CachingConnectionFactoryConfiguration {
74+
75+
@Bean
76+
@ConditionalOnProperty(prefix = "spring.jms.cache", name = "enabled", havingValue = "true",
77+
matchIfMissing = true)
78+
public CachingConnectionFactory cachingJmsConnectionFactory(JmsProperties jmsProperties,
79+
ActiveMQProperties properties,
80+
ObjectProvider<ActiveMQConnectionFactoryCustomizer> connectionFactoryCustomizers) {
81+
JmsProperties.Cache cacheProperties = jmsProperties.getCache();
82+
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(createConnectionFactory(
83+
properties, connectionFactoryCustomizers.orderedStream().collect(Collectors.toList())));
84+
connectionFactory.setCacheConsumers(cacheProperties.isConsumers());
85+
connectionFactory.setCacheProducers(cacheProperties.isProducers());
86+
connectionFactory.setSessionCacheSize(cacheProperties.getSessionCacheSize());
87+
return connectionFactory;
88+
}
8889

89-
private ActiveMQConnectionFactory createConnectionFactory() {
90-
return new ActiveMQConnectionFactoryFactory(this.properties, this.connectionFactoryCustomizers)
91-
.createConnectionFactory(ActiveMQConnectionFactory.class);
9290
}
9391

9492
}
@@ -98,13 +96,11 @@ private ActiveMQConnectionFactory createConnectionFactory() {
9896
static class PooledConnectionFactoryConfiguration {
9997

10098
@Bean(destroyMethod = "stop")
101-
@ConditionalOnProperty(prefix = "spring.activemq.pool", name = "enabled", havingValue = "true",
102-
matchIfMissing = false)
99+
@ConditionalOnProperty(prefix = "spring.activemq.pool", name = "enabled", havingValue = "true")
103100
public JmsPoolConnectionFactory pooledJmsConnectionFactory(ActiveMQProperties properties,
104101
ObjectProvider<ActiveMQConnectionFactoryCustomizer> factoryCustomizers) {
105-
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactoryFactory(properties,
106-
factoryCustomizers.orderedStream().collect(Collectors.toList()))
107-
.createConnectionFactory(ActiveMQConnectionFactory.class);
102+
ActiveMQConnectionFactory connectionFactory = createConnectionFactory(properties,
103+
factoryCustomizers.orderedStream().collect(Collectors.toList()));
108104
return new JmsPoolConnectionFactoryFactory(properties.getPool())
109105
.createPooledConnectionFactory(connectionFactory);
110106
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.jms.activemq;
18+
19+
import org.apache.activemq.ActiveMQConnectionFactory;
20+
import org.junit.Test;
21+
22+
import org.springframework.boot.autoconfigure.AutoConfigurations;
23+
import org.springframework.boot.test.context.FilteredClassLoader;
24+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
25+
import org.springframework.jms.connection.CachingConnectionFactory;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
/**
30+
* Tests for {@link ActiveMQConnectionFactoryConfiguration} when
31+
* {@link CachingConnectionFactory} is not on the classpath.
32+
*
33+
* @author Dmytro Nosan
34+
*/
35+
public class ActiveMQAutoConfigurationTestsWithoutCachingConnectionFactoryTests {
36+
37+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
38+
.withConfiguration(AutoConfigurations.of(ActiveMQAutoConfiguration.class))
39+
.withClassLoader(new FilteredClassLoader(CachingConnectionFactory.class));
40+
41+
@Test
42+
public void cachingConnectionFactoryNotOnTheClasspathThenSimpleConnectionFactoryAutoConfigured() {
43+
this.contextRunner.withPropertyValues("spring.activemq.pool.enabled=false", "spring.jms.cache.enabled=false")
44+
.run((context) -> assertThat(context).hasSingleBean(ActiveMQConnectionFactory.class));
45+
}
46+
47+
@Test
48+
public void cachingConnectionFactoryNotOnTheClasspathAndCacheEnabledThenSimpleConnectionFactoryNotConfigured() {
49+
this.contextRunner.withPropertyValues("spring.activemq.pool.enabled=false", "spring.jms.cache.enabled=true")
50+
.run((context) -> assertThat(context).doesNotHaveBean(ActiveMQConnectionFactory.class));
51+
}
52+
53+
}

0 commit comments

Comments
 (0)