diff --git a/spring-boot-cli/samples/jms.groovy b/spring-boot-cli/samples/jms.groovy index 3875a189b3d..09f5488e1ec 100644 --- a/spring-boot-cli/samples/jms.groovy +++ b/spring-boot-cli/samples/jms.groovy @@ -5,8 +5,7 @@ package org.test import java.util.concurrent.CountDownLatch @Log -@Configuration -@EnableJmsMessaging +@EnableJms class JmsExample implements CommandLineRunner { private CountDownLatch latch = new CountDownLatch(1) @@ -14,18 +13,6 @@ class JmsExample implements CommandLineRunner { @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") @@ -35,13 +22,10 @@ class JmsExample implements CommandLineRunner { log.info "Send JMS message, waiting..." latch.await() } -} -@Log -class Receiver { - CountDownLatch latch + @JmsListener(destination = 'spring-boot') def receive(String message) { log.info "Received ${message}" latch.countDown() } -} +} \ No newline at end of file diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/autoconfigure/JmsCompilerAutoConfiguration.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/autoconfigure/JmsCompilerAutoConfiguration.java index e2e89c27334..4c0b2c42b69 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/autoconfigure/JmsCompilerAutoConfiguration.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/autoconfigure/JmsCompilerAutoConfiguration.java @@ -28,29 +28,32 @@ import org.springframework.boot.groovy.EnableJmsMessaging; * {@link CompilerAutoConfiguration} for Spring JMS. * * @author Greg Turnquist + * @author Stephane Nicoll */ public class JmsCompilerAutoConfiguration extends CompilerAutoConfiguration { @Override public boolean matches(ClassNode classNode) { - // Slightly weird detection algorithm because there is no @Enable annotation for - // Spring JMS - return AstUtils.hasAtLeastOneAnnotation(classNode, "EnableJmsMessaging"); + return AstUtils.hasAtLeastOneAnnotation(classNode, "EnableJms") || + AstUtils.hasAtLeastOneAnnotation(classNode, "EnableJmsMessaging"); } @Override public void applyDependencies(DependencyCustomizer dependencies) throws CompilationFailedException { - dependencies.add("spring-jms", "geronimo-jms_1.1_spec"); + dependencies.add("spring-jms", "jms-api"); } @Override public void applyImports(ImportCustomizer imports) throws CompilationFailedException { - imports.addStarImports("javax.jms", "org.springframework.jms.core", + imports.addStarImports("javax.jms", + "org.springframework.jms.annotation", + "org.springframework.jms.config", + "org.springframework.jms.core", "org.springframework.jms.listener", - "org.springframework.jms.listener.adapter").addImports( - EnableJmsMessaging.class.getCanonicalName()); + "org.springframework.jms.listener.adapter") + .addImports(EnableJmsMessaging.class.getCanonicalName()); } } diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/groovy/EnableJmsMessaging.java b/spring-boot-cli/src/main/java/org/springframework/boot/groovy/EnableJmsMessaging.java index 3ea3ed31b2a..c24c4dc9223 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/groovy/EnableJmsMessaging.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/groovy/EnableJmsMessaging.java @@ -26,10 +26,13 @@ import org.springframework.boot.cli.compiler.autoconfigure.JmsCompilerAutoConfig /** * Pseudo annotation used to trigger {@link JmsCompilerAutoConfiguration}. + * + * @deprecated since 1.2.0 in favor of {@code EnableJms} */ @Target(ElementType.TYPE) @Documented @Retention(RetentionPolicy.RUNTIME) +@Deprecated public @interface EnableJmsMessaging { } diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/ReproIntegrationTests.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/ReproIntegrationTests.java index 971ddcb5da4..bc2fd35928a 100644 --- a/spring-boot-cli/src/test/java/org/springframework/boot/cli/ReproIntegrationTests.java +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/ReproIntegrationTests.java @@ -28,6 +28,7 @@ import static org.junit.Assert.assertThat; * * @author Phillip Webb * @author Andy Wilkinson + * @author Stephane Nicoll */ public class ReproIntegrationTests { @@ -71,4 +72,10 @@ public class ReproIntegrationTests { this.thrown.expectMessage("is not a JAR file"); this.cli.jar("secure.groovy", "crsh.groovy"); } + + @Test + public void jmsListener() throws Exception { + this.cli.run("jms.groovy"); + assertThat(this.cli.getOutput(), containsString("Hello World")); + } } diff --git a/spring-boot-cli/src/test/resources/repro-samples/jms.groovy b/spring-boot-cli/src/test/resources/repro-samples/jms.groovy new file mode 100644 index 00000000000..80bf5bb821c --- /dev/null +++ b/spring-boot-cli/src/test/resources/repro-samples/jms.groovy @@ -0,0 +1,33 @@ +package org.test + +@Grab("org.apache.activemq:activemq-all:5.4.0") +@Grab("activemq-pool") +import java.util.concurrent.CountDownLatch + +@Log +@EnableJms +class SampleJmsListener implements CommandLineRunner { + + private CountDownLatch latch = new CountDownLatch(1) + + @Autowired + JmsTemplate jmsTemplate + + void run(String... args) { + def messageCreator = { session -> + session.createObjectMessage("Hello World") + } as MessageCreator + log.info "Sending JMS message..." + jmsTemplate.send("testQueue", messageCreator) + log.info "Sent JMS message, waiting..." + latch.await() + } + + @JmsListener(destination = 'testQueue') + def receive(String message) { + log.info "Received ${message}" + latch.countDown() + } +} + +