+ All Categories
Home > Technology > Syer amqp-s2gx-2011

Syer amqp-s2gx-2011

Date post: 18-Nov-2014
Category:
Upload: dave-syer
View: 2,307 times
Download: 1 times
Share this document with a friend
Description:
Introduction to AMQP, and comparison with JMS, plus an overview of Spring AMQP.
56
Advanced Messaging with AMQP Dave Syer, SpringSource, 2011
Transcript
Page 1: Syer amqp-s2gx-2011

Advanced Messaging with AMQPDave Syer, SpringSource, 2011

Page 2: Syer amqp-s2gx-2011

Agenda

● Overview of AMQP● Example use cases● What makes AMQP different?● Comparison with JMS● Spring AMQP

Page 3: Syer amqp-s2gx-2011

What is Messaging?

What can I use Messaging for?How does AMQP help?

Page 4: Syer amqp-s2gx-2011

What is Messaging?

What is a Messaging Protocol?What is it not?

Page 5: Syer amqp-s2gx-2011

Messaging Use Cases

Things a Message Broker Can Do ● Work distribution

● Pipelining

● Store-and-forward of messages

● Absorbing spikes of activity (again, decoupling)

● Routing to different consumers based on message properties

Page 6: Syer amqp-s2gx-2011

Messaging Use Cases

Patterns:● spatial decoupling (aka communication)

● temporal decoupling (aka buffering)

● logical decoupling (aka routing)

Promote: flexibility, resilience, performance, and scale

Conclusion: There is an enormous range of applications and problems to which messaging is an effective solution.

Page 7: Syer amqp-s2gx-2011

AMQP

AMQP = Advanced Message Queueing Protocol

Page 8: Syer amqp-s2gx-2011

Why AMQP?

AMQP is Especially Nice Because● All resources are dynamically created and destroyed by clients as

they need them – no static pre-configuration

● A clean and simple model: just three key nouns to learn

● Open standard, developed by the AMQP Working Group (VMware is a member)

● Lots of client libraries available in many languages, for free

● An excellent, freely available, open source broker implementation, called RabbitMQ.

Page 9: Syer amqp-s2gx-2011

AMQP 101

Create an exchange...

“my_exchange”type = fanout

Advanced != Complicated

X

Page 10: Syer amqp-s2gx-2011

AMQP 101

...create a queue...

“my_exchange”type = fanout

Advanced != Complicated

X

“my_queue”

Page 11: Syer amqp-s2gx-2011

AMQP 101

...add a binding...

“my_exchange”type = fanout

Advanced != Complicated

X

“my_queue”

Page 12: Syer amqp-s2gx-2011

AMQP 101

...all inside a broker...

“my_exchange”type = fanout

Advanced != Complicated

X

“my_queue”

Page 13: Syer amqp-s2gx-2011

AMQP 101

...publish a message...

“my_exchange”type = fanout

Advanced != Complicated

X

“my_queue”

Page 14: Syer amqp-s2gx-2011

AMQP 101

...message sits in queue.

“my_exchange”type = fanout

Advanced != Complicated

X

“my_queue”

Page 15: Syer amqp-s2gx-2011

AMQP 101

Publish another message...

“my_exchange”type = fanout

Advanced != Complicated

X

“my_queue”

Page 16: Syer amqp-s2gx-2011

AMQP 101

...it also sits in queue.

“my_exchange”type = fanout

Advanced != Complicated

X

“my_queue”

Page 17: Syer amqp-s2gx-2011

AMQP 101

Consumer retrieves messages in order...

“my_exchange”type = fanout

Advanced != Complicated

X

“my_queue”

Page 18: Syer amqp-s2gx-2011

AMQP 101

Consumer retrieves messages in order...

“my_exchange”type = fanout

Advanced != Complicated

X

“my_queue”

Page 19: Syer amqp-s2gx-2011

AMQP 101

With no binding, the message is discarded(producer can ask to be notified)

“my_exchange”type = fanout

Advanced != Complicated

X

“my_queue”

Page 20: Syer amqp-s2gx-2011

AMQP 101

Publish several messages...

“my_exchange”type = fanout

Advanced != Complicated

