Polish revisted JMS support

- Add createConnectionFactory method on ActiveMQProperties
- Change getBrokerUrl to return the broker URL and add new deduce method
- Move static methods to end of class
- Apply source formatting
pull/890/head
Phillip Webb 11 years ago
parent 875e77d420
commit 0b4e2b3667

@ -20,7 +20,6 @@ import javax.jms.ConnectionFactory;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.transport.vm.VMTransportFactory;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
@ -34,17 +33,16 @@ import org.springframework.context.annotation.Import;
import org.springframework.core.type.AnnotatedTypeMetadata;
/**
* {@link EnableAutoConfiguration Auto-configuration} to integrate with
* an ActiveMQ broker.
*
* <p>Validates that the classpath contain the necessary classes before
* starting an embedded broker.
*
* {@link EnableAutoConfiguration Auto-configuration} to integrate with an ActiveMQ
* broker. Validates that the classpath contain the necessary classes before starting an
* embedded broker.
*
* @author Stephane Nicoll
* @since 1.1.0
*/
@Configuration
@AutoConfigureBefore(JmsTemplateAutoConfiguration.class)
@ConditionalOnClass({ConnectionFactory.class, ActiveMQConnectionFactory.class})
@ConditionalOnClass({ ConnectionFactory.class, ActiveMQConnectionFactory.class })
@ConditionalOnMissingBean(ConnectionFactory.class)
public class ActiveMQAutoConfiguration {
@ -69,18 +67,22 @@ public class ActiveMQAutoConfiguration {
}
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
String brokerUrl = ActiveMQProperties.determineBrokerUrl(context.getEnvironment());
public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) {
String brokerUrl = ActiveMQProperties.determineBrokerUrl(context
.getEnvironment());
boolean match = brokerUrl.contains("vm://");
boolean outcome = (match == this.embedded);
return new ConditionOutcome(outcome, buildMessage(brokerUrl, outcome));
}
protected String buildMessage(String brokerUrl, boolean outcome) {
String brokerType = embedded ? "Embedded" : "Network";
String brokerType = this.embedded ? "Embedded" : "Network";
String detected = outcome ? "detected" : "not detected";
return brokerType + " ActiveMQ broker " + detected + " - brokerUrl '" + brokerUrl + "'";
return brokerType + " ActiveMQ broker " + detected + " - brokerUrl '"
+ brokerUrl + "'";
}
}
static class EmbeddedBrokerCondition extends BrokerTypeCondition {
@ -88,6 +90,7 @@ public class ActiveMQAutoConfiguration {
EmbeddedBrokerCondition() {
super(true);
}
}
static class NonEmbeddedBrokerCondition extends BrokerTypeCondition {
@ -95,6 +98,7 @@ public class ActiveMQAutoConfiguration {
NonEmbeddedBrokerCondition() {
super(false);
}
}
}

@ -18,45 +18,27 @@ package org.springframework.boot.autoconfigure.jms;
import javax.jms.ConnectionFactory;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.pool.PooledConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
/**
* Creates a {@link ConnectionFactory} based on {@link ActiveMQProperties}.
*
*
* @author Greg Turnquist
* @author Stephane Nicoll
* @since 1.1.0
*/
@Configuration
@EnableConfigurationProperties(ActiveMQProperties.class)
class ActiveMQConnectionFactoryConfiguration {
@Autowired
private ActiveMQProperties config;
private ActiveMQProperties properties;
@Bean
public ConnectionFactory jmsConnectionFactory() {
ConnectionFactory connectionFactory = getActiveMQConnectionFactory();
if (this.config.isPooled()) {
PooledConnectionFactory pool = new PooledConnectionFactory();
pool.setConnectionFactory(connectionFactory);
return pool;
}
return connectionFactory;
}
private ConnectionFactory getActiveMQConnectionFactory() {
if (StringUtils.hasLength(this.config.getUser())
&& StringUtils.hasLength(this.config.getPassword())) {
return new ActiveMQConnectionFactory(this.config.getUser(),
this.config.getPassword(), this.config.getBrokerUrl());
}
return new ActiveMQConnectionFactory(this.config.getBrokerUrl());
return this.properties.createConnectionFactory();
}
}

@ -16,10 +16,15 @@
package org.springframework.boot.autoconfigure.jms;
import javax.jms.ConnectionFactory;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.pool.PooledConnectionFactory;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertyResolver;
import org.springframework.util.StringUtils;
/**
* Configuration properties for ActiveMQ
@ -34,35 +39,18 @@ public class ActiveMQProperties {
public static final String DEFAULT_NETWORK_BROKER_URL = "tcp://localhost:61616";
private String brokerUrl = null;
private String brokerUrl;
private boolean inMemory = true;
private boolean pooled = false;
private boolean pooled;
private String user;
private String password;
/**
* Determine the broker url to use for the specified {@link Environment}.
* <p>If no broker url is specified through configuration, a default
* broker is provided, that is {@value #DEFAULT_EMBEDDED_BROKER_URL} if
* the {@code inMemory} flag is {@code null} or {@code true},
* {@value #DEFAULT_NETWORK_BROKER_URL} otherwise.
*
* @param environment the environment to extract configuration from
* @return the broker url to use
*/
public static String determineBrokerUrl(Environment environment) {
PropertyResolver resolver = new RelaxedPropertyResolver(environment, "spring.activemq.");
String brokerUrl = resolver.getProperty("brokerUrl");
Boolean inMemory = resolver.getProperty("inMemory", Boolean.class);
return determineBrokerUrl(brokerUrl, inMemory);
}
public String getBrokerUrl() {
return determineBrokerUrl(this.brokerUrl, this.inMemory);
return this.brokerUrl;
}
public void setBrokerUrl(String brokerUrl) {
@ -70,8 +58,8 @@ public class ActiveMQProperties {
}
/**
* Specify if the default broker url should be in memory. Ignored
* if an explicit broker has been specified.
* Specify if the default broker url should be in memory. Ignored if an explicit
* broker has been specified.
*/
public boolean isInMemory() {
return this.inMemory;
@ -105,22 +93,52 @@ public class ActiveMQProperties {
this.password = password;
}
/**
* Return a new {@link ConnectionFactory} from these properties.
*/
public ConnectionFactory createConnectionFactory() {
ConnectionFactory connectionFactory = createActiveMQConnectionFactory();
if (isPooled()) {
PooledConnectionFactory pool = new PooledConnectionFactory();
pool.setConnectionFactory(connectionFactory);
return pool;
}
return connectionFactory;
}
private ConnectionFactory createActiveMQConnectionFactory() {
String brokerUrl = determineBrokerUrl();
if (StringUtils.hasLength(this.user) && StringUtils.hasLength(this.password)) {
return new ActiveMQConnectionFactory(this.user, this.password, brokerUrl);
}
return new ActiveMQConnectionFactory(brokerUrl);
}
String determineBrokerUrl() {
return determineBrokerUrl(this.brokerUrl, this.inMemory);
}
/**
* @see #determineBrokerUrl(Environment)
* Determine the broker url to use for the specified {@link Environment}. If no broker
* url is specified through configuration, a default broker is provided, that is
* {@value #DEFAULT_EMBEDDED_BROKER_URL} if the {@code inMemory} flag is {@code null}
* or {@code true}, {@value #DEFAULT_NETWORK_BROKER_URL} otherwise.
* @param environment the environment to extract configuration from
* @return the broker url to use
*/
private static String determineBrokerUrl(String brokerUrl, Boolean inMemory) {
boolean embedded = (inMemory != null) ? inMemory : true;
public static String determineBrokerUrl(Environment environment) {
PropertyResolver resolver = new RelaxedPropertyResolver(environment,
"spring.activemq.");
String brokerUrl = resolver.getProperty("brokerUrl");
Boolean inMemory = resolver.getProperty("inMemory", Boolean.class);
return determineBrokerUrl(brokerUrl, inMemory);
}
private static String determineBrokerUrl(String brokerUrl, Boolean inMemory) {
if (brokerUrl != null) {
return brokerUrl;
}
else if (embedded) {
return DEFAULT_EMBEDDED_BROKER_URL;
}
else {
return DEFAULT_NETWORK_BROKER_URL;
}
boolean embedded = inMemory == null || inMemory;
return (embedded ? DEFAULT_EMBEDDED_BROKER_URL : DEFAULT_NETWORK_BROKER_URL);
}
}

@ -30,7 +30,7 @@ import org.springframework.jms.core.JmsTemplate;
/**
* {@link EnableAutoConfiguration Auto-configuration} for {@link JmsTemplate}.
*
*
* @author Greg Turnquist
*/
@Configuration

@ -16,15 +16,15 @@
package org.springframework.boot.autoconfigure.jms;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.core.env.StandardEnvironment;
import static org.junit.Assert.assertEquals;
/**
*
* Tests for {@link ActiveMQProperties}.
*
* @author Stephane Nicoll
*/
public class ActiveMQPropertiesTests {
@ -34,7 +34,7 @@ public class ActiveMQPropertiesTests {
@Test
public void determineBrokerUrlDefault() {
assertEquals(ActiveMQProperties.DEFAULT_EMBEDDED_BROKER_URL,
assertEquals(ActiveMQProperties.DEFAULT_EMBEDDED_BROKER_URL,
ActiveMQProperties.determineBrokerUrl(this.environment));
}
@ -56,25 +56,27 @@ public class ActiveMQPropertiesTests {
@Test
public void getBrokerUrlIsInMemoryByDefault() {
assertEquals(ActiveMQProperties.DEFAULT_EMBEDDED_BROKER_URL, this.properties.getBrokerUrl());
assertEquals(ActiveMQProperties.DEFAULT_EMBEDDED_BROKER_URL,
this.properties.determineBrokerUrl());
}
@Test
public void getBrokerUrlUseExplicitBrokerUrl() {
this.properties.setBrokerUrl("vm://foo-bar");
assertEquals("vm://foo-bar", this.properties.getBrokerUrl());
assertEquals("vm://foo-bar", this.properties.determineBrokerUrl());
}
@Test
public void getBrokerUrlWithInMemorySetToFalse() {
this.properties.setInMemory(false);
assertEquals(ActiveMQProperties.DEFAULT_NETWORK_BROKER_URL, this.properties.getBrokerUrl());
assertEquals(ActiveMQProperties.DEFAULT_NETWORK_BROKER_URL,
this.properties.determineBrokerUrl());
}
@Test
public void getExplicitBrokerUrlAlwaysWins() {
this.properties.setBrokerUrl("vm://foo-bar");
this.properties.setInMemory(false);
assertEquals("vm://foo-bar", this.properties.getBrokerUrl());
assertEquals("vm://foo-bar", this.properties.determineBrokerUrl());
}
}

@ -36,7 +36,7 @@ import static org.junit.Assert.assertTrue;
/**
* Tests for {@link JmsTemplateAutoConfiguration}.
*
*
* @author Greg Turnquist
*/
public class JmsTemplateAutoConfigurationTests {
@ -54,7 +54,8 @@ public class JmsTemplateAutoConfigurationTests {
assertNotNull(connectionFactory);
assertEquals(jmsTemplate.getConnectionFactory(), connectionFactory);
assertEquals(ActiveMQProperties.DEFAULT_EMBEDDED_BROKER_URL,
((ActiveMQConnectionFactory) jmsTemplate.getConnectionFactory()).getBrokerURL());
((ActiveMQConnectionFactory) jmsTemplate.getConnectionFactory())
.getBrokerURL());
}
@Test
@ -127,7 +128,8 @@ public class JmsTemplateAutoConfigurationTests {
assertNotNull(connectionFactory);
assertEquals(jmsTemplate.getConnectionFactory(), connectionFactory);
assertEquals(ActiveMQProperties.DEFAULT_NETWORK_BROKER_URL,
((ActiveMQConnectionFactory) jmsTemplate.getConnectionFactory()).getBrokerURL());
((ActiveMQConnectionFactory) jmsTemplate.getConnectionFactory())
.getBrokerURL());
}
@Test
@ -143,7 +145,8 @@ public class JmsTemplateAutoConfigurationTests {
assertNotNull(connectionFactory);
assertEquals(jmsTemplate.getConnectionFactory(), connectionFactory);
assertEquals("tcp://remote-host:10000",
((ActiveMQConnectionFactory) jmsTemplate.getConnectionFactory()).getBrokerURL());
((ActiveMQConnectionFactory) jmsTemplate.getConnectionFactory())
.getBrokerURL());
}
@Test
@ -159,7 +162,8 @@ public class JmsTemplateAutoConfigurationTests {
assertEquals(jmsTemplate.getConnectionFactory(), pool);
ActiveMQConnectionFactory factory = (ActiveMQConnectionFactory) pool
.getConnectionFactory();
assertEquals(ActiveMQProperties.DEFAULT_EMBEDDED_BROKER_URL, factory.getBrokerURL());
assertEquals(ActiveMQProperties.DEFAULT_EMBEDDED_BROKER_URL,
factory.getBrokerURL());
}
@Test
@ -176,7 +180,8 @@ public class JmsTemplateAutoConfigurationTests {
assertEquals(jmsTemplate.getConnectionFactory(), pool);
ActiveMQConnectionFactory factory = (ActiveMQConnectionFactory) pool
.getConnectionFactory();
assertEquals(ActiveMQProperties.DEFAULT_NETWORK_BROKER_URL, factory.getBrokerURL());
assertEquals(ActiveMQProperties.DEFAULT_NETWORK_BROKER_URL,
factory.getBrokerURL());
}
@Test
@ -196,10 +201,12 @@ public class JmsTemplateAutoConfigurationTests {
assertEquals("tcp://remote-host:10000", factory.getBrokerURL());
}
private AnnotationConfigApplicationContext createContext(Class<?>... additionalClasses) {
private AnnotationConfigApplicationContext createContext(
Class<?>... additionalClasses) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(additionalClasses);
context.register(ActiveMQAutoConfiguration.class, JmsTemplateAutoConfiguration.class);
context.register(ActiveMQAutoConfiguration.class,
JmsTemplateAutoConfiguration.class);
return context;
}

Loading…
Cancel
Save