Commit b51f24cf authored by Gary Russell's avatar Gary Russell Committed by Greg Turnquist

Polishing - actually use the exchange/binding. (#17)

The rabbit template was sending the message to the default exchange `""` instead
of the configured topic exchange.

Use a routing key.
parent 9a431a67
......@@ -115,6 +115,8 @@ The `queue()` method creates an AMQP queue. The `exchange()` method creates a to
NOTE: Spring AMQP requires that the `Queue`, the `TopicExchange`, and the `Binding` be declared as top level Spring beans in order to be set up properly.
In this case, we use a topic exchange and the queue is bound with routing key `` which means any message sent with a routing key beginning with `` will be routed to the queue.
== Send a Test Message
Test messages are sent by a `CommandLineRunner`, which also waits for the latch in the receiver and closes the application context:
......@@ -125,6 +127,8 @@ Test messages are sent by a `CommandLineRunner`, which also waits for the latch
Notice that the template routes the message to the exchange, with a routing key of `` which matches the binding.
The runner can be mocked out in tests, so that the receiver can be tested in isolation.
== Run the Application
......@@ -14,7 +14,9 @@ import org.springframework.context.annotation.Bean;
public class Application {
final static String queueName = "spring-boot";
static final String topicExchangeName = "spring-boot-exchange";
static final String queueName = "spring-boot";
Queue queue() {
......@@ -23,12 +25,12 @@ public class Application {
TopicExchange exchange() {
return new TopicExchange("spring-boot-exchange");
return new TopicExchange(topicExchangeName);
Binding binding(Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(queueName);
return BindingBuilder.bind(queue).to(exchange).with("");
......@@ -47,7 +49,7 @@ public class Application {
public static void main(String[] args) throws InterruptedException {, args);, args).close();
......@@ -4,7 +4,6 @@ import java.util.concurrent.TimeUnit;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;
......@@ -12,21 +11,17 @@ public class Runner implements CommandLineRunner {
private final RabbitTemplate rabbitTemplate;
private final Receiver receiver;
private final ConfigurableApplicationContext context;
public Runner(Receiver receiver, RabbitTemplate rabbitTemplate,
ConfigurableApplicationContext context) {
public Runner(Receiver receiver, RabbitTemplate rabbitTemplate) {
this.receiver = receiver;
this.rabbitTemplate = rabbitTemplate;
this.context = context;
public void run(String... args) throws Exception {
System.out.println("Sending message...");
rabbitTemplate.convertAndSend(Application.queueName, "Hello from RabbitMQ!");
rabbitTemplate.convertAndSend(Application.topicExchangeName, "", "Hello from RabbitMQ!");
receiver.getLatch().await(10000, TimeUnit.MILLISECONDS);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment