Ballerina (programming language)
| Ballerina | |
|---|---|
| Designed by | Sanjiva Weerawarana, James Clark, Sameera Jayasoma, Hasitha Aravinda, Srinath Perera, Frank Leymann and WSO2[1] |
| Developer | WSO2 |
| First appeared | 2017 |
| Typing discipline | Structural, strong, static, inferred |
| Implementation language | Java, Ballerina, TypeScript [2] |
| OS | Cross-platform |
| License | Apache License 2.0[3] |
| Website | ballerina |
| Influenced by | |
| Java, Javascript, Go, Rust, C#[4] | |
Ballerina is an open source general-purpose programming language and platform designed by WSO2 for cloud-era application programmers. It is easy to write and modify and is suitable for application programmers.[5][6][7]
It is an open source project [2] started in 2015 by architects from WSO2 as a code-based alternative to the configuration-based integration tools such as EAI, ESB, and workflow products.[8][9]
It has various constructs geared toward cloud-native development including support for modern data formats and protocols, reliability, distributed transactions, APIs, and event streams.[10][11][12]
History
Ballerina was designed by WSO2 to improve productivity for application developers that have to work with distributed cloud-native systems. The designers, who provided enterprise products in the integration space for over 10 years, used their knowledge of the industry when designing the language.[13][14] Ballerina was first publicly announced in 2017 and version 1.0 was released on September 10, 2019.[15]
Design
Some key concepts in Ballerina include:
- The network in the language - Ballerina introduces fundamental, new abstractions of client objects, services, resource functions, and listeners to bring networking into the language. [16]
- Sequence diagrams for programming - In Ballerina, every program has a corresponding sequence diagram that illustrates distributed and concurrent interactions automatically. [17]
- Structural, open-by-default typing - Ballerina has a statically-typed, structural type system that is designed to be network data schema friendly. [18]
- Moving from code to cloud - Ballerina brings the entire program execution process to the hands of the developer with extensible metadata that gets compiled to runnable programs for all major cloud platforms. [19]
- Automated observability - Ballerina incorporates automatic observability features into the language itself that helps keep track of metrics, logs and tracing. [20]
Examples
Hello World Service
import ballerina/http;
service hello on new http:Listener(9090) {
resource function sayHello(http:Caller caller,
http:Request req) returns error? {
check caller->respond("Hello, World!");
}
}
To start the service, navigate to the directory that contains the `.bal` file, and execute the `ballerina run` command below.
$ ballerina run hello_world.bal
[ballerina/http] started HTTP/WS listener 0.0.0.0:9090
curl http://localhost:9090/hello/sayHello
Hello, World!
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() {
worker w1 {
int w1val = checkpanic calculate("2*3");
// Sends a message asynchronously to the worker `w2`.
w1val -> w2;
// Receives a message from the worker `w2`.
int w2val = <- w2;
io:println("[w1] Message from w2: ", w2val);
// Sends messages synchronously to the worker `w3`. The worker `w1` will wait
// until the worker `w3` receives the message.
w1val ->> w3;
w2val -> w3;
// Flushes all messages sent asynchronously to the worker `w3`. The worker
// will halt at this point until all messages are sent or until the worker `w3`
// fails.
checkpanic flush w3;
}
// A worker can have an explicit return type, or else, if a return type is not mentioned,
// it is equivalent to returning ().
worker w2 {
int w2val = checkpanic calculate("17*5");
// Receives a message from the worker `w1`.
int w1val = <- w1;
io:println("[w2] Message from w1: ", w1val);
// Sends a message asynchronously to the worker `w1`.
w1val + w2val -> w1;
}
worker w3 {
intgRPC Unary Blocking
import ballerina/grpc;
import ballerina/log;
service HelloWorld on new grpc:Listener(9090) {
resource function hello(grpc:Caller caller, string name,
grpc:Headers headers) {
log:printInfo("Server received hello from " + name);
string message = "Hello " + name;
// Reads custom headers in request message.
string reqHeader = headers.get("client_header_key") ?: "none";
log:printInfo("Server received header value: " + reqHeader);
// Writes custom headers to response message.
grpc:Headers resHeader = new;
resHeader.setEntry("server_header_key", "Response Header value");
// Sends response message with headers.
grpc:Error? err = caller->send(message, resHeader);
if (err is grpc:Error) {
log:printError("Error from Connector: " + err.message());
}
// Sends `completed` notification to caller.
grpc:Error? result = caller->complete();
if (result is grpc:Error) {
log:printError("Error in sending completed notification to caller",
err = result);
}
}
}References
- ^ "Ballerina Language Specification". WSO2.
- ^ a b Open Source Contributors (18 June 2019). "Ballerina source code". GitHub.
{{cite web}}:|author=has generic name (help) - ^ "WSO2 / LICENSE". github.com. WSO2. 2017-03-08. Retrieved 2018-03-01.
- ^ "Ballerina, A modern programming language focused on integration" (PDF): 15.
{{cite journal}}: Cite journal requires|journal=(help) - ^ Jackson, Joab. "Ballerina: An API-First Programming Language". The New Stack. Retrieved 2018-06-11.
- ^ Foremski, Tom (2019-03-01). "Technology and the Arts: Celebrating Ballerina, a computer language of integration". Retrieved 2019-07-14.
- ^ Lawton, George (2018-11-01). "Ballerina language promises to improve app integration". Retrieved 2019-07-23.
- ^ "Ballerina Microservices Programming Language: Introducing the Latest Release and "Ballerina Central"". InfoQ. Retrieved 2018-06-07.
- ^ Earls, Alan (2019-03-01). "How does Ballerina stack up as a cloud-native programming language?". Retrieved 2019-07-23.
- ^ Doyle, Kerry. "10 of the best programming languages to learn in 2020". Retrieved 2020-09-16.
- ^ Posta, Christian. "Evolution of Integration and Microservices with Service Mesh and Ballerina". Retrieved 2019-07-24.
- ^ staff, Techworld. "Top programming languages you should try". Techworld. Retrieved 2018-06-07.
- ^ Clark, James. "Ballerina Programming Language Part 0 - Context". Retrieved 2020-09-16.
- ^ Clark, James. "Ballerina Programming Language Part 1 - Concepts". Retrieved 2020-09-16.
- ^ "Ballerina Reinvents Cloud-Native Middleware as a Programming Language"". GlobeNewswire. Retrieved 2020-09-16.
- ^ Warusawithana, Lakmal. "Rethinking Programming: The Network in the Language". Retrieved 2020-09-16.
- ^ Fernando, Anjana. "Rethinking Programming: Making Sequence Diagrams Cool Again". Retrieved 2020-09-16.
- ^ Fernando, Anjana. "Rethinking Programming: Network Aware Type System". Retrieved 2020-09-16.
- ^ Warusawithana, Lakmal. "Rethinking Programming: From Code to Cloud". Retrieved 2020-09-16.
- ^ Fernando, Anjana. "Rethinking Programming: Automated Observability". Retrieved 2020-09-16.
- ^ Ballerina Team (16 September 2020). "Hello world service". ballerina.io.
- ^ Ballerina Team (16 September 2020). "Worker interaction". ballerina.io.
- ^ Ballerina Team (16 September 2020). "gRPC unary blocking". ballerina.io.
Further reading
- Fernando, Anjana, Warusawithana, Lakmal (2020) Beginning Ballerina Programming, Apress (part of Springer Nature)
External links
- https://ballerina.io
- https://github.com/ballerina-platform/ballerina-lang Github repository.