• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
The Future of Messaging: RabbitMQ and AMQP
 

The Future of Messaging: RabbitMQ and AMQP

on

  • 17,048 views

 

Statistics

Views

Total Views
17,048
Views on SlideShare
15,132
Embed Views
1,916

Actions

Likes
14
Downloads
351
Comments
0

23 Embeds 1,916

http://www.scoop.it 1144
http://jandiandme.blogspot.com 387
http://ewolff.com 84
http://www.lifeyun.com 72
http://feeds2.feedburner.com 69
http://innobead.blogspot.com 56
http://feeds.feedburner.com 37
http://jandiandme.blogspot.de 28
https://si0.twimg.com 6
http://blog.ultrainno.com 5
url_unknown 5
http://jandiandme.blogspot.fr 4
http://www.slideshare.net 3
http://jandiandme.blogspot.in 3
http://jandiandme.blogspot.co.at 2
http://jandiandme.blogspot.nl 2
http://www.ewolff.com 2
http://webcache.googleusercontent.com 2
http://jandiandme.blogspot.com.es 1
https://twimg0-a.akamaihd.net 1
http://www.newsblur.com 1
http://jandiandme.blogspot.it 1
https://www.google.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    The Future of Messaging: RabbitMQ and AMQP The Future of Messaging: RabbitMQ and AMQP Presentation Transcript

    • The Future of Messaging: RabbitMQ and AMQP Eberhard WolffArchitecture & Technology Manager adesso AG, Germany
    • About me•  Eberhard Wolff•  Architecture & Technology Manager at adesso•  adesso is a leading IT consultancy in Germany•  Speaker•  Author (i.e. first German Spring book)•  Blog: http://ewolff.com•  Twitter: @ewolff•  eberhard.wolff@adesso.de
    • Overview•  Why Messaging, AMQP and RabbitMQ•  Basic AMQP•  Advanced AMQP•  Advanced Spring-AMQP
    • RPC•  Predominant approach –  RMI, SOAP Web Services, CORBA, HttpInvoker, Burlap, Hessian•  Calls remote methods with parameter•  …and waits for response•  Problems: –  Explicitly tells the server what to do i.e. tight coupling –  What about network failures? –  What about long latencies?
    • Why Messaging?•  Decoupling –  Data, no action i.e. receiver Component can react arbitrarily –  Asynchronous i.e. decoupled by time•  Reliable –  Message can be stored-and- Component forwarded Messages –  Redelivery until message processed•  Solves typical problems of distributed systems
    • Why Messaging?•  But: Requires different architecture•  Very different from calling remote methods•  Asynchronous•  AJAX has the same model•  See for example “Patterns of Enterprise Integration”
    • Why AMQP?•  Open standard protocol•  Standard wire protocol•  i.e. just one client library – no matter which implementation you are using•  Less vendor lock in•  Efficient –  Binary wire protocol•  Support in all major languages•  Supported on most OS platforms
    • What about JMS?•  JMS has been the default for Java messaging system for 10+ years•  But: •  Only standardized on the API level •  Less flexible than AMQP•  Mapping AMQP/JMS is being defined•  There is git://github.com/pieterh/openamq-jms.git
    • Why Rabbit?•  Because it has a kewl name•  Numerous protocols supported•  Most popular choice on EC2•  Foundation for demanding systems e.g. NASA’s cloud initiative Nebula•  Implemented in Erlang•  Clustering built in•  Currently in 2.4.1•  Supports AMQP 0.8, 0.9, 0.9.1
    • Broad Support in RabbitMQ
    • Broad Support in the JVM Space•  Grails Plug In•  Java Client•  Scala / Lift support•  We will discuss Spring support in detail•  Spring AMQP project 1.0.0.RC1•  http://www.springsource.org/spring-amqp
    • Why Erlang?•  Originally designed for telephone switches by Ericsson•  Much easier to develop scalable and fault tolerant systems (by factors)•  See Motorola presentation: http://www.slideshare.net/Arbow/comparing- cpp-and-erlang-for-motorola-telecoms- software•  Good tool for reliable and scalable systems
    • Erlang‘s Model MonitorLink to monitor,restart Light Light Light weight Messages weight Messages weight process process process with with with state state state
    • Why Erlang?•  Let it crash –  If a process fails, it can be easily restarted –  Different approach to fault tolerance –  Otherwise lots of error handling•  Message Passing in the Core –  RabbitMQ is a messaging system…•  Light-weight process model –  Scalabiliy
    • Very Basic AMQP•  Queues: Store messages•  Queues might be –  Durable: Survive server restarts –  Exclusive: For one connection –  autoDelete: Deleted if connection closes•  Queue usually created by consumer•  All resources are dynamic•  Producer sends a message to a Queue•  Let’s see some code…
    • Spring’s RabbitTemplate•  Send & receive message•  AmqpTemplate: Generic AMQP interface•  RabbitOperations: Rabbit specific interface: (adds just a callback)•  RabbitTemplate: Implementation•  Spring might provide support for other AMQP implementations later•  Common interface
    • Spring’s MessageConverter•  Messages are binary data•  RabbitTemplate uses MessageConverter to convert between objects and messages•  Can also send binary data if preferred•  Default: SimpleMessageConverter –  byte[] directly transferred –  String converted with configurable encoding –  Serializable are serialized –  Content type set accordingly•  JsonMessageConverter converts from / to JSON using Jackson•  MarshallingMessageConverter converts from / to XML using Springs OXM mapping
    • Spring‘s AdminTemplate•  Main purpose: Configure the AMQP infrastructure•  E.g. create queues•  AmpqAdmin: Generic AMQP interface•  RabbitAdmin: Rabbit specific
    • CodeConnectionFactory conFactory = new SingleConnectionFactory("localhost"); RabbitTemplate template = new RabbitTemplate(conFactory); RabbitAdmin admin = new RabbitAdmin(conFactory); admin.declareQueue(new Queue("myQueue")); template.convertAndSend("myQueue", "Hi AMQP!"); String receive = (String) template.receiveAndConvert("myQueue"); Assert.assertEquals("Hi AMQP!", receive);
    • Basics of AMQP•  Sending messages directly to queues is not enough•  What about e.g. pub / sub?•  Exchange: Route messages (stateless)•  Messages are byte-streams•  Example used the default exchange•  More dynamic, flexible and cleaner than JMS
    • AMQP  in  a  nutshell  Exchange routes messageStatelessUsually created by producerNo queue: Message discarded X Binding binds an Exchange to a Queue Queues buffer messages Usually created by consumer
    • AMQP  in  a  nutshell  Producer and Consumer might be written in Java, C#,Python, Ruby … C P X C AMQP RabbitMQ AMQP protocol protocol
    • Exchange: Route Messages X•  The type of Exchange defined the routing algorithm used•  Binding provides selector for routing•  Exchange is addressed by name•  Some standard types•  Can provide additional ones
    • Fanout Exchange X•  Broadcast to all bound queues•  Fast•  Simple•  amq.fanout is mandatory•  To broadcast information
    • Fanout Exchange X CP X C Fanout C
    • Queue fanoutQueue = new Queue("fanoutQueue"); admin.declareQueue(fanoutQueue); FanoutExchange fanoutExchange= new FanoutExchange("myFanout"); admin.declareExchange(fanoutExchange); admin.declareBinding( BindingBuilder.from(fanoutQueue). to(fanoutExchange)); template.setExchange("myFanout"); template.convertAndSend("Hi Fanout!"); String receive = (String) template.receiveAndConvert("fanoutQueue"); Assert.assertEquals("Hi Fanout!", receive);
    • Direct Exchange X•  Routing based on one routing key•  amq.direct and the default Exchange (no name) always exist•  To send work orders to a specific worker
    • Direct Exchangenormalexpress Direct Exchange C express P X C normal C
    • Queue directQueue = new Queue("direct"); admin.declareQueue(directQueue); admin.declareBinding(BindingBuilder .from(directQueue) .to(new DirectExchange("amq.direct")) .with("helloKey")); template.setExchange("amq.direct"); template.convertAndSend("amq.direct","dropMe", "I will be dropped!"); template.convertAndSend("amq.direct","helloKey", "Hi Direct!"); Assert.assertEquals("Hi Direct!", template.receiveAndConvert("direct")); Assert.assertNull( template.receiveAndConvert("direct"));
    • Topic Exchange X•  Routing based on routing pattern•  amq.topic is mandatory•  E.g. for public / subscribe scenarios
    • Topic Exchange   order.DEinvoice.USD Topic Exchange order.* P X C invoice.* C
    • Headers Exchange X•  Routing based on one or more headers and an expression•  amqp.match is mandatory•  Complex routing roles
    • Other Features•  Message can be persistent•  Request / response using correlations possible•  Redelivery / acknowledgement possible•  Clustering with e.g. Linux HA possible•  ...or send message through multiple channels and drop duplicates
    • More aboutRabbitMQ and Spring
    • Configuring Rabbit Resources with Spring•  Spring enables decoupling of your application code from the underlying infrastructure•  The container provides the resources•  The application is simply coded against the API
    • Configuring a ConnectionFactory<bean id="connectionFactory" class="org.sfw.amqp.rabbit.connection.SingleConnectionFactory"> <property name="username" value="guest"/> <property name="password" value="guest"/> <constructor-arg value="localhost" /></bean>•  Can easily modify configuration options
    • Defining a RabbitTemplate Bean •  Provide a reference to the ConnectionFactory •  Optionally provide other references –  MessageConverter –  Routing key and exchange to be used if none is specified<bean id="rabbitTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate"> <constructor-arg ref="connectionFactory" /> <property name="routingKey" value=”invoice.USD" /></bean>
    • The MessageListener•  So far: Calling receive() on RabbitTemplate•  Needed: Something that is called when a new message appears•  The API defines this interface for asynchronous reception of messages public void onMessage(Message) { // handle the message }
    • Spring’s MessageListener Container•  Spring provides lightweight containers to call MessageListeners•  SimpleMessageListenerContainer•  Advanced scheduling and endpoint management options available•  i.e. thread pools, concurrent consumers, transaction handling
    • Defining a Message Listener Container<bean class="org.sfw.amqp.rabbit.listener.SimpleMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="queueNames" value="my.amqp.queue" /> <property name="messageListener" ref="messageListener" /></bean>•  Every time a new message appears on my.amqp.queue the messageListener is called
    • Springs message-driven objects•  MessageListener means the receiver depends on Spring API•  Why not just a POJO?•  MessageListenerAdapter takes a POJO and makes it a MessageListener•  i.e. calls consume on Bean consumer<bean id="messageListenerAdapter" class="org.sfw.amqp.rabbit.listener.adapter.MessageListenerAdapter"> <property name="delegate" ref="consumer" /> <property name="defaultListenerMethod" value="consume" /> <property name="messageConverter" ref="jsonMessageConverter" /></bean>
    • Easier Using Namespaces<rabbit:listener-container connection-factory="connectionFactory“ message-converter="jsonMessageConverter"> <rabbit:listener ref="consumer" method="consume" queue-names="my.amqp.queue2" /></rabbit:listener-container>•  Results in the same Spring Beans
    • Consumer code@Component public class Consumer { public String consume(String message) { return …; } }•  No dependency on AMQP!•  But: What about the result of the method?•  Send to the Reply-To address given in message properties with same correlationId as original method
    • Client CodeString response = (String) rabbitOperations.convertSendAndReceive( "my.fanout", "", "test");•  Message sent to destination with routing key•  Reply-To set to exclusive, autodelete, non- durable queue•  Response received through Reply-To converted and returned•  Easy request-response!•  Beware of potential latency
    • Create Environment Using Namespaces<rabbit:fanout-exchange name="my.fanout2"> <rabbit:bindings> <rabbit:binding queue="my.amqp.queue2" /> </rabbit:bindings></rabbit:fanout-exchange><rabbit:queue name="my.amqp.queue2" />•  ...if you don‘t like API calls
    • Conclusion: AMQP•  Ubiquitous Messaging•  AMQP: Protocol standard•  Better scalability•  Dynamic resources
    • Conclusion: Spring AMQP•  Easy to use•  Flexible (e.g. message encoding)•  Allows scalable message handling•  Full support for AMQP and RabbitMQ
    • More•  http://springsource.org/spring-amqp•  Also a .NET version available•  …and support Spring Integration•  http://blog.springsource.com/2011/04/01/ routing-topologies-for-performance-and- scalability-with-rabbitm•  Transaction support•  …and there is very similar JMS support 