Skip to content

Commit

Permalink
Merge pull request #5291 from poorna2152/multiple_receive
Browse files Browse the repository at this point in the history
Add `multiple-receive` bbe
  • Loading branch information
poorna2152 authored Aug 27, 2024
2 parents c368abb + 759b54e commit 869711c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
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
37 changes: 37 additions & 0 deletions examples/multiple-receive/multiple_receive.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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` call the `fetch` function to retrieve content. The workers
// send the result of calling the `fetch` function to the default worker.
worker w1 {
fetch("w1") -> function;
}

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

// The multiple receive action is used to receive values from both workers.
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"}

0 comments on commit 869711c

Please sign in to comment.