+ All Categories
Home > Documents > Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter...

Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter...

Date post: 14-Feb-2018
Category:
Upload: vanquynh
View: 230 times
Download: 0 times
Share this document with a friend
46
SpringOne 2GX 2010. All rights reserved. Do not distribute without permission. Chicago, October 19 - 22, 2010 Grails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010
Transcript
Page 1: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

SpringOne 2GX 2010. All rights reserved. Do not distribute without permission.

Chicago, October 19 - 22, 2010

Grails + messaging with AMQP/RabbitMQ

Peter Ledbrook - SpringSource

Friday, 22 October 2010

Page 2: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

2

A history of messaging

Friday, 22 October 2010

Page 3: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

A history of messaging

Friday, 22 October 2010

Page 4: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

A history of messaging

4

Friday, 22 October 2010

Page 5: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Java messaging - JMS

5

• Java API• Synchronous and asynchronous messaging• Point-to-point and broadcast

– P2P via Queues– Broadcast via Topics

• No standard for communication between client and broker

Friday, 22 October 2010

Page 6: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

JMS Queues

6

Producer

Broker

Queue Consumer

Friday, 22 October 2010

Page 7: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

JMS Topics

7

Producer

Broker

Topic

Consumer

Consumer

Consumer

Friday, 22 October 2010

Page 8: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

The challenger - AMQP

8

• Advanced Message Queuing Protocol• Wire-level protocol

– Any type of client– Client-broker communication standardised

• Synchronous and asynchronous messaging• Point-to-point, broadcast, and more

– Single, flexible model• Simple management part of the protocol

– Create exchanges and queues

Friday, 22 October 2010

Page 9: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Basic structure

9

Producer

Broker

Exchange

Consumer

Consumer

Consumer

Queue

QueueConsumer

Friday, 22 October 2010

Page 10: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Basic structure

9

Producer

Broker

Exchange

Consumer

Consumer

Consumer

Queue

QueueConsumer

Exchange: accepts and routes messages from producer

Friday, 22 October 2010

Page 11: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Basic structure

9

Producer

Broker

Exchange

Consumer

Consumer

Consumer

Queue

QueueConsumer

Exchange: accepts and routes messages from producer

Queue: a FIFO queue of messages - each message can only go to one consumer

Friday, 22 October 2010

Page 12: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Basic structure

9

Producer

Broker

Exchange

Consumer

Consumer

Consumer

Queue

QueueConsumer

Exchange: accepts and routes messages from producer

Binding: rule for routing messages to associated queue

Queue: a FIFO queue of messages - each message can only go to one consumer

Friday, 22 October 2010

Page 13: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Exhanges

10

• Only producers talk to the exchange directly• Message routing depends on

– Exchange type– Message’s ‘routing key’, e.g. “stocks.nasdaq.vmw”– Binding between exchange and queue

• Routing and binding keys are typically strings– Allow for filtering - similar to JMS selectors

Friday, 22 October 2010

Page 14: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Exhange types

11

• Fanout– Messages go to all bound queues– Routing and binding keys are ignored

• Direct– Messages only go to queues with a binding key that exactly

matches the routing key– Typically routing key is the queue name

• Topic– Like Direct exchange but binding key can have wildcards– ‘#’ like regex ‘*’, ‘*’ like regex ‘?’

• Headers– Routing based on message headers

Friday, 22 October 2010

Page 15: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Example: JMS-like Queue

12

Producer

Broker

DirectExchange ConsumermyQueue

Friday, 22 October 2010

Page 16: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Example: JMS-like Queue

12

Producer

Broker

DirectExchange ConsumermyQueue

routing-key = myQueue

Friday, 22 October 2010

Page 17: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Example: JMS-like Queue

12

Producer

Broker

DirectExchange ConsumermyQueue

routing-key = myQueue

binding = myQueue

Friday, 22 October 2010

Page 18: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Example: JMS-like Topic

13

Producer

Broker

FanoutExchange

ConsumerQueue

ConsumerQueue

ConsumerQueue

Friday, 22 October 2010

Page 19: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Example: JMS-like Topic

13

Producer

Broker

FanoutExchange

ConsumerQueue

Routing key not specified

ConsumerQueue

ConsumerQueue

Friday, 22 October 2010

Page 20: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Example: JMS-like Topic

13

Producer

Broker

FanoutExchange

ConsumerQueue

Routing key not specified

Binding not specified

ConsumerQueue

ConsumerQueue

Friday, 22 October 2010

Page 21: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Example: broadcast with filtering

14

Producer

Broker

TopicExchange

ConsumerQueue

ConsumerQueue

ConsumerQueue

Friday, 22 October 2010

Page 22: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Example: broadcast with filtering

14

Producer

Broker

TopicExchange

ConsumerQueue

routing-key = shares.nyse.vmw

ConsumerQueue

ConsumerQueue

Friday, 22 October 2010

Page 23: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Example: broadcast with filtering

14

Producer

Broker

TopicExchange

ConsumerQueue

routing-key = shares.nyse.vmw

binding = shares.#

ConsumerQueue

ConsumerQueue

Friday, 22 October 2010

Page 24: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Example: broadcast with filtering

14

Producer

Broker

TopicExchange

ConsumerQueue

routing-key = shares.nyse.vmw

ConsumerQueue

ConsumerQueue

binding = shares.*

Friday, 22 October 2010

Page 25: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Example: broadcast with filtering

14

Producer

Broker

TopicExchange

ConsumerQueue

routing-key = shares.nyse.vmw

ConsumerQueue

ConsumerQueue

binding = shares.nyse.vmw

