Revisit JMS support

Since ActiveMQ 5.8.0, the modules structure has been revisited and
activemq-core no longer exists. The activemq-broker is required to
create an embedded broker. Since Boot creates such broker by default
if ConnectionFactory is present, a condition has been added to do so
only when the necessary classes are present in the classpath.

The default embedded broker is now configured to disable message
persistence altogether as this requires an extra jar since 5.8.0, i.e.
activemq-kahadb-store.

Split the ActiveMQ auto configuration from the JmsTemplate auto
configuration so these are totally independent.
ActiveMQAutoConfiguration has been created to detect and configure
the ActiveMQ broker if necessary.

The brokerUrl parameter was ignored as long as the inMemory parameter
was true. The actual brokerUrl to use is now determined by the user
defined values of those parameters: if the brokerUrl is set, it is always
used. If no brokerUrl is set, the value of inMemory determines if an
embedded broker should be used (true) or a tcp connection to an
existing local broker (false).

JmsTemplateAutoConfiguration now creates a JmsTemplate only if a
ConnectionFactory is available.

Fixes gh-872, gh-882, gh-883
This commit is contained in:
Stephane Nicoll
2014-05-15 16:18:16 +02:00
parent 401e2c8b6f
commit e695e5d637
10 changed files with 327 additions and 85 deletions

View File

@@ -0,0 +1,80 @@
/*
* Copyright 2012-2014 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
*
* http://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;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.core.env.StandardEnvironment;
/**
*
* @author Stephane Nicoll
*/
public class ActiveMQPropertiesTests {
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.getBrokerUrl());
}
@Test
public void getBrokerUrlUseExplicitBrokerUrl() {
this.properties.setBrokerUrl("vm://foo-bar");
assertEquals("vm://foo-bar", this.properties.getBrokerUrl());
}
@Test
public void getBrokerUrlWithInMemorySetToFalse() {
this.properties.setInMemory(false);
assertEquals(ActiveMQProperties.DEFAULT_NETWORK_BROKER_URL, this.properties.getBrokerUrl());
}
@Test
public void getExplicitBrokerUrlAlwaysWins() {
this.properties.setBrokerUrl("vm://foo-bar");
this.properties.setInMemory(false);
assertEquals("vm://foo-bar", this.properties.getBrokerUrl());
}
}

View File