X

“my_queue”

ED

CB

A

Page 21: Syer amqp-s2gx-2011

AMQP 101

and they are distributed amongst consumers on the same queue...

“my_exchange”type = fanout

Advanced != Complicated

X

“my_queue”

E

D

C

B

A

Page 22: Syer amqp-s2gx-2011

AMQP 101

Let's create another queue and bind it to the same exchange...

“my_exchange”type = fanout

Advanced != Complicated

X“my_queue”

“your_queue”

Page 23: Syer amqp-s2gx-2011

AMQP 101

..with a fanout exchange...

“my_exchange”type = fanout

Advanced != Complicated

X“my_queue”

“your_queue”

BA

Page 24: Syer amqp-s2gx-2011

AMQP 101

..all messages go to every queue...

“my_exchange”type = fanout

Advanced != Complicated

X“my_queue”

“your_queue”

B A

B A

Page 25: Syer amqp-s2gx-2011

AMQP 101

...and can be consumed...

“my_exchange”type = fanout

Advanced != Complicated

X“my_queue”

A

“your_queue”B A

B

Page 26: Syer amqp-s2gx-2011

AMQP 101

...at different rates...

“my_exchange”type = fanout

Advanced != Complicated

X“my_queue”

A

“your_queue” B A

B

Page 27: Syer amqp-s2gx-2011

AMQP 101

...depending on the consumer, network and broker

“my_exchange”type = fanout

Advanced != Complicated

X“my_queue”

A

“your_queue” B A

B

Page 28: Syer amqp-s2gx-2011

AMQP 101

Let's change the exchange to be a direct type...

Advanced != Complicated

X“my_queue”

“your_queue”“my_exchange”type = direct

Page 29: Syer amqp-s2gx-2011

AMQP 101

...then the bindings have to have a key...

“my_exchange”type = direct

Advanced != Complicated

X“my_queue”

“your_queue”

bk = “vodka”

bk = “beer”

bk = Binding Key

Page 30: Syer amqp-s2gx-2011

AMQP 101

...and the messages have to have a routing key...

“my_exchange”type = direct

Advanced != Complicated

X“my_queue”

“your_queue”

bk = “vodka”

bk = “beer”

rk = “vodka”

rk = Routing Keybk = Binding Key

Page 31: Syer amqp-s2gx-2011

AMQP 101

...a direct exchange matches therouting key to a binding key...

“my_exchange”type = direct

Advanced != Complicated

X“my_queue”

“your_queue”

bk = “vodka”

bk = “beer”

rk = Routing Keybk = Binding Key

rk = “beer”

Page 32: Syer amqp-s2gx-2011

AMQP 101

...a direct exchange matches therouting key to a binding key...

“my_exchange”type = direct

Advanced != Complicated

X“my_queue”

“your_queue”

bk = “vodka”

bk = “beer”

rk = Routing Keybk = Binding Key

Page 33: Syer amqp-s2gx-2011

AMQP 101

...messages with no matching binding...

“my_exchange”type = direct

Advanced != Complicated

X“my_queue”

“your_queue”

bk = “vodka”

bk = “beer”

rk = Routing Keybk = Binding Key

rk = “tequila”

Page 34: Syer amqp-s2gx-2011

AMQP 101

...are discarded

“my_exchange”type = direct

Advanced != Complicated

X“my_queue”

“your_queue”

bk = “vodka”

bk = “beer”

rk = Routing Keybk = Binding Key

Page 35: Syer amqp-s2gx-2011

AMQP 101

Let's change the exchange to be a topic type...

Advanced != Complicated

X“my_queue”

“your_queue”“my_exchange”type = topic

Page 36: Syer amqp-s2gx-2011

AMQP 101

...permits wildcard in bindings...

“my_exchange”type = topic

Advanced != Complicated

X“my_queue”

“your_queue”

bk = “a.*.c”

bk = “#.e”

bk = Binding Key

Page 37: Syer amqp-s2gx-2011

AMQP 101

Binding keys are period(.)-separated:* matches 1 element# matches 0 or more elements

“my_exchange”type = topic

Advanced != Complicated

X“my_queue”

“your_queue”

