-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update the multiple receive with url fetches
- Loading branch information
1 parent
ab8b85b
commit da4c772
Showing
2 changed files
with
14 additions
and
12 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,27 +1,29 @@ | ||
import ballerina/http; | ||
import ballerina/io; | ||
import ballerina/lang.runtime; | ||
|
||
type Result record { | ||
int a; | ||
int b; | ||
json|error a; | ||
json|error b; | ||
}; | ||
|
||
public function main() { | ||
worker w1 { | ||
2 -> w3; | ||
fetch("https://postman-echo.com/get?worker=w1") -> function; | ||
} | ||
|
||
worker w2 { | ||
runtime:sleep(2); | ||
3 -> w3; | ||
fetch("https://postman-echo.com/get?worker=w2") -> function; | ||
} | ||
|
||
worker w3 returns json { | ||
// The worker waits until both values are received. | ||
Result result = <- {a: w1, b: w2}; | ||
return result.toJson(); | ||
} | ||
|
||
json result = wait w3; | ||
// The worker waits until both values are received. | ||
Result result = <- {a: w1, b: w2}; | ||
io:println(result); | ||
} | ||
|
||
function fetch(string url) returns json|error { | ||
http:Client cl = check new (url); | ||
record {map<json> args;} payload = check cl->get(""); | ||
return payload.args.'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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
$ bal run multiple_receive.bal | ||
{"a":2,"b":3} | ||
{"a":"w1","b":"w2"} |