How to initiate and run multiple Ballerina services synchronously at run-time

Kawee Lokuge
3 min readFeb 28, 2020

Ballerina is a cloud native opensource programming language developed by WSO2. In today's post I’m going to talk about how to start multiple HTTP services synchronously at run-time in ballerina. If you do not know how to create a simple HTTP Listener using ballerina follow the below link to get a basic understanding.

In the tutorial I will be creating three ballerina services. The first service is the main service. Other two services (sub-services) are initiated and started by the main service. Final outcome is three services running in parallel. Two of them initiated and started at the run-time.

Let’s Start

Commands to create a new project and add modules are listed below .Choose a desired folder and create a new ballerina project. Inside the project add three ballerina modules.

  • mainService moudule
  • firstSubService module
  • secondSubService module
$ ballerina new Ballerina-Services-Sync
$ cd Ballerina-Services-Sync/
$ ballerina add mainService
$ ballerina add firstSubService
$ ballerina add secondSubService

Your project structure:

Now Lets code the first sub-service which will be called by the main service later. Go to the main.bal file in the firstSubService and insert the below code.

firstSubService/main.bal

In the above code a http:Listener endpoint is created that takes the port 8080. Keep in mind that we are making the listener public here so it can be accessed by the main service later. Then we create a service called SubService_One and pass the service configurations. To keep things simple I only added the basePath. Note that the listener is not bind to the service at the compile time. Next a GET method (resource function sayHelloSubService_One) is created inside the service that returns a string to the client saying Hello From firstSubService! Finally we write a getter function which exposes the service that we created earlier to the outside. Congrats!! You have successfully completed the first sub-service. Second sub-service(secondSubService) is coded exactly as the first.

secondSubService/main.bal

Now we are going to code the main service. Open the main.bal file inside the mainService module. Import the firstSubService and secondSubService modules. Then create a function called __init(). This is is an inbuilt function in ballerina that runs before a service starts. Inside the __init function you are accessing the listener object created earlier in the first/second sub-service module. Next we bind that listener object to the HTTP service using __attach method by passing the service as a parameter. Note that we are getting the service object using the getService method. After attaching the service we start the listener by calling __start method (When we start the listener the service that it is bidden to starts as well). Then we write a service;mainService inside the mainService module(Here service name and the module name is the same. Don’t get confused :) ). Finally a resource function is added. Like the previous services this function only returns a string to the client.

mainService/main.bal

Well-done!! You have reached the end. Go inside the Ballerina-Services-Sync directory and run the mainService module.

$ ballerina run mainServiceOutput:Main Service!
[ballerina/http] started HTTP/WS listener 0.0.0.0:8080
[ballerina/http] started HTTP/WS listener 0.0.0.0:9090
[ballerina/http] started HTTP/WS listener 0.0.0.0:3000

The first,second sub-services and main service starts running synchronously.

Testing the Code

Lets issue a cURL request to our services and get the responses.

$ curl -k http://localhost:8080/serviceOneBase/sayHello             Output:
Hello From firstSubService!
$ curl -k http://localhost:9090/serviceTwoBase/sayHelloOutput:
Hello From secondSubService!
$ curl -k http://localhost:3000/mainBase/sayHello Output:
Hello From main Service!

Summary

Hope you guys got a basic understanding how to initiate and run multiple ballerina services at run time in parallel. The key point is keeping the listener and the service separated in the code. Then we bind them together at the run time of the program. Once we call the main service it will bind the listeners and start the sub-services.

--

--