Background Processes using RabbitMQ

Author - Kritika Sood
2.08.2017
|
0 Comments
|

Whenever we need to execute a block of code which may take long time to run then instead of executing them in real time we can put this code in the background process as it may timeout if we wait for its completion in real time.

What is RabbitMQ?

RabbitMQ is a software used for asynchronous code execution (messaging). This software facilitates creation of queues that receive messages and messages get consumed by message handlers. Message here simply means asking to invoke a process and passing arguments to it. Process is the message handler which consumes the message, which means it picks those arguments and processes them as required. For example a process that sends email may receive arguments in the message for “To”, “CC”, “Subject”, “Body” etc and this email process will use these arguments to send email asynchronously.

RabbitMQ is lightweight and easy to deploy on local and on the server. It can be used with any technology like java, ruby, python, php etc. It stores the messages until a receiving application connects and takes a message from the queue.The receiving application then processes the message.

1

In RabbitMQ we have 3 components:

1. Producer : Producer is the code written by developer. It actually generates or produces the requests or messages.

2. Consumer: Consumer is also the code written by developer. It actually consumes or processes the messages from queue that are defined by RabbitMQ.

3. Broker: This is the part that is responsible for queue generation. It takes the messages from producer, appends them in queue and transmits the message to consumer.

2

We can use cloudamqp or amqplib php libraries to write producer and consumer in a php project.

Example:

Suppose we have an application where users purchases a course from an E-learning site. Site will enroll the user in that course and course confirmation email gets sent back to the user. If there are large number of users who purchase the course at the same time it can get very slow/ So enrolling the user and sending email can be implemented as asynchronous process using RabbitMQ instead in real time web application.

We will implement this by creating two process/methods i.e one is Publisher and other is Consumer. Publisher is a producer which will produce course enrollment requests. We will publish this request to the queue for which we will define suitable exchange. Defined exchange will append the message in appropriate queue. Consumer will fetch the message from queue and will process it.

Installation
Setup
Implementation

(….to be continued in next post)

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *

Send your message