Ballerina How To: Data Micro Services

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

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

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

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

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

Conclusion

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

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store