Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActiveMQConnectionFactory is not configured when CachingConnectionFactory is missing and enabled properties are false #17531

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,44 +48,45 @@
@ConditionalOnMissingBean(ConnectionFactory.class)
class ActiveMQConnectionFactoryConfiguration {

private static ActiveMQConnectionFactory createConnectionFactory(ActiveMQProperties properties,
List<ActiveMQConnectionFactoryCustomizer> connectionFactoryCustomizers) {
return new ActiveMQConnectionFactoryFactory(properties, connectionFactoryCustomizers)
.createConnectionFactory(ActiveMQConnectionFactory.class);
}

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(CachingConnectionFactory.class)
@ConditionalOnProperty(prefix = "spring.activemq.pool", name = "enabled", havingValue = "false",
matchIfMissing = true)
static class SimpleConnectionFactoryConfiguration {

private final ActiveMQProperties properties;

private final List<ActiveMQConnectionFactoryCustomizer> connectionFactoryCustomizers;

SimpleConnectionFactoryConfiguration(ActiveMQProperties properties,
@Bean
@ConditionalOnProperty(prefix = "spring.jms.cache", name = "enabled", havingValue = "false")
ActiveMQConnectionFactory jmsConnectionFactory(ActiveMQProperties properties,
ObjectProvider<ActiveMQConnectionFactoryCustomizer> connectionFactoryCustomizers) {
this.properties = properties;
this.connectionFactoryCustomizers = connectionFactoryCustomizers.orderedStream()
.collect(Collectors.toList());
return createConnectionFactory(properties,
connectionFactoryCustomizers.orderedStream().collect(Collectors.toList()));
}

@Bean
@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
@ConditionalOnProperty(prefix = "spring.jms.cache", name = "enabled", havingValue = "false")
ActiveMQConnectionFactory jmsConnectionFactory() {
return createConnectionFactory();
}
static class CachingConnectionFactoryConfiguration {

@Bean
@ConditionalOnProperty(prefix = "spring.jms.cache", name = "enabled", havingValue = "true",
matchIfMissing = true)
CachingConnectionFactory cachingJmsConnectionFactory(JmsProperties jmsProperties,
ActiveMQProperties properties,
ObjectProvider<ActiveMQConnectionFactoryCustomizer> connectionFactoryCustomizers) {
JmsProperties.Cache cacheProperties = jmsProperties.getCache();
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(createConnectionFactory(
properties, connectionFactoryCustomizers.orderedStream().collect(Collectors.toList())));
connectionFactory.setCacheConsumers(cacheProperties.isConsumers());
connectionFactory.setCacheProducers(cacheProperties.isProducers());
connectionFactory.setSessionCacheSize(cacheProperties.getSessionCacheSize());
return connectionFactory;
}

private ActiveMQConnectionFactory createConnectionFactory() {
return new ActiveMQConnectionFactoryFactory(this.properties, this.connectionFactoryCustomizers)
.createConnectionFactory(ActiveMQConnectionFactory.class);
}

}
Expand All @@ -95,13 +96,11 @@ private ActiveMQConnectionFactory createConnectionFactory() {
static class PooledConnectionFactoryConfiguration {

@Bean(destroyMethod = "stop")
@ConditionalOnProperty(prefix = "spring.activemq.pool", name = "enabled", havingValue = "true",
matchIfMissing = false)
@ConditionalOnProperty(prefix = "spring.activemq.pool", name = "enabled", havingValue = "true")
JmsPoolConnectionFactory pooledJmsConnectionFactory(ActiveMQProperties properties,
ObjectProvider<ActiveMQConnectionFactoryCustomizer> factoryCustomizers) {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactoryFactory(properties,
factoryCustomizers.orderedStream().collect(Collectors.toList()))
.createConnectionFactory(ActiveMQConnectionFactory.class);
ActiveMQConnectionFactory connectionFactory = createConnectionFactory(properties,
factoryCustomizers.orderedStream().collect(Collectors.toList()));
return new JmsPoolConnectionFactoryFactory(properties.getPool())
.createPooledConnectionFactory(connectionFactory);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.autoconfigure.jms.activemq;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.junit.jupiter.api.Test;

import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.jms.connection.CachingConnectionFactory;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link ActiveMQConnectionFactoryConfiguration} when
* {@link CachingConnectionFactory} is not on the classpath.
*
* @author Dmytro Nosan
*/
class ActiveMQAutoConfigurationTestsWithoutCachingConnectionFactoryTests {

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(ActiveMQAutoConfiguration.class))
.withClassLoader(new FilteredClassLoader(CachingConnectionFactory.class));

@Test
void cachingConnectionFactoryNotOnTheClasspathThenSimpleConnectionFactoryAutoConfigured() {
this.contextRunner.withPropertyValues("spring.activemq.pool.enabled=false", "spring.jms.cache.enabled=false")
.run((context) -> assertThat(context).hasSingleBean(ActiveMQConnectionFactory.class));
}

@Test
void cachingConnectionFactoryNotOnTheClasspathAndCacheEnabledThenSimpleConnectionFactoryNotConfigured() {
this.contextRunner.withPropertyValues("spring.activemq.pool.enabled=false", "spring.jms.cache.enabled=true")
.run((context) -> assertThat(context).doesNotHaveBean(ActiveMQConnectionFactory.class));
}

}