Integration with Ballerina — II

Use-case-1 — Working with Ballerina Message Broker

Saad Sahibjan
4 min readJun 22, 2018

In the first part of this article series, I explained the scenario from high level to low level. For recalling, the high level diagram of the entire scenario is as below.

High Level Diagram of Event Manager Scenario

In this article, we will be working with Ballerina services and Message Broker. In simple once the user submits the invitation the message will be sent to a queue and message consumer who will be listening to the queue will consume messages which comes to the queue. The diagram of use-case-1 is as below.

Use-case-1 — Working with Ballerina Message Broker

According to the diagram there should be two services, one acting as a message producer and the other as a message consumer and a queue to store the messages given by the producer and to forward the messages to the consumer.

The project structure will look like below,

└───event-manager
├───invitation_acknowledge
│ invitation_acknowledge_service.bal

└───seat_reservation
seat_reservation_service.bal

The invitation_acknowledge_service will have a SimpleQueueSender endpoint with its connection details which will create a queue with the provided details.

endpoint mb:SimpleQueueSender invitationAcknowledgeQueueSender {
host: "localhost",
port: 5672,
queueName: "InvitationAcknowledgeQueue"
};

Then the service will have a resource to handle the request. This will be a POST request passed with the payload in the request body.

Method: 
POST
Resource Path:
/invitation/acknowledge
Request Body:
{
"type": "new",
"company": "Mitra Innovation",
"name": "John Wick",
"email": "jwick@email.com",
"response": "yes"
}
Response:
Request has been submitted, you will be notified via email

The invitation_acknowledge_service with its queue connection details and the resource to handle the user request will be as below,

Once the message is sent to the queue, there should be message consumer who should be listening to the queue and be ready to consume a message once a message is arrived. So to act as a message consumer we have to create another ballerina service which we are going to call as the seat_reservation_service. In this service we have to create a SimpleQueueReceiver endpoint with the connection details and the correct queue name to be listened to.

endpoint mb:SimpleQueueReceiver invitationAcknowledgeQueueReceiver {
host: "localhost",
port: 5672,
queueName: "InvitationAcknowledgeQueue"
};

The ballerina service with queue listener connection details and the message consumption implementation will look as below,

Now we have completed the message producing and consuming part. Source code of this use-case-1 can be found here. To run and test this use-case-1, Run broker command to start the Ballerina Message Broker. Clone the git repository. Then traverse to the event-manager/invitation_acknowledge directory and execute the following ballerina commands,

$ ballerina init
Ballerina project initialized
$ ballerina build
$ ballerina run ./target/invitation_acknowledge_service.balx
ballerina initiating service(s) in './target/invitation_acknowledge_service.balx'
ballerina: started HTTP/WS endpoint 0.0.0.0:9090

This will create a connection to the Ballerina message broker and starts the invitation_acknowledge service on port 9090.

Then traverse to the event-manager/seat_reservation directory and execute the following command to start the seat_reservation service,

$ ballerina init
Ballerina project initialized
$ ballerina build
$ ballerina run ./target/seat_reservation_service.balx
ballerina: initiating service(s) in './target/seat_reservation_service.balx'
2018-06-15 00:08:34,979 INFO [ballerina.jms] - Message receiver created for queue InvitationAcknowledgeQueue

This will connect to the message broker and will be listening to the InvitationAcknowledgeQueue.

Now we can send a request to acknowledge service using CURL to test the use-case-1.

$ curl -v POST -H "Content-Type:application/json" -d '{ "type": "new", "company": "Mitra Innovation", "name": "John Wick", "email": "jwick@email.com", "response": "yes" }' http://127.0.0.1:9090/invitation/acknowledge
* Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 9090 (#0)
> POST /invitation/acknowledge HTTP/1.1
> Host: 127.0.0.1:9090
> Accept: */*
> Content-Type:application/json
> Content-Length: 116
>
< HTTP/1.1 200 OK
< content-type: text/plain
< content-length: 58
< server: ballerina/0.970.1
< date: Fri, 15 Jun 2018 00:26:22 +0100
<
* Connection #0 to host 127.0.0.1 left intact
Request has been submitted, you will be notified via email

According to the above CURL request, the message has been successfully sent to the queue. Now if we look the seat reservation service which is the message consumer, it should log the consumed message.

2018-06-15 00:26:22,651 INFO  [] - [INVITATION ACKNOWLEDGE QUEUE] Reservation Details: {"type":"new","company":"Mitra Innovation","name":"John Wick","email":"jwick@email.com","response":"yes"}

We can even test this use-case further to make sure we are not loosing any messages that are sent to the queue. For that stop the seat_reservation service and try sending multiple requests to invitation_acknowledge service. And then start the seat_reservation service, then you should see all the messages you sent are consumed and logged, non of the messages are lost.

Source code of use-case-1 can be found here. You can go through the code, clone, build and run. If you have any queries, comment on this blog or if found any issues log an issue.

The next article will be on Use-case-2 — Database Interaction with Ballerina on inserting or updating the consumed message in mysql database.

--

--