Difference between revisions of "Spring RabbitMQ cheat sheet"

From DarkWiki
Jump to: navigation, search
Line 38: Line 38:
 
</source>
 
</source>
  
You can then send ''EventMessage'' payloads with event ids such as "event.user.deleted" using the ''signal'' method.
+
You can then send ''EventMessage'' payloads with event ids such as "event.user.created" or "event.user.deleted" using the ''signal'' method.
 +
 
 +
===Listening for events===
 +
 
 +
This shows how to use two dynamically created queues. They will not be persistent (won't survive restart) and
 +
 
 +
<source lang="java">
 +
    @RabbitListener(bindings = @QueueBinding(
 +
            value = @Queue,
 +
            exchange = @Exchange(value = "eventExchange", type = "topic"),
 +
            key = "event.#.created"))
 +
    public void consumeCreated(EventMessage message) {
 +
        // ...
 +
    }
 +
 
 +
    @RabbitListener(bindings = @QueueBinding(
 +
            value = @Queue,
 +
            exchange = @Exchange(value = "eventExchange", type = "topic"),
 +
            key = "event.#.deleted"))
 +
    public void consumeDeleted(EventMessage message) {
 +
        // ...
 +
    }
 +
</source>

Revision as of 16:31, 2 April 2020

Sending to topics

On the Producer side (the one that raises the events) we create our TopicExchange, giving it a sensible name:

import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class EventProducerConfig {

    @Bean("eventExchange")
    public TopicExchange eventExchange() {
        return new TopicExchange("eventExchange");
    }

}

It can then be used by our Producer to send event notifications to anyone who subscribes to the exchange:

@Component
public class EventProducer {
    private final RabbitTemplate rabbitTemplate;
    private final Exchange exchange;

    public EventProducer(RabbitTemplate rabbitTemplate,@Qualifier("eventExchange") Exchange exchange) {
        this.rabbitTemplate = rabbitTemplate;
        this.exchange = exchange;
    }

    public void signal(String eventId,EventMessage message) {
        rabbitTemplate.convertAndSend(exchange.getName(), eventId, message);
    }
}

You can then send EventMessage payloads with event ids such as "event.user.created" or "event.user.deleted" using the signal method.

Listening for events

This shows how to use two dynamically created queues. They will not be persistent (won't survive restart) and

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue,
            exchange = @Exchange(value = "eventExchange", type = "topic"),
            key = "event.#.created"))
    public void consumeCreated(EventMessage message) {
        // ...
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue,
            exchange = @Exchange(value = "eventExchange", type = "topic"),
            key = "event.#.deleted"))
    public void consumeDeleted(EventMessage message) {
        // ...
    }