[BS-48] Add autoconfigured JMS support
* Add ability to detect spring-jms on the path and create a JmsTemplate with ActiveMQConnectionFactory * Create tests showing autoconfigured JmsTemplate with ActiveMQ, but prove it backs off if a separate ConnectionFactory exists. * Add support to spring-boot-cli to that it detects JmsTemplate, DefaultMessageListenerContainer, or SimpleMessageListenerContainer, and turns on autoconfiguration as well as add proper @Grab's and import statements. * Write a jms.groovy test showing proper CLI support Simplify ActiveMQ configuration Update ActiveMQ to 5.7.0pull/49/merge
parent
ecc4676fb3
commit
5801e422cf
@ -0,0 +1,65 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2013 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.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.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.jms.core.JmsTemplate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link EnableAutoConfiguration Auto-configuration} for {@link JmsTemplate}.
|
||||||
|
*
|
||||||
|
* @author Greg Turnquist
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@ConditionalOnClass(JmsTemplate.class)
|
||||||
|
public class JmsTemplateAutoConfiguration {
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ConditionalOnMissingBean(JmsTemplate.class)
|
||||||
|
protected static class JmsTemplateCreator {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
ConnectionFactory connectionFactory;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public JmsTemplate jmsTemplate() {
|
||||||
|
JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
|
||||||
|
jmsTemplate.setPubSubDomain(true);
|
||||||
|
return jmsTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ConditionalOnClass(ActiveMQConnectionFactory.class)
|
||||||
|
@ConditionalOnMissingBean(ConnectionFactory.class)
|
||||||
|
protected static class ActiveMQConnectionFactoryCreator {
|
||||||
|
@Bean
|
||||||
|
ConnectionFactory connectionFactory() {
|
||||||
|
return new ActiveMQConnectionFactory("vm://localhost");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,156 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2013 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.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import javax.jms.ConnectionFactory;
|
||||||
|
|
||||||
|
import org.apache.activemq.ActiveMQConnectionFactory;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.beans.BeansException;
|
||||||
|
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.jms.core.JmsTemplate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link JmsTemplateAutoConfiguration}.
|
||||||
|
*
|
||||||
|
* @author Greg Turnquist
|
||||||
|
*/
|
||||||
|
public class JmsTemplateAutoConfigurationTests {
|
||||||
|
|
||||||
|
private AnnotationConfigApplicationContext context;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDefaultJmsTemplate() {
|
||||||
|
this.context = new AnnotationConfigApplicationContext();
|
||||||
|
this.context
|
||||||
|
.register(TestConfiguration.class, JmsTemplateAutoConfiguration.class);
|
||||||
|
this.context.refresh();
|
||||||
|
JmsTemplate jmsTemplate = this.context.getBean(JmsTemplate.class);
|
||||||
|
ActiveMQConnectionFactory connectionFactory = this.context
|
||||||
|
.getBean(ActiveMQConnectionFactory.class);
|
||||||
|
assertNotNull(jmsTemplate);
|
||||||
|
assertNotNull(connectionFactory);
|
||||||
|
assertEquals(jmsTemplate.getConnectionFactory(), connectionFactory);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
protected static class TestConfiguration {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConnectionFactoryBackoff() {
|
||||||
|
this.context = new AnnotationConfigApplicationContext();
|
||||||
|
this.context.register(TestConfiguration2.class,
|
||||||
|
JmsTemplateAutoConfiguration.class);
|
||||||
|
this.context.refresh();
|
||||||
|
assertEquals("foobar", this.context.getBean(ActiveMQConnectionFactory.class)
|
||||||
|
.getBrokerURL());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
protected static class TestConfiguration2 {
|
||||||
|
@Bean
|
||||||
|
ConnectionFactory connectionFactory() {
|
||||||
|
return new ActiveMQConnectionFactory() {
|
||||||
|
{
|
||||||
|
setBrokerURL("foobar");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testJmsTemplateBackoff() {
|
||||||
|
this.context = new AnnotationConfigApplicationContext();
|
||||||
|
this.context.register(TestConfiguration3.class,
|
||||||
|
JmsTemplateAutoConfiguration.class);
|
||||||
|
this.context.refresh();
|
||||||
|
JmsTemplate jmsTemplate = this.context.getBean(JmsTemplate.class);
|
||||||
|
assertEquals(999, jmsTemplate.getPriority());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
protected static class TestConfiguration3 {
|
||||||
|
@Bean
|
||||||
|
JmsTemplate jmsTemplate(ConnectionFactory connectionFactory) {
|
||||||
|
JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
|
||||||
|
jmsTemplate.setPriority(999);
|
||||||
|
return jmsTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testJmsTemplateBackoffEverything() {
|
||||||
|
this.context = new AnnotationConfigApplicationContext();
|
||||||
|
this.context.register(TestConfiguration2.class, TestConfiguration3.class,
|
||||||
|
JmsTemplateAutoConfiguration.class);
|
||||||
|
this.context.refresh();
|
||||||
|
JmsTemplate jmsTemplate = this.context.getBean(JmsTemplate.class);
|
||||||
|
assertEquals(999, jmsTemplate.getPriority());
|
||||||
|
assertEquals("foobar", this.context.getBean(ActiveMQConnectionFactory.class)
|
||||||
|
.getBrokerURL());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPubSubEnabledByDefault() {
|
||||||
|
this.context = new AnnotationConfigApplicationContext();
|
||||||
|
this.context
|
||||||
|
.register(TestConfiguration.class, JmsTemplateAutoConfiguration.class);
|
||||||
|
this.context.refresh();
|
||||||
|
JmsTemplate jmsTemplate = this.context.getBean(JmsTemplate.class);
|
||||||
|
assertTrue(jmsTemplate.isPubSubDomain());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testJmsTemplatePostProcessedSoThatPubSubIsFalse() {
|
||||||
|
this.context = new AnnotationConfigApplicationContext();
|
||||||
|
this.context.register(TestConfiguration4.class,
|
||||||
|
JmsTemplateAutoConfiguration.class);
|
||||||
|
this.context.refresh();
|
||||||
|
JmsTemplate jmsTemplate = this.context.getBean(JmsTemplate.class);
|
||||||
|
assertFalse(jmsTemplate.isPubSubDomain());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
protected static class TestConfiguration4 implements BeanPostProcessor {
|
||||||
|
@Override
|
||||||
|
public Object postProcessAfterInitialization(Object bean, String beanName)
|
||||||
|
throws BeansException {
|
||||||
|
if (bean.getClass().isAssignableFrom(JmsTemplate.class)) {
|
||||||
|
JmsTemplate jmsTemplate = (JmsTemplate) bean;
|
||||||
|
jmsTemplate.setPubSubDomain(false);
|
||||||
|
}
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object postProcessBeforeInitialization(Object bean, String beanName)
|
||||||
|
throws BeansException {
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
package org.test
|
||||||
|
|
||||||
|
@Grab("org.apache.activemq:activemq-all:5.2.0")
|
||||||
|
|
||||||
|
import java.util.concurrent.CountDownLatch
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@Log
|
||||||
|
class JmsExample implements CommandLineRunner {
|
||||||
|
|
||||||
|
private CountDownLatch latch = new CountDownLatch(1)
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
JmsTemplate jmsTemplate
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
DefaultMessageListenerContainer jmsListener(ConnectionFactory connectionFactory) {
|
||||||
|
new DefaultMessageListenerContainer([
|
||||||
|
connectionFactory: connectionFactory,
|
||||||
|
destinationName: "spring-boot",
|
||||||
|
pubSubDomain: true,
|
||||||
|
messageListener: new MessageListenerAdapter(new Receiver(latch:latch)) {{
|
||||||
|
defaultListenerMethod = "receive"
|
||||||
|
}}
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
void run(String... args) {
|
||||||
|
def messageCreator = { session ->
|
||||||
|
session.createObjectMessage("Greetings from Spring Boot via ActiveMQ")
|
||||||
|
} as MessageCreator
|
||||||
|
log.info "Sending JMS message..."
|
||||||
|
jmsTemplate.pubSubDomain = true
|
||||||
|
jmsTemplate.send("spring-boot", messageCreator)
|
||||||
|
latch.await()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Log
|
||||||
|
class Receiver {
|
||||||
|
CountDownLatch latch
|
||||||
|
|
||||||
|
def receive(String message) {
|
||||||
|
log.info "Received ${message}"
|
||||||
|
latch.countDown()
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2013 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.cli.compiler.autoconfigure;
|
||||||
|
|
||||||
|
import org.codehaus.groovy.ast.ClassNode;
|
||||||
|
import org.codehaus.groovy.control.CompilationFailedException;
|
||||||
|
import org.codehaus.groovy.control.customizers.ImportCustomizer;
|
||||||
|
import org.springframework.boot.cli.compiler.AstUtils;
|
||||||
|
import org.springframework.boot.cli.compiler.CompilerAutoConfiguration;
|
||||||
|
import org.springframework.boot.cli.compiler.DependencyCustomizer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link CompilerAutoConfiguration} for Spring JMS.
|
||||||
|
*
|
||||||
|
* @author Greg Turnquist
|
||||||
|
*/
|
||||||
|
public class JmsCompilerAutoConfiguration extends CompilerAutoConfiguration {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean matches(ClassNode classNode) {
|
||||||
|
return AstUtils.hasAtLeastOneFieldOrMethod(classNode, "JmsTemplate",
|
||||||
|
"DefaultMessageListenerContainer", "SimpleMessageListenerContainer");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void applyDependencies(DependencyCustomizer dependencies)
|
||||||
|
throws CompilationFailedException {
|
||||||
|
dependencies.add("org.springframework", "spring-jms",
|
||||||
|
dependencies.getProperty("spring.version")).add(
|
||||||
|
"org.apache.geronimo.specs", "geronimo-jms_1.1_spec", "1.1");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void applyImports(ImportCustomizer imports) throws CompilationFailedException {
|
||||||
|
imports.addStarImports("javax.jms", "org.springframework.jms.core",
|
||||||
|
"org.springframework.jms.listener",
|
||||||
|
"org.springframework.jms.listener.adapter");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,5 +1,5 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<configuration>
|
<configuration>
|
||||||
<include resource="org/springframework/boot/logging/logback/base.xml"/>
|
<include resource="org/springframework/boot/logging/logback/base.xml"/>
|
||||||
<!-- logger name="org.springframework.jdbc" level="DEBUG"/-->
|
<!-- logger name="org.springframework.jms" level="DEBUG"/-->
|
||||||
</configuration>
|
</configuration>
|
||||||
|
Loading…
Reference in New Issue