You are on page 1of 4

Assignment 3_2

Asynchronous Communication using Messaging

This assignment is about implementing a distributed system that uses MOM(Message


oriented middleware) to create an asynchronous communication between the client(message
producer) and the server(message consumer).
The application is supposed to be used by a DVD store administrator. The
administrator must send a notification to its customers when a new DVD is available. The
information about the new DVD must be saved in a text file. Each time new information
about a DVD is introduced in the system, the application must send automatically notification
emails to all the subscriber customers to notify them about the new item. Each time new
information about a DVD is introduced in the system the application must create
automatically a text file and write the information about the DVD in it.
For implementing this assignment I used the following technologies: RabbitMQ for
Asynchronous Messaging and Tomcat server for running the servlet.
The message passing inter-process communication paradigm is an asynchronous
communication mechanism that means that the message sending application does not have to
wait for a response from the destination process or from any other application. The
disadvantage with the request-reply communication paradigm is that the client process makes
a request for the server and it is blocked until the server replies.
Message oriented middleware is designed to facilitate the asynchronous inter-process
communication. The distributed MOM systems have three major components:
● Message Sender - process that creates and sends the message
● Message Repository - where messages are stored until delivery
● Message Receiver - process that receives the message
The sender is the client application used by the administrator to introduce data
regarding the DVDs. The Message Repository is a special application where the sender
connects to send the message. This application replies immediately to the client an
acknowledge message, so that the client can continue its execution. From this point, the
repository is responsible for sending the request to the receiver and making sure the message
arrives there. Messages are kept in a queue until they are sent. The Receiver connects to the
Message Repository using a synchronous request-reply mechanism to take the message and
starts sending emails and writing data to a text file.
I made two distinct projects in Intellij IDEA: Producer and Consumer. Producer is run
by starting the Tomcat server. A servlet starts and a web page appears in browser, having the
url: ​http://localhost:8080/CreateDVDServlet​. The administrator enters data about the new
DVD, and when ​Create ​button is pressed, the data is sent to the consumer. I will explain in
the following paragraphs what Producer project contains, the part that connects the two
project and what consumer does.

1
Producer

Producer project contains 4 packages: ​entities,​ ​serialization​, ​servlet​, and ​start​. Besides
this, information regarding the web application(html page, correspondence between webpage
and servlet class) is kept in ​webapp ​directory.
In the first package, we keep the DVD class. A dvd is described by: a title, year of
production and selling price.

For sending DVD information to another distributed application, we need


serialization. I took the ​POJOSerializer f​ rom the first assignment. This object makes the
conversion from an object to a string and vice-versa. Serialization is done by means of
reflection. Java reflection is a feature that allows a program to introspect upon itself. In this
way we can obtain information about a class without knowing the class before hand, and
serialize all its fields. Then, based also on reflection, we can reconstruct the object.
In the ​servlet p​ ackage we have ​CreateDVDServlet ​class. This servlet is actually the
user interface. It contains two methods: doPost and doGet http requests. doGet request is
called when we access the url /CreateDVDServlet. In this moment, createDVD.html page is
loaded, and a form is displayed. This form allows the user to fill in the dvd data: title, year
and price. When pressing Create, the data is sent via a channel to the consumer.
The start package has no use at all. I let it there because Tomcat server asked for a
main class.

2
RabbitMQ - ​Publish/Subscribe

In this section I will present the Message Repository that is in between producer and
consumer. This part is provided for us by RabbitMQ service. There are several ways of
creating messaging applications using RabbitMQ, but I preferred to use Publish/Subscribe
method. With this model, the producer does not send the messages directly to a queue. The
producer can only send messages to an ​exchange.​ An exchange receives on one side
messages from producers and on the other side it pushes them to queues. The exchange must
know exactly what to do with a message it receives. Exchange type allows specifying to
which queues to send the messages: to one of them, to all, etc. I used the ​fanout ​exchange
type. It just broadcasts all messages it receives to all queues it knows. In my project I
implemented two queues, for two consumers.

First, we have to create a Connection, and the a Channel. On the channel we declare
the exchange, with a given name and a type. After this declarations, we can publish the
messages on the exchange. Finally, we have to close the channel and the connection.
On the consumer side, we create too a connection and a channel. We declare the same
exchange as for the producer. We declare a queue on that channel and we bind to it. Then, we
are ready to receive messages that come on that queue. The messages will be lost if no queue
is bound to the exchange. ​handleDelivery ​method is overwritten and it is used for handling
the incoming messages.

Consumer

Consumer project has 4 packages: entities, serialization, service and start. The first
two are the same for the producer. In the service package we have two classes that provided
the needed services: FileService, MailService.
In the start package we have the consumers that connect to the queues and receive a
DVD. One consumers sends the email, and the other one writes the data in a text file.

3
Deployment diagram

Build and execution


1. Install RabbitMQ service and Tomcat server. Perform the needed configurations.
2. Open the 2 projects: Producer and Consumer.
3. Start the consumers: FileConsumer and MailConsumer in the Consumer project.
4. Start Tomcat server in the Producer project.
5. When the webpage opens in browser, introduce some DVD data and click Create.
6. See the results: emails are sent, the text file is created.

Student: Fărcașș Cristian - Teodor


Group: 30441

You might also like