Refactor Rabbit and JMS to avoid bean name clash
This commit is contained in:
@@ -23,6 +23,7 @@ import org.springframework.amqp.rabbit.core.RabbitAdmin;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.amqp.RabbitTemplateAutoConfiguration.RabbitConnectionFactoryProperties;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
@@ -31,14 +32,16 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.rabbitmq.client.Channel;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for {@link RabbitTemplate}.
|
||||
*
|
||||
* @author Greg Turnquist
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass({ RabbitTemplate.class })
|
||||
@EnableConfigurationProperties
|
||||
@ConditionalOnClass({ RabbitTemplate.class, Channel.class })
|
||||
@EnableConfigurationProperties(RabbitConnectionFactoryProperties.class)
|
||||
public class RabbitTemplateAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@@ -48,38 +51,30 @@ public class RabbitTemplateAutoConfiguration {
|
||||
return new RabbitAdmin(connectionFactory);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Autowired
|
||||
private ConnectionFactory connectionFactory;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(RabbitTemplate.class)
|
||||
protected static class RabbitTemplateCreator {
|
||||
|
||||
@Autowired
|
||||
CachingConnectionFactory connectionFactory;
|
||||
|
||||
@Bean
|
||||
public RabbitTemplate rabbitTemplate() {
|
||||
return new RabbitTemplate(this.connectionFactory);
|
||||
}
|
||||
|
||||
public RabbitTemplate rabbitTemplate() {
|
||||
return new RabbitTemplate(this.connectionFactory);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnMissingBean(ConnectionFactory.class)
|
||||
@EnableConfigurationProperties(RabbitConnectionFactoryProperties.class)
|
||||
protected static class RabbitConnectionFactoryCreator {
|
||||
|
||||
@Autowired
|
||||
private RabbitConnectionFactoryProperties config;
|
||||
|
||||
@Bean
|
||||
public CachingConnectionFactory connectionFactory() {
|
||||
public ConnectionFactory rabbitConnectionFactory(
|
||||
RabbitConnectionFactoryProperties config) {
|
||||
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(
|
||||
this.config.getHost());
|
||||
connectionFactory.setPort(this.config.getPort());
|
||||
if (this.config.getUsername() != null) {
|
||||
connectionFactory.setUsername(this.config.getUsername());
|
||||
config.getHost());
|
||||
connectionFactory.setPort(config.getPort());
|
||||
if (config.getUsername() != null) {
|
||||
connectionFactory.setUsername(config.getUsername());
|
||||
}
|
||||
if (this.config.getPassword() != null) {
|
||||
connectionFactory.setPassword(this.config.getPassword());
|
||||
if (config.getPassword() != null) {
|
||||
connectionFactory.setPassword(config.getPassword());
|
||||
}
|
||||
return connectionFactory;
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.jms.JmsTemplateAutoConfiguration.JmsTemplateProperties;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -37,41 +38,36 @@ import org.springframework.jms.core.JmsTemplate;
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass({ JmsTemplate.class, ConnectionFactory.class })
|
||||
@EnableConfigurationProperties(JmsTemplateProperties.class)
|
||||
public class JmsTemplateAutoConfiguration {
|
||||
|
||||
@Configuration
|
||||
@Autowired
|
||||
private JmsTemplateProperties config;
|
||||
|
||||
@Autowired
|
||||
private ConnectionFactory connectionFactory;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(JmsTemplate.class)
|
||||
@EnableConfigurationProperties(JmsTemplateProperties.class)
|
||||
protected static class JmsTemplateCreator {
|
||||
|
||||
@Autowired
|
||||
private JmsTemplateProperties config;
|
||||
|
||||
@Autowired
|
||||
private ConnectionFactory connectionFactory;
|
||||
|
||||
@Bean
|
||||
public JmsTemplate jmsTemplate() {
|
||||
JmsTemplate jmsTemplate = new JmsTemplate(this.connectionFactory);
|
||||
jmsTemplate.setPubSubDomain(this.config.isPubSubDomain());
|
||||
return jmsTemplate;
|
||||
}
|
||||
|
||||
public JmsTemplate jmsTemplate() {
|
||||
JmsTemplate jmsTemplate = new JmsTemplate(this.connectionFactory);
|
||||
jmsTemplate.setPubSubDomain(this.config.isPubSubDomain());
|
||||
return jmsTemplate;
|
||||
}
|
||||
|
||||
|
||||
@ConfigurationProperties(name = "spring.jms")
|
||||
public static class JmsTemplateProperties {
|
||||
|
||||
|
||||
private boolean pubSubDomain = true;
|
||||
|
||||
public boolean isPubSubDomain() {
|
||||
return pubSubDomain;
|
||||
return this.pubSubDomain;
|
||||
}
|
||||
|
||||
public void setPubSubDomain(boolean pubSubDomain) {
|
||||
this.pubSubDomain = pubSubDomain;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@@ -79,37 +75,40 @@ public class JmsTemplateAutoConfiguration {
|
||||
@ConditionalOnMissingBean(ConnectionFactory.class)
|
||||
@EnableConfigurationProperties(ActiveMQConnectionFactoryProperties.class)
|
||||
protected static class ActiveMQConnectionFactoryCreator {
|
||||
|
||||
|
||||
@Autowired
|
||||
private ActiveMQConnectionFactoryProperties config;
|
||||
|
||||
|
||||
@Bean
|
||||
ConnectionFactory connectionFactory() {
|
||||
ConnectionFactory jmsConnectionFactory() {
|
||||
if (this.config.isPooled()) {
|
||||
PooledConnectionFactory pool = new PooledConnectionFactory();
|
||||
pool.setConnectionFactory(new ActiveMQConnectionFactory(this.config.getBrokerURL()));
|
||||
pool.setConnectionFactory(new ActiveMQConnectionFactory(this.config
|
||||
.getBrokerURL()));
|
||||
return pool;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
return new ActiveMQConnectionFactory(this.config.getBrokerURL());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ConfigurationProperties(name = "spring.activemq")
|
||||
public static class ActiveMQConnectionFactoryProperties {
|
||||
|
||||
|
||||
private String brokerURL = "tcp://localhost:61616";
|
||||
|
||||
|
||||
private boolean inMemory = true;
|
||||
|
||||
|
||||
private boolean pooled = false;
|
||||
|
||||
|
||||
// Will override brokerURL if inMemory is set to true
|
||||
public String getBrokerURL() {
|
||||
if (this.inMemory) {
|
||||
return "vm://localhost";
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
return this.brokerURL;
|
||||
}
|
||||
}
|
||||
@@ -119,7 +118,7 @@ public class JmsTemplateAutoConfiguration {
|
||||
}
|
||||
|
||||
public boolean isInMemory() {
|
||||
return inMemory;
|
||||
return this.inMemory;
|
||||
}
|
||||
|
||||
public void setInMemory(boolean inMemory) {
|
||||
@@ -127,13 +126,13 @@ public class JmsTemplateAutoConfiguration {
|
||||
}
|
||||
|
||||
public boolean isPooled() {
|
||||
return pooled;
|
||||
return this.pooled;
|
||||
}
|
||||
|
||||
public void setPooled(boolean pooled) {
|
||||
this.pooled = pooled;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user