Understanding Ballerina’s Native Data Types, Parallel Processing, and Development Tools
This article introduces Ballerina’s unique language features, including native XML/JSON data types, datatable handling, inline definitions, parallel processing with workers and fork‑join, and the comprehensive development toolset such as Composer, Testerina, connectors, and editor plugins, illustrating code examples throughout.
In this article we discuss the distinctive features of the Ballerina programming language, which are designed to meet the needs of integration and data‑centric technical domains.
XML, JSON, and Datatable as Native Data Types
Communication relies on information and data. In any integration ecosystem, XML and JSON are the most common data types, and interaction with databases (SQL, NoSQL) is another frequent use case. Ballerina supports all three scenarios with native data types.
You can define XML and JSON inline and manipulate them easily using utility methods provided in the jsons and messages packages.
json j = {"company":{"name":"wso2", "country":"USA"}}; messages:setJsonPayload(m, j);
The two lines above create a JSON payload and replace the current message with it; the same approach works for XML messages.
To extract data from an application/json message you can use the following code:
json newJson = jsons:getJson(messages:getJsonPayload(m), "$.company");
This extracts the {"name":"wso2","country":"USA"} object into the newJson variable.
Inline definitions also allow variable interpolation inside XML/JSON templates:
string name = "WSO2"; xml x = ` {$name} `;
The resulting XML is <name>WSO2</name> . The same technique can be applied to JSON messages.
Datatable represents a pointer to a result set returned from a database query and works in a streaming fashion. The following example reads data from a datatable in a Ballerina program:
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 }
More functions are available in the Ballerina API documentation.
Parallel Processing Made Simple
Parallel processing, which can be intimidating, is straightforward in Ballerina. The concept is inspired by a ballet where many dancers synchronize by exchanging information, technically called “orchestration”. Ballerina introduces this through two friendly features.
Worker‑Based Parallelism
A worker is an execution flow that runs alongside the default worker. You can create a worker and send messages to it using the following syntax:
worker friend(message m) { // Do some work here reply m; } msg -> friend; // Send message to friend // Do my own work replyMsg <- friend; // Receive reply
Key characteristics of workers:
The friend worker runs in parallel with the default worker.
The default worker continues its own execution independently.
The default worker can block while waiting for a result from the friend worker, with an optional timeout (default 1 minute).
Fork‑Join (Multiple Workers) Parallelism
When you need to send the same message to several workers and handle their results differently, fork‑join is used. Define workers inside a fork block and specify how to join their results or handle a timeout:
fork(msg) { worker chanaka(message m1) { // Do some work here reply m1; } worker sameera(message m2) { // Do something else reply m2; } worker isuru(message m3) { // Do another thing reply m3; } } join (all)(message[] results) { // Process all results } timeout (60)(message[] resultsBeforeTimeout) { // Handle timeout case }
In this example the three workers run in parallel with the default worker. The join clause determines how results are collected (e.g., wait for all workers, wait for any, etc.), while the timeout clause executes if the join condition is not satisfied within the specified seconds.
The timeout is coupled with the join block; if the condition isn’t met within the given time, the timeout block runs and any completed worker results are returned.
During the wait, the default worker remains idle.
Workers can invoke any function declared in the same or other packages, but currently a worker other than the default cannot communicate directly with another worker.
Comprehensive Development Toolset
Ballerina provides a full suite of tools to simplify the development experience.
Composer
The Composer is the primary IDE for writing Ballerina programs, offering source, design, and Swagger views, direct run/debug capabilities, and drag‑and‑drop composition of program elements.
Testerina
Testerina is Ballerina’s unit‑testing framework, allowing developers to write tests, mock components, and simulate real Ballerina programs.
Connectors
Connectors are client libraries that enable integration with various cloud APIs and systems; developers can also create custom connectors using Ballerina.
Editor Plugins
Ballerina ships plugins for popular editors such as IntelliJ IDEA, Atom, VS Code, and Vim, bringing language support to the tools developers prefer.
Architects Research Society
A daily treasure trove for architects, expanding your view and depth. We share enterprise, business, application, data, technology, and security architecture, discuss frameworks, planning, governance, standards, and implementation, and explore emerging styles such as microservices, event‑driven, micro‑frontend, big data, data warehousing, IoT, and AI architecture.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.