Jump to content
Wikipedia The Free Encyclopedia

Ballerina (programming language)

From Wikipedia, the free encyclopedia
Programming language
Ballerina
Ballerina Language
Designed by Sanjiva Weerawarana, James Clark, Sameera Jayasoma, Hasitha Aravinda, Srinath Perera, Frank Leymann and WSO2 [1]
Developer WSO2
First appearedSeptember 10, 2019; 6 years ago (2019年09月10日)
Stable release
2201年12月3日 (Swan Lake Update 12) / April 10, 2025; 6 months ago (2025年04月10日)
Typing discipline Structural, strong, static, inferred
Implementation languageJava, Ballerina, TypeScript [2]
Platform x86-64
OS Cross-platform: Windows, Linux, macOS
License Apache 2.0[3]
Websiteballerina.io
Influenced by
Java, JavaScript, Go, Rust, C# [4]

Ballerina is a general-purpose programming language designed by WSO2 for cloud-era application programmers.[2] It is free and open-source software released under Apache License 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 (ESB), and workflow products.[5] [6]

It has various constructs geared toward cloud-native development including support for various data formats and protocols, reliability, distributed transactions, application programming interfaces (APIs), and event streams.[7] [8] [9]

History

[edit ]

Ballerina was first publicly announced in 2017 and version 1.0 was released on September 10, 2019.[10]

Design

[edit ]

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.[11] Ballerina was designed by WSO2 to improve productivity for application developers that have to work with distributed computing. It is easy to write and modify and is suitable for application programmers.[12] [13] [14]

The designers, who provided enterprise integration products for over 10 years, used their knowledge of the industry when designing the language,[15] [16] says WSO2 director and Ballerina founder James Clark.

Examples

[edit ]

Hello World

[edit ]

The regular Hello World program:

importballerina/io;
publicfunctionmain(){
io:println("Hello World!");
}

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.

$ ballerinarunhello_world.bal
Hello World!

The service version of the Hello World program:

importballerina/http;
service/greetonnewhttp:Listener(9090){
resourcefunctionget.()returnsstring{
return"Hello World!";
}
}

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:

$ curlhttp://localhost:9090/greet
Hello World!

[17]

REST API

[edit ]
importballerina/http;
serviceonnewhttp:Listener(9090){
resourcefunctionpostfactorial(@http:Payloadstringpayload)returnshttp:Ok|http:BadRequest{
int|errornum=int:fromString(payload);
ifnumiserror{
return<http:BadRequest>{body:"Invalid integer: "+payload};
}
ifnum<0{
return<http:BadRequest>{body:"Integer should be >= 0"};
}
intresult=1;
foreachintiin2...num{
result*=i;
}
return<http:Ok>{body:result};
}
}
$ curlhttp://localhost:9090/factorial-d5
120

GraphQL API

[edit ]
importballerina/graphql;
service/stocksonnewgraphql:Listener(4000){
resourcefunctiongetquote()returnsStockQuote{
return{
ticker:"EXPO",
price:287.5,
open:285,
prevClose:285.5,
low:276.25,
high:297
};
}
}
typeStockQuoterecord{|
stringticker;
floatprice;
floatopen;
floatprevClose;
floatlow;
floathigh;
|};
$ curl-H"Content-type: application/json"-d'{"query": "{ quote { ticker, price } }" }''http://localhost:4000/stocks'
{"data":{"quote":{"ticker":"EXPO", "price":287.5}}}

Sequence diagram

[edit ]

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

[edit ]

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<json>

importballerina/io;
publicfunctionmain()returnserror{
// Syntax for `json` object values is very similar to the syntax of JSON
jsonperson={name:"John Doe",age:25};
// Serialized `json` values conforms to the JSON specification 
io:println(person);
// The fields of the `json` value can be accessed as follows
stringname=checkperson.name;
intage=checkperson.age;
}

Code to cloud

[edit ]

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 files would look something like the following:

