-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5291 from poorna2152/multiple_receive
Add `multiple-receive` bbe
- Loading branch information
Showing
5 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
$ bal run multiple_receive.bal | ||
{"a":"w1","b":"w2"} |