Skip to content

Commit

Permalink
Add alternate receive bbe
Browse files Browse the repository at this point in the history
  • Loading branch information
poorna2152 committed Apr 8, 2024
1 parent 129bcb6 commit 534ede9
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
47 changes: 47 additions & 0 deletions examples/alternate-receive/alternate_receive.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import ballerina/io;
import ballerina/lang.runtime;

public function main() {
worker w1 {
2 -> w3;
}

worker w2 {
runtime:sleep(2);
3 -> w3;
}

worker w3 returns int {
// The value of the variable `result` is set as soon as the values from either
// worker `w1` or `w2` is received.
int result = <- w1 | w2;
return result;
}

worker w4 returns error? {
int value = 10;
if value == 10 {
return error("Error in worker 1");
}
value -> w6;
}

worker w5 {
runtime:sleep(2);
3 -> w6;
}

worker w6 returns int|error? {
// Alternate receive action waits until a message that is not an error is received
// when error is not an expected static type. Since `w4` returns an error it
// waits further and sets the value that is received from `w5`.
int a = check <- w4 | w5;
return a;
}

int valueW3 = wait w3;
io:println(valueW3);

int|error? valueW6 = wait w6;
io:println(valueW6);
}
7 changes: 7 additions & 0 deletions examples/alternate-receive/alternate_receive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Alternate receive

The alternate receive action can be used to receive values from several workers.

::: code alternate_receive.bal :::

::: out alternate_receive.out :::
2 changes: 2 additions & 0 deletions examples/alternate-receive/alternate_receive.metatags
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
description: This BBE demonstrates the alternate receive action
keywords: ballerina, ballerina by example, bbe, worker, alternate receive
3 changes: 3 additions & 0 deletions examples/alternate-receive/alternate_receive.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$ bal run alternate_receive.bal
2
3
7 changes: 7 additions & 0 deletions examples/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -1419,6 +1419,13 @@
"disableVerificationReason": "Includes varying output",
"isLearnByExample": true
},
{
"name": "Alternate receive",
"url": "alternate-receive",
"verifyBuild": true,
"verifyOutput": true,
"isLearnByExample": true
},
{
"name": "Multiple wait",
"url": "multiple-wait",
Expand Down

0 comments on commit 534ede9

Please sign in to comment.