@@ -45,9 +45,7 @@ public class JmsTemplateAutoConfigurationTests {
@Test
public void testDefaultJmsTemplate() {
this.context = new AnnotationConfigApplicationContext();
this.context
.register(TestConfiguration.class, JmsTemplateAutoConfiguration.class);
this.context = createContext(TestConfiguration.class);
this.context.refresh();
JmsTemplate jmsTemplate = this.context.getBean(JmsTemplate.class);
ActiveMQConnectionFactory connectionFactory = this.context
@@ -55,14 +53,13 @@ public class JmsTemplateAutoConfigurationTests {
assertNotNull(jmsTemplate);
assertNotNull(connectionFactory);
assertEquals(jmsTemplate.getConnectionFactory(), connectionFactory);
assertEquals("vm://localhost", ((ActiveMQConnectionFactory) jmsTemplate.getConnectionFactory()).getBrokerURL());
assertEquals(ActiveMQProperties.DEFAULT_EMBEDDED_BROKER_URL,
((ActiveMQConnectionFactory) jmsTemplate.getConnectionFactory()).getBrokerURL());
}
@Test
public void testConnectionFactoryBackoff() {
this.context = new AnnotationConfigApplicationContext();
this.context.register(TestConfiguration2.class,
JmsTemplateAutoConfiguration.class);
this.context = createContext(TestConfiguration2.class);
this.context.refresh();
assertEquals("foobar", this.context.getBean(ActiveMQConnectionFactory.class)
.getBrokerURL());
@@ -70,9 +67,7 @@ public class JmsTemplateAutoConfigurationTests {
@Test
public void testJmsTemplateBackoff() {
this.context = new AnnotationConfigApplicationContext();
this.context.register(TestConfiguration3.class,
JmsTemplateAutoConfiguration.class);
this.context = createContext(TestConfiguration3.class);
this.context.refresh();
JmsTemplate jmsTemplate = this.context.getBean(JmsTemplate.class);
assertEquals(999, jmsTemplate.getPriority());
@@ -80,9 +75,7 @@ public class JmsTemplateAutoConfigurationTests {
@Test
public void testJmsTemplateBackoffEverything() {
this.context = new AnnotationConfigApplicationContext();
this.context.register(TestConfiguration2.class, TestConfiguration3.class,
JmsTemplateAutoConfiguration.class);
this.context = createContext(TestConfiguration2.class, TestConfiguration3.class);
this.context.refresh();
JmsTemplate jmsTemplate = this.context.getBean(JmsTemplate.class);
assertEquals(999, jmsTemplate.getPriority());
@@ -92,9 +85,7 @@ public class JmsTemplateAutoConfigurationTests {
@Test
public void testPubSubDisabledByDefault() {
this.context = new AnnotationConfigApplicationContext();
this.context
.register(TestConfiguration.class, JmsTemplateAutoConfiguration.class);
this.context = createContext(TestConfiguration.class);
this.context.refresh();
JmsTemplate jmsTemplate = this.context.getBean(JmsTemplate.class);
assertFalse(jmsTemplate.isPubSubDomain());
@@ -102,9 +93,7 @@ public class JmsTemplateAutoConfigurationTests {
@Test
public void testJmsTemplatePostProcessedSoThatPubSubIsTrue() {
this.context = new AnnotationConfigApplicationContext();
this.context.register(TestConfiguration4.class,
JmsTemplateAutoConfiguration.class);
this.context = createContext(TestConfiguration4.class);
this.context.refresh();
JmsTemplate jmsTemplate = this.context.getBean(JmsTemplate.class);
assertTrue(jmsTemplate.isPubSubDomain());
@@ -112,9 +101,7 @@ public class JmsTemplateAutoConfigurationTests {
@Test
public void testJmsTemplateOverridden() {
this.context = new AnnotationConfigApplicationContext();
this.context
.register(TestConfiguration.class, JmsTemplateAutoConfiguration.class);
this.context = createContext(TestConfiguration.class);
EnvironmentTestUtils
.addEnvironment(this.context, "spring.jms.pubSubDomain:false");
this.context.refresh();
@@ -129,9 +116,7 @@ public class JmsTemplateAutoConfigurationTests {
@Test
public void testActiveMQOverriddenStandalone() {
this.context = new AnnotationConfigApplicationContext();
this.context
.register(TestConfiguration.class, JmsTemplateAutoConfiguration.class);
this.context = createContext(TestConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context,
"spring.activemq.inMemory:false");
this.context.refresh();
@@ -141,17 +126,14 @@ public class JmsTemplateAutoConfigurationTests {
assertNotNull(jmsTemplate);
assertNotNull(connectionFactory);
assertEquals(jmsTemplate.getConnectionFactory(), connectionFactory);
assertEquals("tcp://localhost:61616",
assertEquals(ActiveMQProperties.DEFAULT_NETWORK_BROKER_URL,
((ActiveMQConnectionFactory) jmsTemplate.getConnectionFactory()).getBrokerURL());
}
@Test
public void testActiveMQOverriddenRemoteHost() {
this.context = new AnnotationConfigApplicationContext();
this.context
.register(TestConfiguration.class, JmsTemplateAutoConfiguration.class);
this.context = createContext(TestConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context,
"spring.activemq.inMemory:false",
"spring.activemq.brokerUrl:tcp://remote-host:10000");
this.context.refresh();
JmsTemplate jmsTemplate = this.context.getBean(JmsTemplate.class);
@@ -166,9 +148,7 @@ public class JmsTemplateAutoConfigurationTests {
@Test
public void testActiveMQOverriddenPool() {
this.context = new AnnotationConfigApplicationContext();
this.context
.register(TestConfiguration.class, JmsTemplateAutoConfiguration.class);
this.context = createContext(TestConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "spring.activemq.pooled:true");
this.context.refresh();
JmsTemplate jmsTemplate = this.context.getBean(JmsTemplate.class);
@@ -179,14 +159,12 @@ public class JmsTemplateAutoConfigurationTests {
assertEquals(jmsTemplate.getConnectionFactory(), pool);
ActiveMQConnectionFactory factory = (ActiveMQConnectionFactory) pool
.getConnectionFactory();
assertEquals("vm://localhost", factory.getBrokerURL());
assertEquals(ActiveMQProperties.DEFAULT_EMBEDDED_BROKER_URL, factory.getBrokerURL());
}
@Test
public void testActiveMQOverriddenPoolAndStandalone() {
this.context = new AnnotationConfigApplicationContext();
this.context
.register(TestConfiguration.class, JmsTemplateAutoConfiguration.class);
this.context = createContext(TestConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "spring.activemq.pooled:true",
"spring.activemq.inMemory:false");
this.context.refresh();
@@ -198,16 +176,13 @@ public class JmsTemplateAutoConfigurationTests {
assertEquals(jmsTemplate.getConnectionFactory(), pool);
ActiveMQConnectionFactory factory = (ActiveMQConnectionFactory) pool
.getConnectionFactory();
assertEquals("tcp://localhost:61616", factory.getBrokerURL());
assertEquals(ActiveMQProperties.DEFAULT_NETWORK_BROKER_URL, factory.getBrokerURL());
}
@Test
public void testActiveMQOverriddenPoolAndRemoteServer() {
this.context = new AnnotationConfigApplicationContext();
this.context
.register(TestConfiguration.class, JmsTemplateAutoConfiguration.class);
this.context = createContext(TestConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "spring.activemq.pooled:true",
"spring.activemq.inMemory:false",
"spring.activemq.brokerUrl:tcp://remote-host:10000");
this.context.refresh();
JmsTemplate jmsTemplate = this.context.getBean(JmsTemplate.class);
@@ -221,6 +196,13 @@ public class JmsTemplateAutoConfigurationTests {
assertEquals("tcp://remote-host:10000", factory.getBrokerURL());
}
private AnnotationConfigApplicationContext createContext(Class<?>... additionalClasses) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(additionalClasses);
context.register(ActiveMQAutoConfiguration.class, JmsTemplateAutoConfiguration.class);
return context;
}
@Configuration
protected static class TestConfiguration {
}