Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multiple-receive bbe #5291

Merged
merged 12 commits into from
Aug 27, 2024
7 changes: 7 additions & 0 deletions examples/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,13 @@
"verifyOutput": true,
"isLearnByExample": true
},
{
"name": "Multiple receive",
"url": "multiple-receive",
"verifyBuild": true,
"verifyOutput": true,
"isLearnByExample": true
},
{
"name": "Conditional send",
"url": "conditional-send",
Expand Down
36 changes: 36 additions & 0 deletions examples/multiple-receive/multiple_receive.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import ballerina/http;
import ballerina/io;
import ballerina/lang.runtime;

type Response record {
record {
string 'worker;
} args;
};

type Result record {
string|error a;
string|error b;
};

function fetch(string workerParam) returns string|error {
http:Client cl = check new ("https://postman-echo.com");
Response response = check cl->/get('worker = workerParam);
return response.args.'worker;
}

public function main() {
// Workers `w1` and `w2` fetch content using the respective arguments.
poorna2152 marked this conversation as resolved.
Show resolved Hide resolved
worker w1 {
poorna2152 marked this conversation as resolved.
Show resolved Hide resolved
fetch("w1") -> function;
}

worker w2 {
runtime:sleep(2);
fetch("w2") -> function;
}

poorna2152 marked this conversation as resolved.
Show resolved Hide resolved
// The worker waits until both values are received.
poorna2152 marked this conversation as resolved.
Show resolved Hide resolved
Result result = <- {a: w1, b: w2};
io:println(result);
}
7 changes: 7 additions & 0 deletions examples/multiple-receive/multiple_receive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Multiple receive

The multiple receive action can be used to receive values corresponding to multiple send actions. It operates by waiting for the receipt of values from all the send actions, subsequently constructing a mapping value containing those values.

::: code multiple_receive.bal :::

::: out multiple_receive.out :::
2 changes: 2 additions & 0 deletions examples/multiple-receive/multiple_receive.metatags
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
description: This BBE demonstrates the use of the multiple receive action in inter-worker communication
keywords: ballerina, ballerina by example, bbe, worker, multiple receive
2 changes: 2 additions & 0 deletions examples/multiple-receive/multiple_receive.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$ bal run multiple_receive.bal
{"a":"w1","b":"w2"}
Loading