Friday, 22 October 2010

Page 26: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Example: RPC

15

Producer

Broker

DirectExchange

ConsumermyQueue

replyQ

Friday, 22 October 2010

Page 27: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Example: RPC

15

Producer

Broker

DirectExchange

ConsumermyQueue

replyQrouting-key = myQueuereply-to = replyQ

Friday, 22 October 2010

Page 28: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Example: RPC

15

Producer

Broker

DirectExchange

ConsumermyQueue

binding = myQueue

replyQrouting-key = myQueuereply-to = replyQ

Friday, 22 October 2010

Page 29: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Example: RPC

15

Producer

Broker

DirectExchange

ConsumermyQueue

replyQ

routing-key = replyQ

Friday, 22 October 2010

Page 30: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Example: RPC

15

Producer

Broker

DirectExchange

ConsumermyQueue

replyQ

binding = replyQ

routing-key = replyQ

Friday, 22 October 2010

Page 31: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Example: work distribution

16

Producer

Broker

DirectExchange

Consumer

ConsumerQueue

Consumer

Friday, 22 October 2010

Page 32: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Example: work distribution

16

Producer

Broker

DirectExchange

Consumer

routing-key = some.task

ConsumerQueue

Consumer

Friday, 22 October 2010

Page 33: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Example: work distribution

16

Producer

Broker

DirectExchange

Consumer

routing-key = some.task

binding = some.task

ConsumerQueue

Consumer

Friday, 22 October 2010

Page 34: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Messages

17

• Headers– routing-key– reply-to– content-type, etc.

• Custom properties• Body

– Byte data– Producer and consumer must agree on the format of the content– ... or use content-type header– AMQP does not define a meaning for content-type!

Friday, 22 October 2010

Page 35: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Queue and exchange properties

18

• Durable– Survives a broker restart– Applies to exchanges and queues

• Auto delete– Exchange will be deleted when all its bindings are gone– Queue will be deleted when all consumers are gone

• Exclusive– Only the owner can read messages from the queue– Doesn’t apply to exchanges

Friday, 22 October 2010

Page 36: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

The Grails integration

19

• RabbitMQ plugin• Declare exchanges and queues• Configure services as queue consumers

– Simple static properties• Dynamic method for sending AMQP messages

Friday, 22 October 2010

Page 37: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Consuming messages

20

class ListenerService { // Declare name of queue to listen to static rabbitQueue = "msgs"

void handleMessage(msg) { // Do something with the message }}

class AnotherListenerService { // Subscribe to a topic exchange static rabbitSubscribe = "sharesExchange"

void handleMessage(msg) { // Do something with the message }}

Friday, 22 October 2010

Page 38: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Consuming messages

20

class ListenerService { // Declare name of queue to listen to static rabbitQueue = "msgs"

void handleMessage(msg) { // Do something with the message }}

class AnotherListenerService { // Subscribe to a topic exchange static rabbitSubscribe = "sharesExchange"

void handleMessage(msg) { // Do something with the message }}

class AnotherListenerService { // Subscribe to a topic exchange static rabbitSubscribe = [ name: "myEx", routingKey: "shares.#" ]

void handleMessage(msg) { // Do something with the message }}

Friday, 22 October 2010

Page 39: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

class PublisherService {

def notify() { rabbitSend "msgs", "app.event", "The event details" }}

Sending messages

21

Friday, 22 October 2010

Page 40: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

class PublisherService {

def notify() { rabbitSend "msgs", "app.event", "The event details" }}

Sending messages

21

Routing key

Exchange name(optional) Message body

Friday, 22 October 2010

Page 41: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

class PublisherService {

def notify() { rabbitSend "msgs", "app.event", "The event details" }}

Sending messages

21

class PublisherService {

def notify(String itemName) { rabbitSend "msgs", "app.event", [event: "publish", item: itemName ] }}

Friday, 22 October 2010

Page 42: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Declaring exchanges and queues

22

// Config.groovyrabbitmq { connectionfactory { ... }

queues = { msgs durable: false, autoDelete: true exchange name: "shares", type: topic, durable: true, { allShares durable: true, autoDelete: false, binding: 'shares.#' } }}

Friday, 22 October 2010

Page 43: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

Declaring exchanges and queues

22

// Config.groovyrabbitmq { connectionfactory { ... }

queues = { msgs durable: false, autoDelete: true exchange name: "shares", type: topic, durable: true, { allShares durable: true, autoDelete: false, binding: 'shares.#' } }}

Standalone queue (msgs) - bound to default direct exchange

Topic exchange (shares)

Queue (allShares) bound to exchange (shares) with routing key ('shares.#')

Friday, 22 October 2010

Page 44: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

A word about message content

23

• In the broker, it’s just byte data• Plugin interprets data based on content-type header

– Spring AMQP SimpleMessageConverter– String ➝ text/plain; charset=utf-8– Serializable ➝ application/x-java-serialized-object– Otherwise, just byte[]

• Producers & consumers typically agree on format• Not all clients set the content-type!• You still have to agree on format even if you use JSON or

XML message content

Friday, 22 October 2010

Page 45: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.

Chicago, October 19 - 22, 2010

Demo

Friday, 22 October 2010

Page 46: Grails + messaging with AMQP/ RabbitMQ · PDF fileGrails + messaging with AMQP/ RabbitMQ Peter Ledbrook - SpringSource Friday, 22 October 2010. 2 A history of messaging ... The Grails

SpringOne 2GX 2009. All rights reserved. Do not distribute without permission.

Chicago, October 19 - 22, 2010

Q&A

Friday, 22 October 2010


Recommended