Ballerina is an general-purpose
programming language
A programming language is a system of notation for writing computer programs.
Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
designed by
WSO2 for cloud-era application
programmers.
It is
free and open-source software
Free and open-source software (FOSS) is software available under a license that grants users the right to use, modify, and distribute the software modified or not to everyone free of charge. FOSS is an inclusive umbrella term encompassing free ...
released under
Apache License
The Apache License is a permissive free software license written by the Apache Software Foundation (ASF). It allows users to use the software for any purpose, to distribute it, to modify it, and to distribute modified versions of the software ...
2.
The project started in 2015 by architects from
WSO2 as a code-based alternative to the configuration-based integration tools such as
enterprise application integration (EAI),
enterprise service bus
An enterprise service bus (ESB) implements a communication system between mutually interacting software applications in a service-oriented architecture (SOA). It represents a software architecture for distributed computing, and is a special vari ...
(ESB), and workflow products.
It has various constructs geared toward cloud-native development including support for various data formats and protocols, reliability, distributed transactions, application programming interfaces (
API
An application programming interface (API) is a connection between computers or between computer programs. It is a type of software interface, offering a service to other pieces of software. A document or standard that describes how to build ...
s), and event streams.
History
Ballerina was first publicly announced in 2017 and version 1.0 was released on September 10, 2019.
Design
Ballerina is a general-purpose language with a familiar syntax along with a direct graphical representation of the code in the form of sequence diagrams. It has fundamental abstractions designed to make integration problems easier to program. Ballerina was designed by WSO2 to improve productivity for application developers that have to work with
distributed computing
Distributed computing is a field of computer science that studies distributed systems, defined as computer systems whose inter-communicating components are located on different networked computers.
The components of a distributed system commu ...
. It is easy to write and modify and is suitable for application programmers.
The designers, who provided
enterprise integration
Enterprise integration is a technical field of enterprise architecture, which is focused on the study of topics such as system interconnection, electronic data interchange, product data exchange and distributed computing environments.
It is a con ...
products for over 10 years, used their knowledge of the industry when designing the language,
says WSO2 director and Ballerina founder James Clark.
Examples
Hello World
The regular Hello World program:
import ballerina/io;
public function main()
To execute the above program, place the source code in a
.bal
file and provide the path of the file to the
bal run
command.
$ ballerina run hello_world.bal
Hello World!
The service version of the Hello World program:
import ballerina/http;
service /greet on new http:Listener(9090)
Services are executed in the same manner, except they don't terminate like regular programs do. Once the service is up and running, one can use an HTTP client to invoke the service. For example, the above service can be invoked using the following cURL command:
$ curl http://localhost:9090/greet
Hello World!
REST API
import ballerina/http;
service on new http:Listener(9090)
$ curl http://localhost:9090/factorial -d 5
120
GraphQL API
import ballerina/graphql;
service /stocks on new graphql:Listener(4000)
type StockQuote record ;
$ curl -H "Content-type: application/json" -d '' 'http://localhost:4000/stocks'
Sequence diagram
The generated
sequence diagram is a canonical representation of the source code. The two representations can be used interchangeably. The diagram support is provided through the Ballerina VS Code plugin. The following are a couple of such generated sequence diagrams, compared with its associated code.
A sample program for retrieving and processing COVID-19 data:
A sample program for creating a report out of pull request data retrieved from GitHub:
JSON support
The language provides support for working with JSON values. The builtin type `json` is defined as the following union:
(), boolean, int, float, decimal, string, json[], map
import ballerina/io;
public function main() returns error
Code to cloud
Docker and Kubernetes artifacts required for deploying the code to the cloud can be generated when building the code. Values required for these artifacts are derived from the code. Additionally, one can override these values as well using the
Cloud.toml
file. To enable generation of the cloud artifacts, the users can use the
cloud
build option in the
Ballerina.toml
file. Use
docker
to generate just the Docker image and the Dockerfile and use
k8s
to generate Kubernetes artifacts as well. Minimal sample config
TOML
Tom's Obvious, Minimal Language (TOML, originally ''Tom's Own Markup Language'') is a file format for configuration files. It is intended to be easy to read and write due to obvious semantics which aim to be "minimal", and it is designed to map u ...
files would look something like the following:
Ballerina.toml
file:
ackagedistribution = "2201.0.0"
uild-optionscloud="k8s"
Cloud.toml
file:
ontainer.imagerepository="bal_user"
name="greet"
tag="v0.1.0"
Workers
import ballerina/http;
import ballerina/lang.'int;
import ballerina/io;
// Workers interact with each other by sending and receiving messages.
// Ballerina validates every worker interaction (send and receive)
// to avoid deadlocks.
public function main()
function calculate(string expr) returns int, error
References
Further reading
* Fernando, Anjana, Warusawithana, Lakmal (2020)
Beginning Ballerina Programming',
Apress (part of Springer Nature)
External links
*
*
{{Programming languages
2017 software
Programming languages created in 2017
Scripting languages
Dynamically typed programming languages
Statically typed programming languages
Concurrent programming languages
High-level programming languages
Free software projects
Cross-platform free software
Articles with example code