BAEL-2688 Moved example code from spring-amqp-simple to spring-amqp project. Also updated for Spring Boot.

This commit is contained in:
pcoates 2019-08-02 16:43:35 +01:00
parent 968710433a
commit 4f948b26f8
10 changed files with 13 additions and 120 deletions

View File

@ -43,10 +43,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>${start-class}</mainClass>
<!-- this is necessary as we're not using the Boot parent -->
</configuration>
</plugin>
</plugins>
</pluginManagement>
@ -60,14 +56,6 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<!-- The following enables the "thin jar" deployment option. -->
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-thin-layout</artifactId>
<version>${thin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

View File

@ -13,7 +13,6 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
@Profile("!test")
public class SpringAmqpConfig {
public final static String queueName = "com.baeldung.spring-amqp-simple.queue";

View File

@ -1,11 +1,6 @@
package com.baeldung.springamqpsimple.broadcast;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Declarable;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
@ -17,7 +12,6 @@ import java.util.Arrays;
import java.util.List;
@Configuration
@Profile("!test")
public class BroadcastConfig {
public final static String fanoutQueue1Name = "com.baeldung.spring-amqp-simple.fanout.queue1";
@ -29,29 +23,28 @@ public class BroadcastConfig {
public final static String topicExchangeName = "com.baeldung.spring-amql-simple.topic.exchange";
@Bean
public List<Declarable> topicBindings() {
public Declarables topicBindings() {
Queue topicQueue1 = new Queue(topicQueue1Name, false);
Queue topicQueue2 = new Queue(topicQueue2Name, false);
TopicExchange topicExchange = new TopicExchange(topicExchangeName);
return Arrays.asList(
return new Declarables(
topicQueue1,
topicQueue2,
topicExchange,
BindingBuilder.bind(topicQueue1).to(topicExchange).with("*.important.*"),
BindingBuilder.bind(topicQueue2).to(topicExchange).with("user.#")
);
BindingBuilder.bind(topicQueue2).to(topicExchange).with("user.#"));
}
@Bean
public List<Declarable> fanoutBindings() {
public Declarables fanoutBindings() {
Queue fanoutQueue1 = new Queue(fanoutQueue1Name, false);
Queue fanoutQueue2 = new Queue(fanoutQueue2Name, false);
FanoutExchange fanoutExchange = new FanoutExchange(fanoutExchangeName);
return Arrays.asList(
return new Declarables(
fanoutQueue1,
fanoutQueue2,
fanoutExchange,

View File

@ -1,12 +1,11 @@
package org.baeldung;
import com.baeldung.springamqpsimple.SpringAmqpApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.springamqpsimple.SpringAmqpApplication;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringAmqpApplication.class)
public class SpringContextManualTest {

View File

@ -1,3 +1,4 @@
## Relevant articles:
- [Messaging With Spring AMQP](http://www.baeldung.com/spring-amqp)
- [Messaging With Spring AMQP](https://www.baeldung.com/spring-amqp)
- [RabbitMQ Message Dispatching with Spring AMQP](https://www.baeldung.com/rabbitmq-spring-amqp)

View File

@ -9,31 +9,16 @@
<description>Introduction to Spring-AMQP</description>
<parent>
<artifactId>parent-boot-2</artifactId>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>${spring-rabbit}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
<build>
<finalName>spring-amqp</finalName>
</build>
<properties>
<spring-rabbit>1.6.6.RELEASE</spring-rabbit>
</properties>
</project>

View File

@ -1,7 +0,0 @@
package com.baeldung.springamqp.consumer;
public class Consumer {
public void listen(String foo) {
System.out.println(foo);
}
}

View File

@ -1,17 +0,0 @@
package com.baeldung.springamqp.producer;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Producer {
public static void main(String[] args) throws InterruptedException {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
AmqpTemplate template = ctx.getBean(RabbitTemplate.class);
template.convertAndSend("Hello, world!");
Thread.sleep(1000);
ctx.destroy();
}
}

View File

@ -1,35 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xmlns:c="http://www.springframework.org/schema/c" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd">
<rabbit:connection-factory id="connectionFactory" host="localhost" username="guest" password="guest" />
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory"
exchange="myExchange" routing-key="foo.bar" />
<rabbit:admin connection-factory="connectionFactory" />
<rabbit:queue name="myQueue" />
<rabbit:topic-exchange name="myExchange">
<rabbit:bindings>
<rabbit:binding queue="myQueue" pattern="foo.*" />
</rabbit:bindings>
</rabbit:topic-exchange>
<rabbit:listener-container
connection-factory="connectionFactory">
<rabbit:listener ref="consumer" method="listen"
queue-names="myQueue" />
</rabbit:listener-container>
<bean id="consumer" class="com.baeldung.springamqp.consumer.Consumer" />
</beans>

View File

@ -1,13 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>