Ballerina - Let’s dance !!!!!

Introduction— Part -1

Dushan Abeyruwan
5 min readFeb 26, 2017

Recently concluded WSO2Con USA 2017 brought the concept which revolutionized and opened the eye of Agile Digital Transformation Through Open Source and we are being asked review the integration practices through a different angle. It’s a conjunction of integration paradigm or I should say the cross check of what we known so far; the launch of http://ballerinalang.org/. We thought the suitable phrase for the announcement as

Let’s Make Integration Great Again….

I guess, some of you may heard same verbal during 2016 in one of the presidential election !! So, as same way as in political arena, WSO2 thinks Integration require a radical change, the change that would reflect and energize the integration, in fact, we need a top to bottom shake up. So, lets start what Ballerina is all about ;

The Problem; Integrating various systems is gonna be a monotonous task the problem has never ending cycle . We used to discuss various integration issues while working with different customers, we identified that there is a gap in the integration space where programmers and architects speaks in different languages and sometimes, this cause some panic when integration become complex and it has become bottleneck when someone trying to explain the integration in detail and then, the Integration has lot to do with diagrams. Top level people always prefer diagrams than code but programmers do the other way around. As result of that WSO2 engineering thought of filling this gap with a more modernized programming language.

Ballerina is constructive programming language designed for integration. It allows you to connect apps and services to handle all types of integration scenarios, such as collecting top tweets from a specific location and adding them to a Google spreadsheet, generating emails with real-time quotes pulled from a stock quote service, transforming and routing data based on advanced logic, and much more.

I’ll be planing to compose multiple blogs related to Ballerina since there will be different section various integration problems can put on tables and can be simplified with the new integration terminology and to start with will walk through the important characteristics surrounded with Ballerina and why we think it’s a brought up the radical change to the integration.

We think Ballerina is;

  • Flexible. You can build your integrations from sequence diagrams, or write code in Ballerina or Swagger.
  • Powerful. The Ballerina language was designed from the ground up specifically for integration and can handle everything from a simple Hello World program to complex service chaining and content-based routing scenarios.
  • Beautiful. The Ballerina Composer allows you to easily draw your integration scenario and all the components that need to interact by dragging and dropping elements onto a canvas.

key concepts of Ballerina

Let’s focus some of the key concepts of Ballerina, Ballerina inherits or I should say, Ballerina has absorbed some of the key concepts from languages like Go,Python to construct program definitions.

Each Ballerina program represents a discrete unit of functionality that performs an integration task. You can create a Ballerina program as a service that runs in the Ballerina server and awaits requests over HTTP. Or you can create your program as an executable program that executes a main() function and then exits. Your Ballerina programs can be as simple or as complex as you like, but for best results, each program should focus on a specific task

Service: When defining a Ballerina program as a service instead of an executable program, the service construct acts as the top-level container that holds all the integration logic and can interact with the rest of the world. Its base path is the context part of the URL that you use when sending requests to the service.

Resource: A resource is a single request handler within a service.

Function: A function is a single operation.

Worker: A worker is a thread that executes a function.

Connector: A connector represents a participant in the integration and is used to interact with an external system or a service you’ve defined in Ballerina.

Action: An action is an operation you can execute against a connector. It represents a single interaction with a participant of the integration.

Running Hello world!!!

  • Download Ballerina 0.8 (you will find the tooling which is known as composer and the runtime, download them separately)
  • Unzip and navigate to the bin folder then start ‘./composer’
  • Composer runtime will display the URL which you should use in order to view. The default URL is ‘http://localhost:9091
  • You can play around the tooling palette, if need more information please do refer blog
  • As depicted in below images, you can point to the samples directory and playa round with them.
  • Go to Ballerina-runtime, then, navigate to bin folder.
  • Run samples e.g bin$ ./ballerina run service ../samples/echoService/echoService.bal
  • Then invoke API as follows curl -v http://localhost:9090/echo -d “Hello World” simple as that!!!!
  • If you willing to write the Ballerina sample from scratch, the please refer Write your First Program

Working with Connectors

One of the important aspect of Ballerina language is the connector representation, the connector represents a participant in the integration and is used to interact with an external system or a service you’ve defined in Ballerina. The 0.8 release comes with some interesting but very useful connectors such as;

@Source ( 
protocol = "file",
fileURI = "file:///home/user/orders",
pollingInterval = "20000",
fileSortAttribute = "size",
fileSortAscending = "false" )
service orderProcessService
{
resource processOrder (message m)
{
// file processing logic here.
}
}
jms:JMSConnector jmsEP = create jms:JMSConnector("org.wso2.andes.jndi.PropertiesFileInitialContextFactory", "jndi.properties"); 
message queueMessage = {};
map dataMap; dataMap = { "country" : "US", "currency" : "Dollar" , "states" : "50"};
map propertyMap; propertyMap = { "MapData" : dataMap}; jms:JMSConnector.send(jmsEP, "QueueConnectionFactory", "MyQueue", "queue", "MapMessage", queueMessage, propertyMap);
@Source (
protocol = "jms", destination = "ballerina", connectionFactoryJNDIName = "QpidConnectionFactory",
factoryInitial = "org.wso2.andes.jndi.PropertiesFileInitialContextFactory",
providerUrl = "jndi.properties",
connectionFactoryType = "queue",
sessionAcknowledgement = "CLIENT_ACKNOWLEDGE")
service jmsService
{
resource onMessage (message m)
{
// ProcessMessage
}
}
import ballerina.lang.messages;  
@http:BasePath ("/test")
@ws:WebSocketUpgradePath("/websocket")
service helloWorld {
@ws:OnOpen resource onOpenMessage(message m) {} @ws:OnTextMessage resource onTextMessage(message m) {} @ws:OnClose resource onCloseMessage(message m) {}
}

What else available with Ballerina? There are few other projects wrapped with the release of ballerina runtime

  • Docerina — is the API documentation generator tool of the Ballerina language
  • Testerina — is the test framework built for the Ballerina language. This will be a part of ballerina-tools-<release-version>.zip distribution.

Anyway, that’s it from for now, I would welcome everyone who read this short introduction to be a part of new journey !! suggestions are mostly welcome You can check out the documentation, examine the code on GitHub, and join ballerina dev group.

Dr.Sanjiva Announcing Ballerina

--

--