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 `foo.bar.#` which means any message sent with a routing key beginning with `foo.bar.` 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
include::complete/src/main/java/hello/Runner.java[]
----
Notice that the template routes the message to the exchange, with a routing key of `foo.bar.baz` 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;
@SpringBootApplication
public class Application {
final static String queueName = "spring-boot";
static final String topicExchangeName = "spring-boot-exchange";
static final String queueName = "spring-boot";
@Bean
Queue queue() {
......@@ -23,12 +25,12 @@ public class Application {
@Bean
TopicExchange exchange() {
return new TopicExchange("spring-boot-exchange");
return new TopicExchange(topicExchangeName);
}
@Bean
Binding binding(Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(queueName);
return BindingBuilder.bind(queue).to(exchange).with("foo.bar.#");
}
@Bean
......@@ -47,7 +49,7 @@ public class Application {
}
public static void main(String[] args) throws InterruptedException {
SpringApplication.run(Application.class, args);
SpringApplication.run(Application.class, 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;
@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;
}
@Override
public void run(String... args) throws Exception {
System.out.println("Sending message...");
rabbitTemplate.convertAndSend(Application.queueName, "Hello from RabbitMQ!");
rabbitTemplate.convertAndSend(Application.topicExchangeName, "foo.bar.baz", "Hello from RabbitMQ!");
receiver.getLatch().await(10000, TimeUnit.MILLISECONDS);
context.close();
}
}
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