Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How Ballerina  Is Different From Other Programming Languages

DZone's Guide to

How Ballerina  Is Different From Other Programming Languages

Ballerina has numerous unique features that are specifically designed to address the requirements of technology domains.

Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

Since its introduction at WSO2 Con USA 2017, ballerina has captured a lot of traction in the developer community. Here are some interesting resources if you are new to ballerina programming language:

The above resources are for a completely new reader to ballerina who wants to learn about concepts behind ballerina and get their hands dirty with it.

The intention of this post is to introduce the special features of ballerina language which are unique to itself. These features are specifically designed to address the requirements of the technology domain we are targeting with this new language.

XML, JSON, and Datatable Are Native Data Types

Communication is all about messages and data. XML and JSON are the most common and heavily used data types in any kind of integration ecosystem. In addition to those two types, interaction with databases (SQL, NoSQL) is the other most common use case. We have covered all three scenarios with native data types.

You can define XML and JSON data types inline and manipulate them easily with utility methods in JSON and messages packages.

json j = `{"company":{"name":"wso2", "country":"USA"}}`;messages:setJsonPayload(m, j);

With the above two lines, you can define your own JSON message and replace the current message with your message. You can do the same thing for XML messages, as well.

If you need to extract some data from a message which is of type application/JSON, you can easily do that with following lines of code.

json newJson = jsons:getJson(messages:getJsonPayload(m), "$.company");

The above code will set the following JSON message to the newJson variable:

{"name":"wso2","country":"USA"}

Another cool feature of this inline representation is the variable access within these template expressions. You can access any variable when you define your XML/JSON message like below.

string name = "WSO2";xml x = `<name>{$name}</name>`;

The above two lines create an XML message with following data in it:

<name>WSO2</name>

You can do the same thing for JSON messages in a similar fashion.

Datatable is a representation of a pointer to a result set returned from a database query. It works in a streaming manner. The data will be consumed as it is used in the program. Here is a sample code for reading data within a ballerina program using the datatable type.

Parallel Processing Is as Easy as It Gets

The term parallel processing scares even experienced programmers. However, with ballerina, you can do parallel processing as you do any other action. The main concept of term “ballerina” stems from the concept of a ballet dance where so many different ballet dancers synchronized with each other during the dance act by sending messages between each other. The technical term for this process is called choreography. Ballerina (language) brings this concept into a more programmer-friendly concept with following two features.

1. Parallel Processing With Worker

The concept of a worker is that it is an execution flow. The execution will be carried by the default worker. If the ballerina programmer wants to delegate their work to another worker who is working in parallel to the default worker, he or she can create a worker and send a message to that worker with the following syntax:

worker friend(message m) {
  //Do some work here
  reply m';
}
msg -> friend;
//Do my own work
replyMsg <- friend;

There are few things special about this task delegation.

  • Worker (friend) will run in parallel to the default worker.

  • Default worker can continue its worker independently.

  • When the default worker wants to get the result from the friend worker, it will call the friend worker and block there until it gets the result message or times out after one minute.

2. Parallel Processing With Fork-Join (Multiple Workers)

Sometimes, users need to send the same message to multiple workers at the same time and process results in different ways. That is where fork-join comes into rescue. Ballerina programmers can define workers and their actions within the fork-join statement and then decide on what to do once the workers are done with their work. Given below is a sample code of a fork-join.

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) {
    //Do something with results message array
  } timeout (60)(message[] resultsBeforeTimeout) {
    //Do something after timeout
  }

The above code sample is a powerful program that will be really hard to implement in any other programming language (some, languages cannot even do this). However, with ballerina, you get all the power with simplicity. Here is an explanation of the above program:

  • Workers “chanaka,” “sameera,” and “isuru” are executed in parallel to the main default worker.

  • The join condition specifies how a user would need to get the results of the above-started workers. In this sample, it waits for all workers. It is possible to join the workers in one of the following options

    • Join all of 3 workers.

    • Join all of named workers.

    • Join any 1 of all 3 workers.

    • Join any 1 of named workers.

  • The timeout condition is coupled with the join block. A user can specify the timeout value in seconds to wait until the join condition is satisfied. If that join condition is not satisfied within the given time duration, the timeout block will get executed with any results returned from the completed workers.

  • Once the fork-join statement is started and executing, the default worker is waiting until it completes the join block or timeout block. It will stay idle during that time (some rest).

In addition to the above-mentioned features, workers can invoke any function declared within the same package or any other package. One limitation with the current worker/fork-join implementation is that workers cannot be communicated with any other worker than default worker.

Comprehensive Set of Developer Tools

Ballerina is not the language and the runtime itself. It comes with a complete set of developer tools that can help you to start your ballerina experience as quickly and easily as possible.

Composer

This is the main editor to develop ballerina programs. It has so many cool features that cannot even be found in sophisticated editors. Here are some of the main features:

  • Source, design, and Swagger view of the same implementation and ability to edit through any interface.

  • Run/debug ballerina programs directly from the editor.

  • Drag/drop program elements and compose your program.

You can find more details from the ballerina lang medium publication.

Testerina

This is the unit testing framework for ballerina programs. Users can write unit tests to test their ballerina source code with this framework. It allows users to mock ballerina components and emulate the actual ballerina programs within a unit testing environment. You can find details from this Medium post.

Connectors

These are the client connectors which are written to connect with different cloud APIs and systems. This is one of the extension points ballerina has and users can write their own connectors from ballerina language and use within any other ballerina program.

Editor Plugins

Another important set of tools coming with ballerina tooling distribution is the set of editor plugins for popular source code editors like Intellij Idea, Atom, VSCode, Vim. This will make sure if you are a hardcore script editing person who is not interested in IDEs, you are also given the power of ballerina language capabilities in your favorite editor.

Conclusion

I am only half done with the cool new features of ballerina, but this is enough for a single post. You can try out these cool features and let us know your experience and thoughts through our Google user group, Twitter, Facebook, Medium, or any other channel or by putting a comment on this post.

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:
integration ,programming language ,ballerina

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}