bk = “a.*.c”

bk = “#.e”

rk = Routing Keybk = Binding Key

EDCBArk = “a”

rk = “a.b”rk = “a.b.c”

rk = “a.e”rk = “a.b.e”

Page 38: Syer amqp-s2gx-2011

AMQP 101

Binding keys are period(.)-separated:* matches 1 element# matches 0 or more elements

“my_exchange”type = topic

Advanced != Complicated

X“my_queue”

“your_queue”

bk = “a.*.c”

bk = “#.e”

rk = Routing Keybk = Binding Key

EDCBArk = “a”

rk = “a.b”rk = “a.b.c”

rk = “a.e”rk = “a.b.e”

X

Page 39: Syer amqp-s2gx-2011

AMQP 101

Binding keys are period(.)-separated:* matches 1 element# matches 0 or more elements

“my_exchange”type = topic

Advanced != Complicated

X“my_queue”

“your_queue”

bk = “a.*.c”

bk = “#.e”

rk = Routing Keybk = Binding Key

EDCBrk = “a.b”

rk = “a.b.c”

rk = “a.e”rk = “a.b.e”

Page 40: Syer amqp-s2gx-2011

AMQP 101

Binding keys are period(.)-separated:* matches 1 element# matches 0 or more elements

“my_exchange”type = topic

Advanced != Complicated

X“my_queue”

“your_queue”

bk = “a.*.c”

bk = “#.e”

rk = Routing Keybk = Binding Key

EDCBrk = “a.b”

rk = “a.b.c”

rk = “a.e”rk = “a.b.e”

X

Page 41: Syer amqp-s2gx-2011

AMQP 101

Binding keys are period(.)-separated:* matches 1 element# matches 0 or more elements

“my_exchange”type = topic

Advanced != Complicated

X“my_queue”

“your_queue”

bk = “a.*.c”

bk = “#.e”

rk = Routing Keybk = Binding Key

EDCrk = “a.b.c”

rk = “a.e”rk = “a.b.e”

Page 42: Syer amqp-s2gx-2011

AMQP 101

Binding keys are period(.)-separated:* matches 1 element# matches 0 or more elements

“my_exchange”type = topic

Advanced != Complicated

X“my_queue”

“your_queue”

bk = “a.*.c”

bk = “#.e”

rk = Routing Keybk = Binding Key

EDrk = “a.e”

rk = “a.b.e”

C

Page 43: Syer amqp-s2gx-2011

AMQP 101

Binding keys are period(.)-separated:* matches 1 element# matches 0 or more elements

“my_exchange”type = topic

Advanced != Complicated

X“my_queue”

“your_queue”

bk = “a.*.c”

bk = “#.e”

rk = Routing Keybk = Binding Key

Erk = “a.b.e”

C

D

Page 44: Syer amqp-s2gx-2011

AMQP 101

Binding keys are period(.)-separated:* matches 1 element# matches 0 or more elements

“my_exchange”type = topic

Advanced != Complicated

X“my_queue”

“your_queue”

bk = “a.*.c”

bk = “#.e”

rk = Routing Keybk = Binding Key

C

DE

Page 45: Syer amqp-s2gx-2011

Other Stuff the AMQP Broker Does

Yes, all the standard things

● Errors can be raised for messages that do not get routed to any queues

● Messages can be consumed so that the broker does not forget about the message until the client explicitly acknowledges the message

● Messages can be published with a property indicating whether the message should be written to disk

● Transactions: making publication and acknowledgement of several messages atomic

● Flow control: e.g. used to stop publishers from overwhelming the broker in extreme situations

Page 46: Syer amqp-s2gx-2011

Conclusion: Why AMQP?

An Open Protocol has some advantages...

● Makes it easier to have multiple implementations that interoperate at the wire-level

● Avoids vendor lock-in: easy to rip out and replace individual components with alternative implementations

● Allows third-parties to write client libraries for other languages

● Decouples flag-day upgrades for both client libraries and broker

● Allows third-party traffic analysis tools to inspect and decode interactions between the clients and brokers

● Promotes similarities in APIs presented by different client libraries

Page 47: Syer amqp-s2gx-2011