Ballerina.toml file:

[package]
distribution="220100"
[build-options]
cloud="k8s"

Cloud.toml file:

[container.image]
repository="bal_user"
name="greet"
tag="v0.1.0"

Workers

[edit ]
importballerina/http;
importballerina/lang.'int;
importballerina/io;
// Workers interact with each other by sending and receiving messages.
// Ballerina validates every worker interaction (send and receive)
// to avoid deadlocks.
publicfunctionmain(){
@strand{thread:"any"}
workerw1{
intw1val=checkpaniccalculate("2*3");
// Sends a message asynchronously to the worker `w2`.
w1val->w2;
// Receives a message from the worker `w2`.
intw2val=<-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.
checkpanicflushw3;
}
// A worker can have an explicit return type, or else, if a return type is not mentioned,
// it is equivalent to returning ().
@strand{thread:"any"}
workerw2{
intw2val=checkpaniccalculate("17*5");
// Receives a message from the worker `w1`.
intw1val=<-w1;
io:println("[w2] Message from w1: ",w1val);
// Sends a message asynchronously to the worker `w1`.
w1val+w2val->w1;
}
workerw3{
int|errorw1val=<-w1;
int|errorw2val=<-w1;
io:println("[w3] Messages from w1: ",w1val,", ",w2val);
}
// Waits for the worker `w1`to finish.
waitw1;
}
functioncalculate(stringexpr)returnsint|error{
http:ClienthttpClient=checknew("https://api.mathjs.org");
stringresponse=checkhttpClient->get(string`/v4/?expr=${expr}`);
returncheck'int:fromString(response);
}

[18]

References

[edit ]
  1. ^ "Ballerina Language Specification". WSO2. Archived from the original on 2020年08月11日. Retrieved 2020年04月24日.
  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). p. 15.
  5. ^ "Ballerina Microservices Programming Language: Introducing the Latest Release and "Ballerina Central"". InfoQ. Retrieved 2018年06月07日.
  6. ^ Earls, Alan (2019年03月01日). "How does Ballerina stack up as a cloud-native programming language?" . Retrieved 2019年07月23日.
  7. ^ Doyle, Kerry. "10 of the best programming languages to learn in 2020" . Retrieved 2020年09月16日.
  8. ^ Posta, Christian. "Evolution of Integration and Microservices with Service Mesh and Ballerina". YouTube . Retrieved 2019年07月24日.
  9. ^ Techworld staff. "Top programming languages you should try". Techworld. Retrieved 2018年06月07日.
  10. ^ "Ballerina Reinvents Cloud-Native Middleware as a Programming Language". GlobeNewswire (Press release). 10 September 2019. Retrieved 2020年09月16日.
  11. ^ Ratnayake, Dakshitha. "Ballerina Swan Lake: 10 Compelling Language Characteristics for Cloud Native Programming". InfoQ. Retrieved 1 March 2022.
  12. ^ Jackson, Joab. "Ballerina: An API-First Programming Language". The New Stack. Retrieved 2018年06月11日.
  13. ^ Foremski, Tom (2019年03月01日). "Technology and the Arts: Celebrating Ballerina, a computer language of integration" . Retrieved 2019年07月14日.
  14. ^ Lawton, George (2018年11月01日). "Ballerina language promises to improve app integration" . Retrieved 2019年07月23日.
  15. ^ Clark, James. "Ballerina Programming Language Part 0 - Context" . Retrieved 2020年09月16日.
  16. ^ Clark, James. "Ballerina Programming Language Part 1 - Concepts" . Retrieved 2020年09月16日.
  17. ^ Ballerina Team (16 September 2020). "Hello world service". ballerina.io. Archived from the original on 16 September 2020. Retrieved 16 September 2020.
  18. ^ Ballerina Team (16 September 2020). "Worker interaction". ballerina.io.

Further reading

[edit ]
[edit ]

AltStyle によって変換されたページ (->オリジナル) /