Getting started with Ballerina

Balakrishnan Sathiyakugan
4 min readJul 22, 2018

Ballerina is a compiled, type-safe, concurrent programming language targeting micro service development and integration.

It is an open source project initiated in 2015 by the architects of WSO2 as code-based alternative to the configuration-based integration tools such as EAI, ESB, and workflow products.

The Ballerina has various constructs geared toward cloud-native development, including support for modern data formats and protocols, reliability, distributed transactions, APIs, and event streams.

Setting up Ballerina in IntelliJ IDEA

Download the Ballerina distribution below. You can download it for either Mac, Windows or Linux. Installation details are specified clearly.

DOWNLOAD THE BALLERINA DISTRIBUTION

Installing Ballerina plugin
The first thing to be done is install Ballerina plugin for IntelliJ IDEA. It is essential to have IDEA version 2016.3 or above to use the plugin. This plugin supports both IDEA Community and Ultimate versions.

Follow this link to get the full installation guide

Quick Tour gives you a better understanding of how ballerina can be used to integrate with services.

First build a simple Service Hello World Service to get understand with how Ballerina can be used to provide services. Simply if you are familiar with NodeJS basics, it gives the better understandings of how the request and the responses are handled. I have given the familiar NodeJS code, before we move further

The following code tells the computer to write “Hello World!” if anyone (e.g. A web browser) tries to access your computer on port 8080.

var http = require('http');http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!');
}).listen(8080);

Output will be

Hello World!

Let’s move on to Ballerina Code

import ballerina/http;
import ballerina/log;

//By default, Ballerina exposes a service via HTTP/1.1.
service <http:Service> hello bind { port: 9090 } {
//Invoke all resources with arguments of server connector and request.
sayHello(endpoint caller, http:Request req) {
http:Response res = new;

//Use a util method to set a string payload as String This will be Given as output
//if you want json pay load use "res.setJsonPayload();"
res.setPayload("Hello, World!");

//Send the response back to the caller.
//If there are any error Error will be responded
caller->respond(res) but {
error e => log:printError(
"Error sending response", err = e)
};
}
}

To run the service

You will find the lines as this

ballerina: initiating service(s) in 'ballerina.bal'
ballerina: started HTTP/WS endpoint 0.0.0.0:9090

Use the curl command to invoke the service

 curl http://localhost:9090/hello/sayHello

You will find Hello, World in the terminal

Hello, World!

In order to invoke the services you may need to install curl, for Ubuntu users use this command to install -> sudo apt-get install curl

What is the endpoint?

Come on guys :) We could explain it in simpler way, by these examples:

/this-is-an-endpoint
/hello/endpoint
/some/other/endpoint
/login
/logout
/cart/items

And when put under a domain, it would look like:

https://yourdomain.com/this-is-an-endpoint
https://yourdomain.com/hello/endpoint
https://yourdomain.com/some/other/endpoint
https://yourdomain.com/login
https://yourdomain.com/logout
https://yourdomain.com/cart/items

Can be either http or https, we use https in the example.

Also endpoint can be different for different HTTP methods, for example:

GET /item/{id}
PUT /item/{id}

would be two different endpoints — one for retrieving (as in “cRud” abbreviation), and the other for updating (as in “crUd”)
And that’s all, really that simple.

Reminder:

The Quick Tour is the fastest way to try Ballerina. Take this dancer for a twirl and a spin!

Most of the implementation in Ballerina is just like Java so no need to worry about the syntax as much. In case you find any difficulties, visit this link, that will teach you the Ballerina language incrementally with commented examples that cover every nuance of the syntax with examples

Ballerina by the Guide are long form examples that showcase how to build different types of integrations using a complete development life cycle, including IDE configuration, packages, dependencies, coding, unit testing, deployment, and observability.

To get some libraries to connect to Twitter, Gmail, GitHub from Ballerina and access it easily you can use BallerinaCentral

Finally, it’s just an overview of how to install and how to use ballerina and the guidance to get more out of ballerina for more information comment below If you like it share it with your friends. I will come up with a Ballerina code that will fetch the tweets and send it to the subscribers through email and direct message.

--

--