Idea Transcript
Chanaka Fernando Follow Engineer, Philanthropist, Sportsman, Writer, Believe humanity Feb 25, 2017 · 6 min read
How Ballerina is different from other programming languages
In this post, we’re going to talk about special features of the Ballerina language which are unique to itself. These features are specifically designed to address the requirements of the technology domain we are targeting with this new language.
XML , JSON and datatable are native data types Communication is all about messages and data. XML and JSON are the most common and heavily used data types in any kind of integration eco system. In addition to those 2 types, interaction with databases (SQL, NoSQL) is the other most common use case. We have covered all 3 scenarios with native data types. You can define xml and json data types inline and manipulate them easily with utility methods in jsons and messages packages.
json j = `{"company":{"name":"wso2", "country":"USA"}}`; messages:setJsonPayload(m, j);
With the above 2 lines, you can define your own json message and replace the current message with your message. You can do the same thing for XML messages as well. If you need to extract some data from a message which is of type application/json, you can easily do that with following lines of code.
json newJson = jsons:getJson(messages:getJsonPayload(m), "$.company");
The above code will set the following json message to the
newJson
variable. {"name":"wso2","country":"USA"}
Another cool feature of this inline representation is the variable access within these template expressions. You can access any variable when you define your XML/JSON message like below.
string name = "WSO2"; xml x = `{$name}`;
The above 2 lines create an xml message with following data in it.
WSO2
You can do the same thing for JSON messages in a similar fashion. Datatable is a representation of a pointer to a result set returned from a database query. It works in a streaming manner. The data will be consumed as it is used in the program. Here is a sample code for reading data within a ballerina program using the datatable type.
string s; datatable dt = sql:ClientConnector.select(testDB, "SELECT int_type, long_type, float_type, double_type, boolean_type, string_type from DataTable LIMIT 1",parameters); while (datatables:next(dt)) { s = datatables:getString(dt, "string_type"); // do something with s }
You can find the complete set of functions in Ballerina API documentation.
Parallel processing is as easy as it can get The term “parallel processing” scares even experienced programmers. But with Ballerina, you can do parallel processing as you do any other action. The main concept of term “Ballerina” stems from the concept of a ballet dance where so many different ballet dancers synchronized with each other during the dance act by sending messages between each other. The technical term for this process is called “Choreography”. Ballerina (language) brings this concept into a more programmer friendly concept with following 2 features.
Parallel processing with worker The concept of a worker is that, it is an execution flow. The execution will be carried by the “Default Worker”. If the Ballerina programmer wants to delegate his work to another “Worker” which is working in parallel to the “Default Worker”, he can create a worker and send a message to that worker with the following syntax.
worker friend(message m) { //Do some work here reply m'; } msg -> friend; //Do my own work replyMsg