JMS with Ballerina lang: Part 5

Building an online food-ordering platform using JMS

Ayesh Almeida
3 min readMay 21, 2024

(This article is written using Ballerina Swan Lake 2201.9.0)

In this article, we will discuss how to build a real-world application using the Ballerina JMS connector.

In our use case we will focus on an online food ordering platform designed to streamline the process of placing and processing food orders. The system comprises three main components: Order Service, Order Processor, and Menu Service.

Menu service
— Manages the menu of food items available for ordering.
— Provides API endpoints for retrieving the entire menu and details for specific items.

Order service
— Provides REST API capabilities for customers to place orders, view order status, and complete payments.
— Manages the overall order lifecycle, from order placement to completion or cancellation.

Order processor
— Backend service responsible for processing order information.
— Calculates the total price for orders and estimates the completion time.
— Communicates with the Order Service using JMS queues to exchange order details and confirmations.

The system adopts a microservices architecture, with each component handling specific functionalities. Communication between services is facilitated by Java Message Service (JMS), ensuring reliable and asynchronous messaging.

Following diagram depicts the high-level architecture of the system.

Menu service

Menu service offers a simple REST API to retrieve the current menu items and the details for a specific menu item. Following API contract describes the basic functionalities offered by the Menu service.

Order service

Order service offers a REST API for customers to place orders, view order status, and make payments for the orders. Following API contract describes the basic functionalities of the Order service REST API.

For the Order Service database, we will use an in-memory datastore along with the Ballerina persistence package. Since our main focus is on JMS-based integration, we will not cover the details of the Ballerina persistence package in this article. For more information on using the Ballerina persistence package, please refer to this article.

Order processor

The Order processor is a back-end service that calculates the total price and estimated completion time for an order. It does not provide any REST API which can be used by the customers.

JMS integration

The communication between Order service and Order processor happens via an ActiveMQ broker using several JMS topics and queues. Following diagram depicts the topics and queues used by individual components.

  • When there is a new order, the Order service will publish a message to Orders queue with the order details. The Order processor which listen to the Orders queue will receive the new order details and calculates the total cost and the estimated completion time for the order.
  • Once, the Order processor completes the calculation it will publish a message to Order confirmation queue. Order service will be listening to the Order confirmation queue and mark the order status as PAYMENT_PENDING.
  • Once an order is in PAYMENT_PENDING state the customer can proceed with the payment. Once the customer completes the payment for the order, the Order service will publish a message to Order status update topic. The Order processor which listens to the Order status update topic will pick the payment completed order and will start processing the order.
  • When the Order processor completes processing an order it will publish a message to Order status update topic notifying that the order processing is complete. The Order service, which listens to the Order status update topic will then update the order status to COMPLETED.

In this article we discussed on how to implement an online food-ordering system using JMS. The complete code sample for the system could be found here.

--

--

Ayesh Almeida

A Tech Enthusiast who wants to build simple solutions for complex problems.