Ballerina (programming language)

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
Ballerina
Ballerina Language
Designed bySanjiva Weerawarana, James Clark, Sameera Jayasoma, Hasitha Aravinda, Srinath Perera, Frank Leymann and WSO2[1]
DeveloperWSO2
First appeared2017
Typing disciplineStructural, strong, static, inferred
Implementation languageJava, Ballerina, TypeScript [2]
OSCross-platform
LicenseApache License 2.0[3]
Websiteballerina.io
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[edit]

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[edit]

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[edit]

Hello World Service[edit]

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!

[21]

Workers[edit]

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 {
        int

[22]

gRPC Unary Blocking[edit]

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);
        }
    }
}

[23]

References[edit]

  1. ^ "Ballerina Language Specification". WSO2.
  2. ^ a b Open Source Contributors (18 June 2019). "Ballerina source code". GitHub.
  3. ^ "WSO2 / LICENSE". github.com. WSO2. 2017-03-08. Retrieved 2018-03-01.
  4. ^ "Ballerina, A modern programming language focused on integration" (PDF): 15. Cite journal requires |journal= (help)
  5. ^ Jackson, Joab. "Ballerina: An API-First Programming Language". The New Stack. Retrieved 2018-06-11.
  6. ^ Foremski, Tom (2019-03-01). "Technology and the Arts: Celebrating Ballerina, a computer language of integration". Retrieved 2019-07-14.
  7. ^ Lawton, George (2018-11-01). "Ballerina language promises to improve app integration". Retrieved 2019-07-23.
  8. ^ "Ballerina Microservices Programming Language: Introducing the Latest Release and "Ballerina Central"". InfoQ. Retrieved 2018-06-07.
  9. ^ Earls, Alan (2019-03-01). "How does Ballerina stack up as a cloud-native programming language?". Retrieved 2019-07-23.
  10. ^ Doyle, Kerry. "10 of the best programming languages to learn in 2020". Retrieved 2020-09-16.
  11. ^ Posta, Christian. "Evolution of Integration and Microservices with Service Mesh and Ballerina". Retrieved 2019-07-24.
  12. ^ staff, Techworld. "Top programming languages you should try". Techworld. Retrieved 2018-06-07.
  13. ^ Clark, James. "Ballerina Programming Language Part 0 - Context". Retrieved 2020-09-16.
  14. ^ Clark, James. "Ballerina Programming Language Part 1 - Concepts". Retrieved 2020-09-16.
  15. ^ "Ballerina Reinvents Cloud-Native Middleware as a Programming Language"". GlobeNewswire. Retrieved 2020-09-16.
  16. ^ Warusawithana, Lakmal. "Rethinking Programming: The Network in the Language". Retrieved 2020-09-16.
  17. ^ Fernando, Anjana. "Rethinking Programming: Making Sequence Diagrams Cool Again". Retrieved 2020-09-16.
  18. ^ Fernando, Anjana. "Rethinking Programming: Network Aware Type System". Retrieved 2020-09-16.
  19. ^ Warusawithana, Lakmal. "Rethinking Programming: From Code to Cloud". Retrieved 2020-09-16.
  20. ^ Fernando, Anjana. "Rethinking Programming: Automated Observability". Retrieved 2020-09-16.
  21. ^ Ballerina Team (16 September 2020). "Hello world service". ballerina.io.
  22. ^ Ballerina Team (16 September 2020). "Worker interaction". ballerina.io.
  23. ^ Ballerina Team (16 September 2020). "gRPC unary blocking". ballerina.io.

Further reading[edit]

External links[edit]