Skip to content

Commit

Permalink
Harmonize JMS connection factory bean names
Browse files Browse the repository at this point in the history
This commit restores consistent naming for the auto-configured
connection factory.

Closes spring-projectsgh-19565
  • Loading branch information
snicoll committed Feb 11, 2020
1 parent d1f4a18 commit 8f06d81
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
Expand Down Expand Up @@ -55,7 +55,7 @@ public class JndiConnectionFactoryAutoConfiguration {
private static final String[] JNDI_LOCATIONS = { "java:/JmsXA", "java:/XAConnectionFactory" };

@Bean
public ConnectionFactory connectionFactory(JmsProperties properties) throws NamingException {
public ConnectionFactory jmsConnectionFactory(JmsProperties properties) throws NamingException {
JndiLocatorDelegate jndiLocatorDelegate = JndiLocatorDelegate.createDefaultResourceRefLocator();
if (StringUtils.hasLength(properties.getJndiName())) {
return jndiLocatorDelegate.lookup(properties.getJndiName(), ConnectionFactory.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
Expand Down Expand Up @@ -73,8 +73,7 @@ private static ActiveMQConnectionFactory createJmsConnectionFactory(ActiveMQProp
static class CachingConnectionFactoryConfiguration {

@Bean
CachingConnectionFactory cachingJmsConnectionFactory(JmsProperties jmsProperties,
ActiveMQProperties properties,
CachingConnectionFactory jmsConnectionFactory(JmsProperties jmsProperties, ActiveMQProperties properties,
ObjectProvider<ActiveMQConnectionFactoryCustomizer> factoryCustomizers) {
JmsProperties.Cache cacheProperties = jmsProperties.getCache();
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(
Expand All @@ -95,7 +94,7 @@ static class PooledConnectionFactoryConfiguration {

@Bean(destroyMethod = "stop")
@ConditionalOnProperty(prefix = "spring.activemq.pool", name = "enabled", havingValue = "true")
JmsPoolConnectionFactory pooledJmsConnectionFactory(ActiveMQProperties properties,
JmsPoolConnectionFactory jmsConnectionFactory(ActiveMQProperties properties,
ObjectProvider<ActiveMQConnectionFactoryCustomizer> factoryCustomizers) {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactoryFactory(properties,
factoryCustomizers.orderedStream().collect(Collectors.toList()))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
Expand Down Expand Up @@ -58,7 +58,7 @@ static class SimpleConnectionFactoryConfiguration {
this.beanFactory = beanFactory;
}

@Bean
@Bean(name = "jmsConnectionFactory")
@ConditionalOnProperty(prefix = "spring.jms.cache", name = "enabled", havingValue = "true",
matchIfMissing = true)
CachingConnectionFactory cachingJmsConnectionFactory(JmsProperties jmsProperties) {
Expand All @@ -70,7 +70,7 @@ CachingConnectionFactory cachingJmsConnectionFactory(JmsProperties jmsProperties
return connectionFactory;
}

@Bean
@Bean(name = "jmsConnectionFactory")
@ConditionalOnProperty(prefix = "spring.jms.cache", name = "enabled", havingValue = "false")
ActiveMQConnectionFactory jmsConnectionFactory() {
return createConnectionFactory();
Expand All @@ -85,13 +85,11 @@ private ActiveMQConnectionFactory createConnectionFactory() {

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ JmsPoolConnectionFactory.class, PooledObject.class })
@ConditionalOnProperty(prefix = "spring.artemis.pool", name = "enabled", havingValue = "true")
static class PooledConnectionFactoryConfiguration {

@Bean(destroyMethod = "stop")
@ConditionalOnProperty(prefix = "spring.artemis.pool", name = "enabled", havingValue = "true",
matchIfMissing = false)
JmsPoolConnectionFactory pooledJmsConnectionFactory(ListableBeanFactory beanFactory,
ArtemisProperties properties) {
JmsPoolConnectionFactory jmsConnectionFactory(ListableBeanFactory beanFactory, ArtemisProperties properties) {
ActiveMQConnectionFactory connectionFactory = new ArtemisConnectionFactoryFactory(beanFactory, properties)
.createConnectionFactory(ActiveMQConnectionFactory.class);
return new JmsPoolConnectionFactoryFactory(properties.getPool())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
Expand Down Expand Up @@ -111,8 +111,9 @@ void jndiNamePropertySetWithWrongValue() {

private ContextConsumer<AssertableApplicationContext> assertConnectionFactory(ConnectionFactory connectionFactory) {
return (context) -> {
assertThat(context).hasSingleBean(ConnectionFactory.class);
assertThat(context.getBean(ConnectionFactory.class)).isSameAs(connectionFactory);
assertThat(context).hasSingleBean(ConnectionFactory.class).hasBean("jmsConnectionFactory");
assertThat(context.getBean(ConnectionFactory.class)).isSameAs(connectionFactory)
.isSameAs(context.getBean("jmsConnectionFactory"));
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
Expand Down Expand Up @@ -50,13 +50,12 @@ class ActiveMQAutoConfigurationTests {
@Test
void brokerIsEmbeddedByDefault() {
this.contextRunner.withUserConfiguration(EmptyConfiguration.class).run((context) -> {
assertThat(context).hasSingleBean(CachingConnectionFactory.class);
CachingConnectionFactory cachingConnectionFactory = context.getBean(CachingConnectionFactory.class);
assertThat(cachingConnectionFactory.getTargetConnectionFactory())
.isInstanceOf(ActiveMQConnectionFactory.class);
assertThat(
((ActiveMQConnectionFactory) cachingConnectionFactory.getTargetConnectionFactory()).getBrokerURL())
.isEqualTo("vm://localhost?broker.persistent=false");
assertThat(context).hasSingleBean(CachingConnectionFactory.class).hasBean("jmsConnectionFactory");
CachingConnectionFactory connectionFactory = context.getBean(CachingConnectionFactory.class);
assertThat(context.getBean("jmsConnectionFactory")).isSameAs(connectionFactory);
assertThat(connectionFactory.getTargetConnectionFactory()).isInstanceOf(ActiveMQConnectionFactory.class);
assertThat(((ActiveMQConnectionFactory) connectionFactory.getTargetConnectionFactory()).getBrokerURL())
.isEqualTo("vm://localhost?broker.persistent=false");
});
}

Expand All @@ -69,9 +68,10 @@ void configurationBacksOffWhenCustomConnectionFactoryExists() {
@Test
void connectionFactoryIsCachedByDefault() {
this.contextRunner.withUserConfiguration(EmptyConfiguration.class).run((context) -> {
assertThat(context).hasSingleBean(ConnectionFactory.class);
assertThat(context).hasSingleBean(CachingConnectionFactory.class);
assertThat(context).hasSingleBean(ConnectionFactory.class).hasSingleBean(CachingConnectionFactory.class)
.hasBean("jmsConnectionFactory");
CachingConnectionFactory connectionFactory = context.getBean(CachingConnectionFactory.class);
assertThat(context.getBean("jmsConnectionFactory")).isSameAs(connectionFactory);
assertThat(connectionFactory.getTargetConnectionFactory()).isInstanceOf(ActiveMQConnectionFactory.class);
assertThat(connectionFactory.isCacheConsumers()).isFalse();
assertThat(connectionFactory.isCacheProducers()).isTrue();
Expand All @@ -85,9 +85,10 @@ void connectionFactoryCachingCanBeCustomized() {
.withPropertyValues("spring.jms.cache.consumers=true", "spring.jms.cache.producers=false",
"spring.jms.cache.session-cache-size=10")
.run((context) -> {
assertThat(context).hasSingleBean(ConnectionFactory.class);
assertThat(context).hasSingleBean(CachingConnectionFactory.class);
assertThat(context).hasSingleBean(ConnectionFactory.class)
.hasSingleBean(CachingConnectionFactory.class).hasBean("jmsConnectionFactory");
CachingConnectionFactory connectionFactory = context.getBean(CachingConnectionFactory.class);
assertThat(context.getBean("jmsConnectionFactory")).isSameAs(connectionFactory);
assertThat(connectionFactory.isCacheConsumers()).isTrue();
assertThat(connectionFactory.isCacheProducers()).isFalse();
assertThat(connectionFactory.getSessionCacheSize()).isEqualTo(10);
Expand All @@ -98,8 +99,10 @@ void connectionFactoryCachingCanBeCustomized() {
void connectionFactoryCachingCanBeDisabled() {
this.contextRunner.withUserConfiguration(EmptyConfiguration.class)
.withPropertyValues("spring.jms.cache.enabled=false").run((context) -> {
assertThat(context.getBeansOfType(ActiveMQConnectionFactory.class)).hasSize(1);
assertThat(context).hasSingleBean(ConnectionFactory.class)
.hasSingleBean(ActiveMQConnectionFactory.class).hasBean("jmsConnectionFactory");
ActiveMQConnectionFactory connectionFactory = context.getBean(ActiveMQConnectionFactory.class);
assertThat(context.getBean("jmsConnectionFactory")).isSameAs(connectionFactory);
ActiveMQConnectionFactory defaultFactory = new ActiveMQConnectionFactory(
"vm://localhost?broker.persistent=false");
assertThat(connectionFactory.getUserName()).isEqualTo(defaultFactory.getUserName());
Expand All @@ -123,8 +126,10 @@ void customConnectionFactoryIsApplied() {
"spring.activemq.nonBlockingRedelivery=true", "spring.activemq.sendTimeout=1000",
"spring.activemq.packages.trust-all=false", "spring.activemq.packages.trusted=com.example.acme")
.run((context) -> {
assertThat(context.getBeansOfType(ActiveMQConnectionFactory.class)).hasSize(1);
assertThat(context).hasSingleBean(ConnectionFactory.class)
.hasSingleBean(ActiveMQConnectionFactory.class).hasBean("jmsConnectionFactory");
ActiveMQConnectionFactory connectionFactory = context.getBean(ActiveMQConnectionFactory.class);
assertThat(context.getBean("jmsConnectionFactory")).isSameAs(connectionFactory);
assertThat(connectionFactory.getUserName()).isEqualTo("foo");
assertThat(connectionFactory.getPassword()).isEqualTo("bar");
assertThat(connectionFactory.getCloseTimeout()).isEqualTo(500);
Expand All @@ -139,8 +144,10 @@ void customConnectionFactoryIsApplied() {
void defaultPoolConnectionFactoryIsApplied() {
this.contextRunner.withUserConfiguration(EmptyConfiguration.class)
.withPropertyValues("spring.activemq.pool.enabled=true").run((context) -> {
assertThat(context.getBeansOfType(JmsPoolConnectionFactory.class)).hasSize(1);
assertThat(context).hasSingleBean(ConnectionFactory.class)
.hasSingleBean(JmsPoolConnectionFactory.class).hasBean("jmsConnectionFactory");
JmsPoolConnectionFactory connectionFactory = context.getBean(JmsPoolConnectionFactory.class);
assertThat(context.getBean("jmsConnectionFactory")).isSameAs(connectionFactory);
JmsPoolConnectionFactory defaultFactory = new JmsPoolConnectionFactory();
assertThat(connectionFactory.isBlockIfSessionPoolIsFull())
.isEqualTo(defaultFactory.isBlockIfSessionPoolIsFull());
Expand All @@ -167,8 +174,10 @@ void customPoolConnectionFactoryIsApplied() {
"spring.activemq.pool.timeBetweenExpirationCheck=2048",
"spring.activemq.pool.useAnonymousProducers=false")
.run((context) -> {
assertThat(context.getBeansOfType(JmsPoolConnectionFactory.class)).hasSize(1);
assertThat(context).hasSingleBean(ConnectionFactory.class)
.hasSingleBean(JmsPoolConnectionFactory.class).hasBean("jmsConnectionFactory");
JmsPoolConnectionFactory connectionFactory = context.getBean(JmsPoolConnectionFactory.class);
assertThat(context.getBean("jmsConnectionFactory")).isSameAs(connectionFactory);
assertThat(connectionFactory.isBlockIfSessionPoolIsFull()).isFalse();
assertThat(connectionFactory.getBlockIfSessionPoolIsFullTimeout()).isEqualTo(64);
assertThat(connectionFactory.getConnectionIdleTimeout()).isEqualTo(512);
Expand All @@ -183,7 +192,10 @@ void customPoolConnectionFactoryIsApplied() {
void poolConnectionFactoryConfiguration() {
this.contextRunner.withUserConfiguration(EmptyConfiguration.class)
.withPropertyValues("spring.activemq.pool.enabled:true").run((context) -> {
assertThat(context).hasSingleBean(ConnectionFactory.class)
.hasSingleBean(JmsPoolConnectionFactory.class).hasBean("jmsConnectionFactory");
ConnectionFactory factory = context.getBean(ConnectionFactory.class);
assertThat(context.getBean("jmsConnectionFactory")).isSameAs(factory);
assertThat(factory).isInstanceOf(JmsPoolConnectionFactory.class);
context.getSourceApplicationContext().close();
assertThat(factory.createConnection()).isNull();
Expand All @@ -194,14 +206,20 @@ void poolConnectionFactoryConfiguration() {
void cachingConnectionFactoryNotOnTheClasspathThenSimpleConnectionFactoryAutoConfigured() {
this.contextRunner.withClassLoader(new FilteredClassLoader(CachingConnectionFactory.class))
.withPropertyValues("spring.activemq.pool.enabled=false", "spring.jms.cache.enabled=false")
.run((context) -> assertThat(context).hasSingleBean(ActiveMQConnectionFactory.class));
.run((context) -> {
assertThat(context).hasSingleBean(ConnectionFactory.class)
.hasSingleBean(ActiveMQConnectionFactory.class).hasBean("jmsConnectionFactory");
ActiveMQConnectionFactory connectionFactory = context.getBean(ActiveMQConnectionFactory.class);
assertThat(context.getBean("jmsConnectionFactory")).isSameAs(connectionFactory);
});
}

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

@Configuration(proxyBeanMethods = false)
Expand Down
Loading

0 comments on commit 8f06d81

Please sign in to comment.