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-883pull/890/head
parent
401e2c8b6f
commit
e695e5d637
@ -0,0 +1,100 @@
|
||||
/*
|
||||
* 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 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;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
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.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@Configuration
|
||||
@AutoConfigureBefore(JmsTemplateAutoConfiguration.class)
|
||||
@ConditionalOnClass({ConnectionFactory.class, ActiveMQConnectionFactory.class})
|
||||
@ConditionalOnMissingBean(ConnectionFactory.class)
|
||||
public class ActiveMQAutoConfiguration {
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnClass(VMTransportFactory.class)
|
||||
@Conditional(EmbeddedBrokerCondition.class)
|
||||
@Import(ActiveMQConnectionFactoryConfiguration.class)
|
||||
protected static class EmbeddedBroker {
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Conditional(NonEmbeddedBrokerCondition.class)
|
||||
@Import(ActiveMQConnectionFactoryConfiguration.class)
|
||||
protected static class NetworkBroker {
|
||||
}
|
||||
|
||||
static abstract class BrokerTypeCondition extends SpringBootCondition {
|
||||
private final boolean embedded;
|
||||
|
||||
BrokerTypeCondition(boolean embedded) {
|
||||
this.embedded = embedded;
|
||||
}
|
||||
|
||||
@Override
|
||||
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 detected = outcome ? "detected" : "not detected";
|
||||
return brokerType + " ActiveMQ broker " + detected + " - brokerUrl '" + brokerUrl + "'";
|
||||
}
|
||||
}
|
||||
|
||||
static class EmbeddedBrokerCondition extends BrokerTypeCondition {
|
||||
|
||||
EmbeddedBrokerCondition() {
|
||||
super(true);
|
||||
}
|
||||
}
|
||||
|
||||
static class NonEmbeddedBrokerCondition extends BrokerTypeCondition {
|
||||
|
||||
NonEmbeddedBrokerCondition() {
|
||||
super(false);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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 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
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(ActiveMQProperties.class)
|
||||
class ActiveMQConnectionFactoryConfiguration {
|
||||
|
||||
@Autowired
|
||||
private ActiveMQProperties config;
|
||||
|
||||
@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());
|
||||
}
|
||||
}
|
@ -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());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue