-
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.
Update the alternative receiver code
- Loading branch information
1 parent
8d9e931
commit 0ea111e
Showing
3 changed files
with
34 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,57 @@ | ||
import ballerina/http; | ||
import ballerina/io; | ||
import ballerina/lang.runtime; | ||
|
||
public function main() { | ||
worker w1 { | ||
2 -> w3; | ||
fetch("https://postman-echo.com/get?worker=w1") -> w3; | ||
} | ||
|
||
worker w2 { | ||
runtime:sleep(2); | ||
3 -> w3; | ||
runtime:sleep(3); | ||
fetch("https://postman-echo.com/get?worker=w2") -> w3; | ||
} | ||
|
||
worker w3 returns int { | ||
// The value of the variable `result` is set as soon as the values from either | ||
worker w3 returns json|error? { | ||
// The value of the variable `result` is set as soon as the value from either | ||
// worker `w1` or `w2` is received. | ||
int result = <- w1 | w2; | ||
return result; | ||
json|error result = <- w1 | w2; | ||
return getJsonProperty(result); | ||
} | ||
|
||
worker w4 returns error? { | ||
int value = 10; | ||
if value == 10 { | ||
return error("Error in worker 4"); | ||
} | ||
value -> w6; | ||
// invalid url | ||
fetch("https://postman-echo.com/ge?worker=w4") -> w6; | ||
} | ||
|
||
worker w5 { | ||
worker w5 returns error? { | ||
runtime:sleep(2); | ||
3 -> w6; | ||
fetch("https://postman-echo.com/get?worker=w5") -> w6; | ||
} | ||
|
||
worker w6 returns int|error? { | ||
worker w6 returns json|error? { | ||
// Alternate receive action waits until a message that is not an error is received. | ||
// Since `w4` returns an error, it waits further and sets the value that is received from `w5`. | ||
int a = check <- w4 | w5; | ||
return a; | ||
// Since `w3` returns an error, it waits further and sets the value that is received from `w4`. | ||
json|error result = <- w4 | w5; | ||
return getJsonProperty(result); | ||
} | ||
|
||
int w3Result = wait w3; | ||
json|error? w3Result = wait w3; | ||
io:println(w3Result); | ||
|
||
int|error? w6Result = wait w6; | ||
json|error? w6Result = wait w6; | ||
io:println(w6Result); | ||
} | ||
|
||
function fetch(string url) returns json|error { | ||
http:Client client = check new (url); | ||
map<json> payload = check client->get(""); | ||
return payload["args"]; | ||
} | ||
|
||
function getJsonProperty(json|error data) returns json|error? { | ||
if data is error { | ||
return data.message(); | ||
} | ||
return data.'worker; | ||
} |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
$ bal run alternate_receive.bal | ||
2 | ||
3 | ||
w1 | ||
w5 |