Ballerina How To: Data Micro Services

Edgar A Silva
5 min readMar 22, 2017

Ballerina language is a programming language focused for integration , which can make your life far easier. You might ask why? Well, one of the reasons is that in the last about two decades, we have been representing some of our integrating tasks using Sequence Diagrams, for transmitting and sharing our ideas, and then transferring our thoughts to some integration tool.

One of the good things in Ballerina, is that the diagram that you use to define your ideas, can now be used to execute them too. In this example, we will go a little further than a Hello-World, and we will show how to build a Data Micro-service.

Understanding the Classpaths

After you unzip distribution zip of Ballerina-Lang, keep in mind that we will refer to that extracted folder as <BALLERINA_HOME>.

Ballerina took some inspirations from several languages, and one of those is Java, therefore, if you need to refer to some library/jar , in our case, a JDBC driver library, we will drop that jar in the <BALLERINA_HOME>/bre folder, like the image1:

The MySQL Driver dropped in the <ballerina-home>/bre/lib — Similar to what we did with JRE in the past

The BRE (ballerina run-time environment) will load these jars, and make them available for your integration needs.

Starting the Composer

Well, IMHO Ballerina composer is a great tool, so let’s use it. In order to start it, please, just go to <BALLERINA_HOME>/bin and type sh composer for unix based systems or composer.bat for windows, and then you will see the following output (refer image 2) :

Starting the Composer on Unix System (for Windows type composer.bat)

Now, let’s move on through simple steps.

Step 1: Open the Composer tool in your browser, using the following URL: http://localhost:9091 .

Step 2: Please, click on New Button, it will generate an untitled file, which is fine for now. On the bottom at your left corner, you will see a “Source View” button option, that will open the “text editor” for you. Now, don’t be afraid, we are not living in the past rendering, parsing whatever anymore, you have a full language at your service! So, first of all , just copy the skeleton of your Data Micro service:

Now, press ctrl+s or just Save in Menu Option, and save your file with name: employee.bal , and click on Design View Button again, you will see something like this: (see image 3)

The Composer rendering your Employee Service

Putting Cheese on top of the Pizza

Defining the Database Connector
We must add a shared component for all the resources in our Ballerina Service, that will be a Database Connection. For that purpose we will have a connector, which is defined like this right after the Service declaration and at same scope/level of our resource methods:

Done! Your database Connector is ready. For those who are familiar with Delphi, Visual Basic and similar languages, this might remind you the power to define a resource once and reuse across your UIs! Well, we can do that with same productivity now with our Services, but following the same reuse principles.

A connector must be useful for many purposes, and can be reused many many times. I believe, that in the near feature we will be able to see a huge and collaborative marketplace of connectors available for Ballerina (some are already available now). Take a look at other Ballerina-lang connectors here.

Defining the simple Select SQL

For our resource method represented by the name select and path “/“ (e.g.: http://server/api-context/employee/) , we will add our very simple SQL implementation.

A connector must be useful for many purposes, and can be reused many many times. I believe, that in the near feature we will be able to see a huge and collaborative marketplace of connectors available for Ballerina (some are already available now). Take a look at other Ballerina-Lang connectors here.

See some ballerina helpers and facilitators in action, see the following code for our select resource :

What we can notice in that piece of code:

datatable : It is a component that encapsulates the return of a SQL execution. To do that, this component needs as their parameters a DB Connector, the SQL that you want execute and a map of potential params that you can use! Well, that is awesome! I personally always prefer to handle the SQL myself, and there are very popular ORM tools nowadays that has got good notoriety simply because they force this approach.

json: JAXB, “Jacksons five”, “Jetsons”, “Jyraia”, “GSON”? NO, you don’t need any hero to simply marshal your JSON result. Ballerinacomes with a simple toJSON from data tables passing our previously rendered datatable. Yeah, forget about annotate beans, and know that beans are good when you can eat them.

message : That is your “message back”

reply: that is when you tell to JSON response….”run forest, run forest….” and then your integration is ready.

Exception Handling

One of the most important things in distributed computing, is to tell others what happened at your end. Who got happy and saw this following message “OLE32.dll: CATASTROPHIC FAILURE”. For this reason, in our service, we create a function, in order to let our consumers understand better what happened in our service, see the following source:

Conclusion

Ballerina is a very powerful language and also provides a set of solid features to enable the new demands from integration’s needs. You can easily create robust services, package and distribute them using Docker, and I am sure that there is a lot of good things about to come, keep tuned in to Ballerina News.

Find below the full employee service using jdbc, mysql and a very versatile way to create your new micro/services!

See the services in execution mode:

The Service in Execution

Here the result invoking the select resource via employee/:

Result from our Data Micro Service

--

--

Edgar A Silva

Just a dad, a husband, and an Old-School Brazilian Jiu-Jitsu Black-belt. Working in the technology field since 1997.