Update JMS auto-configuration to support XA

Update JMS auto-configuration for ActiveMQ and HornetQ to support XA
transactions.

See gh-947
This commit is contained in:
Phillip Webb
2014-08-21 23:35:35 -07:00
parent 8219f2be4c
commit da88bb4791
13 changed files with 558 additions and 350 deletions

View File

@@ -24,7 +24,6 @@ import org.junit.Test;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration;
import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQProperties;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
@@ -49,6 +48,10 @@ import static org.junit.Assert.assertTrue;
*/
public class JmsAutoConfigurationTests {
private static final String ACTIVEMQ_EMBEDDED_URL = "vm://localhost?broker.persistent=false";
private static final String ACTIVEMQ_NETWORK_URL = "tcp://localhost:61616";
private AnnotationConfigApplicationContext context;
@Test
@@ -61,7 +64,7 @@ public class JmsAutoConfigurationTests {
.getBean(JmsMessagingTemplate.class);
assertEquals(jmsTemplate.getConnectionFactory(), connectionFactory);
assertEquals(jmsTemplate, messagingTemplate.getJmsTemplate());
assertEquals(ActiveMQProperties.DEFAULT_EMBEDDED_BROKER_URL,
assertEquals(ACTIVEMQ_EMBEDDED_URL,
((ActiveMQConnectionFactory) jmsTemplate.getConnectionFactory())
.getBrokerURL());
assertTrue("listener container factory should be created by default",
@@ -158,7 +161,7 @@ public class JmsAutoConfigurationTests {
assertNotNull(jmsTemplate);
assertNotNull(connectionFactory);
assertEquals(jmsTemplate.getConnectionFactory(), connectionFactory);
assertEquals(ActiveMQProperties.DEFAULT_NETWORK_BROKER_URL,
assertEquals(ACTIVEMQ_NETWORK_URL,
((ActiveMQConnectionFactory) jmsTemplate.getConnectionFactory())
.getBrokerURL());
}
@@ -188,8 +191,7 @@ public class JmsAutoConfigurationTests {
assertEquals(jmsTemplate.getConnectionFactory(), pool);
ActiveMQConnectionFactory factory = (ActiveMQConnectionFactory) pool
.getConnectionFactory();
assertEquals(ActiveMQProperties.DEFAULT_EMBEDDED_BROKER_URL,
factory.getBrokerURL());
assertEquals(ACTIVEMQ_EMBEDDED_URL, factory.getBrokerURL());
}
@Test
@@ -204,8 +206,7 @@ public class JmsAutoConfigurationTests {
assertEquals(jmsTemplate.getConnectionFactory(), pool);
ActiveMQConnectionFactory factory = (ActiveMQConnectionFactory) pool
.getConnectionFactory();
assertEquals(ActiveMQProperties.DEFAULT_NETWORK_BROKER_URL,
factory.getBrokerURL());
assertEquals(ACTIVEMQ_NETWORK_URL, factory.getBrokerURL());
}
@Test
@@ -257,6 +258,7 @@ public class JmsAutoConfigurationTests {
@Configuration
protected static class TestConfiguration2 {
@Bean
ConnectionFactory connectionFactory() {
return new ActiveMQConnectionFactory() {
@@ -265,10 +267,12 @@ public class JmsAutoConfigurationTests {
}
};
}
}
@Configuration
protected static class TestConfiguration3 {
@Bean
JmsTemplate jmsTemplate(ConnectionFactory connectionFactory) {
JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
@@ -280,6 +284,7 @@ public class JmsAutoConfigurationTests {
@Configuration
protected static class TestConfiguration4 implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
@@ -295,6 +300,7 @@ public class JmsAutoConfigurationTests {
throws BeansException {
return bean;
}
}
@Configuration

View File

@@ -17,67 +17,50 @@
package org.springframework.boot.autoconfigure.jms.activemq;
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}.
* Tests for {@link ActiveMQProperties} and ActiveMQConnectionFactoryFactory.
*
* @author Stephane Nicoll
*/
public class ActiveMQPropertiesTests {
private static final String DEFAULT_EMBEDDED_BROKER_URL = "vm://localhost?broker.persistent=false";
private static final String DEFAULT_NETWORK_BROKER_URL = "tcp://localhost:61616";
private final ActiveMQProperties properties = new ActiveMQProperties();
private final StandardEnvironment environment = new StandardEnvironment();
@Test
public void determineBrokerUrlDefault() {
assertEquals(ActiveMQProperties.DEFAULT_EMBEDDED_BROKER_URL,
ActiveMQProperties.determineBrokerUrl(this.environment));
}
@Test
public void determineBrokerUrlVmBrokerUrl() {
EnvironmentTestUtils.addEnvironment(this.environment,
"spring.activemq.brokerUrl:vm://localhost?persistent=true");
assertEquals("vm://localhost?persistent=true",
ActiveMQProperties.determineBrokerUrl(this.environment));
}
@Test
public void determineBrokerUrlInMemoryFlag() {
EnvironmentTestUtils.addEnvironment(this.environment,
"spring.activemq.inMemory:false");
assertEquals(ActiveMQProperties.DEFAULT_NETWORK_BROKER_URL,
ActiveMQProperties.determineBrokerUrl(this.environment));
}
@Test
public void getBrokerUrlIsInMemoryByDefault() {
assertEquals(ActiveMQProperties.DEFAULT_EMBEDDED_BROKER_URL,
this.properties.determineBrokerUrl());
assertEquals(DEFAULT_EMBEDDED_BROKER_URL, new ActiveMQConnectionFactoryFactory(
this.properties).determineBrokerUrl());
}
@Test
public void getBrokerUrlUseExplicitBrokerUrl() {
this.properties.setBrokerUrl("vm://foo-bar");
assertEquals("vm://foo-bar", this.properties.determineBrokerUrl());
assertEquals("vm://foo-bar",
new ActiveMQConnectionFactoryFactory(this.properties)
.determineBrokerUrl());
}
@Test
public void getBrokerUrlWithInMemorySetToFalse() {
this.properties.setInMemory(false);
assertEquals(ActiveMQProperties.DEFAULT_NETWORK_BROKER_URL,
this.properties.determineBrokerUrl());
assertEquals(DEFAULT_NETWORK_BROKER_URL, new ActiveMQConnectionFactoryFactory(
this.properties).determineBrokerUrl());
}
@Test
public void getExplicitBrokerUrlAlwaysWins() {
this.properties.setBrokerUrl("vm://foo-bar");
this.properties.setInMemory(false);
assertEquals("vm://foo-bar", this.properties.determineBrokerUrl());
assertEquals("vm://foo-bar",
new ActiveMQConnectionFactoryFactory(this.properties)
.determineBrokerUrl());
}
}

View File

@@ -43,11 +43,13 @@ import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.jms.core.SessionCallback;
@@ -183,10 +185,8 @@ public class HornetQAutoConfigurationTests {
@Test
public void embeddedServiceWithCustomJmsConfiguration() {
load(CustomJmsConfiguration.class, "spring.hornetq.embedded.queues=Queue1,Queue2"); // Ignored
// with
// custom
// config
// Ignored with custom config
load(CustomJmsConfiguration.class, "spring.hornetq.embedded.queues=Queue1,Queue2");
DestinationChecker checker = new DestinationChecker(this.context);
checker.checkQueue("custom", true); // See CustomJmsConfiguration
@@ -317,7 +317,7 @@ public class HornetQAutoConfigurationTests {
String... environment) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.register(config);
applicationContext.register(HornetQAutoConfiguration.class,
applicationContext.register(HornetQAutoConfigurationWithoutXA.class,
JmsAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(applicationContext, environment);
applicationContext.refresh();
@@ -417,4 +417,11 @@ public class HornetQAutoConfigurationTests {
}
}
@Configuration
@EnableConfigurationProperties(HornetQProperties.class)
@Import({ HornetQEmbeddedServerConfiguration.class,
HornetQConnectionFactoryConfiguration.class })
protected static class HornetQAutoConfigurationWithoutXA {
}
}