diff --git a/spring-boot-samples/pom.xml b/spring-boot-samples/pom.xml index 18f8e32833..85fb2ae60d 100644 --- a/spring-boot-samples/pom.xml +++ b/spring-boot-samples/pom.xml @@ -20,6 +20,7 @@ ${basedir}/.. + spring-boot-sample-activemq spring-boot-sample-actuator spring-boot-sample-actuator-log4j spring-boot-sample-actuator-log4j2 diff --git a/spring-boot-samples/spring-boot-sample-activemq/pom.xml b/spring-boot-samples/spring-boot-sample-activemq/pom.xml new file mode 100644 index 0000000000..8ac54f36cf --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-activemq/pom.xml @@ -0,0 +1,46 @@ + + + + spring-boot-samples + org.springframework.boot + 1.3.0.BUILD-SNAPSHOT + + 4.0.0 + spring-boot-sample-activemq + Spring Boot ActiveMQ Sample + Spring Boot ActiveMQ Sample + http://projects.spring.io/spring-boot/ + + + + org.springframework.boot + spring-boot-starter + + + org.springframework + spring-jms + + + org.apache.activemq + activemq-broker + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/spring-boot-samples/spring-boot-sample-activemq/src/main/java/sample/activemq/Consumer.java b/spring-boot-samples/spring-boot-sample-activemq/src/main/java/sample/activemq/Consumer.java new file mode 100644 index 0000000000..30aa2ef455 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-activemq/src/main/java/sample/activemq/Consumer.java @@ -0,0 +1,30 @@ +/* + * Copyright 2012-2015 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 sample.activemq; + +import org.springframework.jms.annotation.JmsListener; +import org.springframework.stereotype.Component; + +@Component +public class Consumer { + + @JmsListener(destination = "sample.queue") + public void receiveQueue(String text) { + System.out.println(text); + } + +} diff --git a/spring-boot-samples/spring-boot-sample-activemq/src/main/java/sample/activemq/Producer.java b/spring-boot-samples/spring-boot-sample-activemq/src/main/java/sample/activemq/Producer.java new file mode 100644 index 0000000000..0f972b0af4 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-activemq/src/main/java/sample/activemq/Producer.java @@ -0,0 +1,45 @@ +/* + * Copyright 2012-2015 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 sample.activemq; + +import javax.jms.Queue; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.jms.core.JmsMessagingTemplate; +import org.springframework.stereotype.Component; + +@Component +public class Producer implements CommandLineRunner { + + @Autowired + private JmsMessagingTemplate jmsMessagingTemplate; + + @Autowired + private Queue queue; + + @Override + public void run(String... args) throws Exception { + send("Sample message"); + System.out.println("Message was sent to the Queue"); + } + + public void send(String msg) { + jmsMessagingTemplate.convertAndSend(queue, msg); + } + +} diff --git a/spring-boot-samples/spring-boot-sample-activemq/src/main/java/sample/activemq/SampleActiveMQApplication.java b/spring-boot-samples/spring-boot-sample-activemq/src/main/java/sample/activemq/SampleActiveMQApplication.java new file mode 100644 index 0000000000..5ff035c4b5 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-activemq/src/main/java/sample/activemq/SampleActiveMQApplication.java @@ -0,0 +1,40 @@ +/* + * Copyright 2012-2015 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 sample.activemq; + +import org.apache.activemq.command.ActiveMQQueue; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.jms.annotation.EnableJms; + +import javax.jms.Queue; + +@SpringBootApplication +@EnableJms +public class SampleActiveMQApplication { + + @Bean + public Queue queue() { + return new ActiveMQQueue("sample.queue"); + } + + public static void main(String[] args) { + SpringApplication.run(SampleActiveMQApplication.class, args); + } + +} diff --git a/spring-boot-samples/spring-boot-sample-activemq/src/main/resources/application.properties b/spring-boot-samples/spring-boot-sample-activemq/src/main/resources/application.properties new file mode 100644 index 0000000000..e6f67126ea --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-activemq/src/main/resources/application.properties @@ -0,0 +1,2 @@ +spring.activemq.in-memory=true +spring.activemq.pooled=false \ No newline at end of file diff --git a/spring-boot-samples/spring-boot-sample-activemq/src/test/java/sample/activemq/SampleActiveMqTests.java b/spring-boot-samples/spring-boot-sample-activemq/src/test/java/sample/activemq/SampleActiveMqTests.java new file mode 100644 index 0000000000..f9c8d35e19 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-activemq/src/test/java/sample/activemq/SampleActiveMqTests.java @@ -0,0 +1,48 @@ +/* + * Copyright 2012-2015 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 sample.activemq; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.OutputCapture; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import javax.jms.JMSException; + +import static org.junit.Assert.assertTrue; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = {SampleActiveMQApplication.class}) +public class SampleActiveMqTests { + + @Rule + public OutputCapture outputCapture = new OutputCapture(); + + @Autowired + private Producer producer; + + @Test + public void sendSimpleMessage() throws InterruptedException, JMSException { + producer.send("Test message"); + Thread.sleep(1000L); + assertTrue(outputCapture.toString().contains("Test message")); + } + +}