Introducing RabbitMQ

RabbitMQ, AMQP and Messaging

● RabbitMQ consists of the broker (server) and several clients

● (Java, .Net, plus others)

● They speak the AMQP protocol to each other

● Anyone can write (and many have) other clients which also speak AMQP and work with RabbitMQ

● RabbitMQ is freely available and open source, licensed under the Mozilla Public License v1.1

● It’s already in many popular Linux distributions (Ubuntu, Debian, Fedora, Gentoo) and can be easily installed on OS X both through MacPorts and Homebrew

Page 48: Syer amqp-s2gx-2011

AMQP and JMS

How does AMQP compare to JMS?When would I use one or the other?

Page 49: Syer amqp-s2gx-2011

AMQP and JMS

AMQP JMSProtocol API

No language Java

Industry committee, mainly finance JEE

Interoperability from client libraries Interoperability from proprietary features

Resources can be managed by clients Resources often centrally managed

Not ubiquitous in enterprise Ubiquitous through JEE

Flexible usage patterns, emphasise routing

Two basic producer patterns (Queue and Topic)

Designed for large numbers of queues and exchanges

Hard to manage large numbers of Destinations

Page 50: Syer amqp-s2gx-2011

AMQP and JMS

JMS• standard in the Java space

• lots of open source options

• provided with Java EE application servers

• Spring JMS

Java producers

C

C

C

P

P

queue

Java consumers

queue

Message broker

Page 51: Syer amqp-s2gx-2011

@Component public class MessageSender { @Autowired private volatile JmsTemplate jmsTemplate; public void send(String message) { this.jmsTemplate.convertAndSend("example.queue", message); }}

Sending JMS Messages• Inject an instance of Spring's JmsTemplate.

• Provide the JMS ConnectionFactory in the JmsTemplate bean definition.

@Bean public ConnnectionFactory cf (){ return new CachingConnectionFactory(...); }@Bean public JmsTemplate template(){ return new JmsTemplate(this.cf()) ;}

JMS

Page 52: Syer amqp-s2gx-2011

AMQP and JMS

● AMQP• real standard

• a product of the the companies with the most mission critical requirements

• Spring AMQPAMQP producers

C

C

C

P queue

AMQP consumers

queue

Message broker

X

P X

exchanges

Page 53: Syer amqp-s2gx-2011

Spring AMQP

● Java and .NET

● AMQP core abstraction plus RabbitMQ implementation (built on rabbit client libraries)

● Higher level patterns for clients: admin, producers and consumers● ConnectionFactory – extra abstraction on top of Rabbit client (e.g. for re-

connection)

● RabbitAdmin – declare exchanges, queues, bindings

● RabbitTemplate – convenience methods for send and receive

● MessageListenerContainer – POJO message handler, asynchronous

● Spring Integration support

http://www.springsource.org/spring-amqp

Page 54: Syer amqp-s2gx-2011

Spring AMQP: RabbitTemplate

public class MyComponent { private RabbitTemplate rabbitTemplate;

public MyComponent(ConnectionFactory connectionFactory) { this.rabbitTemplate = new RabbitTemplate(connectionFactory); } public void read() throws Exception { ... String value = rabbitTemplate.receiveAndConvert("myQueueName"); ... }

}

public class MyComponent { private RabbitTemplate rabbitTemplate;

public MyComponent(ConnectionFactory connectionFactory) { this.rabbitTemplate = new RabbitTemplate(connectionFactory); } public void read() throws Exception { ... String value = rabbitTemplate.receiveAndConvert("myQueueName"); ... }

} Convenience methods

Page 55: Syer amqp-s2gx-2011

Spring AMQP: SimpleMessageListenerContainer

● Asynchronous message receiver● POJO handlers● Handles re-connection and listener failure (rollback, redelivery)● Message conversion and error handling strategies

<listener-container connection-factory="connectionFactory"> <listener ref="handler" method="handle" queue-names="my.queue"></listener-container>

<listener-container connection-factory="connectionFactory"> <listener ref="handler" method="handle" queue-names="my.queue"></listener-container>

Page 56: Syer amqp-s2gx-2011

Q&A


Recommended