Quantcast
Channel: vijayakkineni » Fanout
Viewing all articles
Browse latest Browse all 2

RabbitMQ Series – Part 2 – Concepts and HelloWorld

$
0
0

Hello All,

Welcome to part 2. The goal of this session is to introduce the basic concepts and terminology of RabbitMQ and the reader should be able to implement a hello world program when done reading this article.

Without wasting any further time lets dive into the 101 of RabbitMQ. We all know RabbitMQ is a message broker and readers familiar with JMS might find some familiarty with the elements explained here.

Producer: Producer is the entity who creates a message and publishes them to a broker.

Consumer: Consumer is the entity that subscribers itself to a queues in a broker to receive messages.

Message: Message is the actual data that gets exchanged between the parties of interest(producers and consumers). Message has two parts the payload and label. Payload is the actual data that could be of any format JSON, XML or Text document. Label carries the metadata about the message.

Connection: A connection represents a real TCP connection between the client and the broker for communication.

AMQP Channel: A Channel is a virtual connection inside an actual TCP connection. Once a TCP connection is being established between the client and the broker the client has to create a channel over which AMQP commands are being issues.

Note: Channel instances are safe for use by multiple threads. Requests into a Channel are serialized, with only one thread being able to run a command on the Channel at a time. Even so, applications should prefer using a Channel per thread instead of sharing the same Channel across multiple threads.
Reference: RabbitMQ API Guide

Exchanges: The Exchanges are the destination where publishers publish messages and exchanges takes on the responsibility of forwarding the message to the appropriate queues. Routing depends on Exchange Type and Bindings involved. The most important attributes that are used on a daily basis when creating an exchange would be name, durability, auto-delete and arguments.

Direct Exchange: A direct exchange delivers messages to queues based on the message routing key. A direct exchange is ideal for the unicast routing of messages (although they can be used for multicast routing as well).

exchange-direct

Fanout Exchange: A Fanout exchange routes all of the messages to all the Queues that are bounded to an exchange. It’s an 1-n model. An ideal model for broadcast.

exchange-fanout

Topic Exchange: Topic exchange routes messages to one or many queues based on the routing patterns through which a queue gets binded to exchange.

Header Exchange: Header exchange is the one least used exchanged as the routing is based on the message headers and the routing key is ignored. The same use case can also be implemented using Direct Exchange.

Queue:  Queues are the fundamental building blocks of RabbitMQ and the final destination for messages where messages wait to get consumed.  Queues are declared by issuing an AMQP command “queue.declare”. RabbitMQ uses round robin load balancing mechanism when there are multiple consumers attached to broker trying to consume from the same queue. Attributes that are frequently used when creating a queue are Name, Durable, Auto-Delete, Exclusive and Arguments.

Hello World:

Lets get started writing some code, enough of definitions. Fire up your favorite IDE for Java development and create a maven based project or a gradle based one. We will be using amqp java driver provided by the rabbit guys. Add the below snippet to your pom.xml

<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>3.1.3</version>
</dependency>

Lets create a producer who publishes a “Hello World” message to Greet queue.

The code for Consumer to consume messages from Greet queue

References: RabbitMQ

Stay Tuned for Part 3 Management and Vhosts. Till then enjoy this song.



Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images