Skip to content

Commit 8b9ee4e

Browse files
author
jodzga
committed
Cleanup of parseq-examples.
1 parent 4630a8c commit 8b9ee4e

File tree

9 files changed

+223
-14
lines changed

9 files changed

+223
-14
lines changed
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
package com.linkedin.parseq.example.javadoc;
2+
3+
import java.net.URI;
4+
5+
import com.linkedin.parseq.Engine;
6+
import com.linkedin.parseq.Task;
7+
import com.linkedin.parseq.example.common.AbstractExample;
8+
import com.linkedin.parseq.example.common.ExampleUtil;
9+
import com.linkedin.parseq.example.domain.Person;
10+
import com.linkedin.parseq.httpclient.HttpClient;
11+
12+
13+
/**
14+
* This example contains code used to generate diagrams in Task's javadoc.
15+
*
16+
* @author Jaroslaw Odzga ([email protected])
17+
*/
18+
public class JavadocExamples extends AbstractExample {
19+
public static void main(String[] args) throws Exception {
20+
21+
for (int i = 0; i < 100; i++) {
22+
new JavadocExamples().runExample();
23+
}
24+
new JavadocExamples().runExample();
25+
HttpClient.close();
26+
}
27+
28+
@Override
29+
protected void doRunExample(final Engine engine) throws Exception {
30+
31+
// map-1.svg
32+
// Task<String> hello = Task.value("Hello World");
33+
// Task<Integer> length = hello.map("length", s -> s.length());
34+
35+
// map-2.svg
36+
// Task<String> failing = Task.callable("hello", () -> {
37+
// return "Hello World".substring(100);
38+
// });
39+
// Task<Integer> length = failing.map("length", s -> s.length());
40+
41+
// flatMap-1
42+
// Task<URI> url = Task.value("uri", URI.create("http://linkedin.com"));
43+
// Task<String> homepage = url.flatMap("fetch", u -> fetch(u));
44+
45+
// flatMap-2
46+
// Task<URI> url = Task.callable("uri", () -> URI.create("not a URI"));
47+
// Task<String> homepage = url.flatMap("fetch", u -> fetch(u));
48+
49+
// withSideEffect-1
50+
Task<Long> id = Task.value("id", 1223L);
51+
Task<String> userName = id.flatMap("fetch", u -> fetch(u))
52+
.withSideEffect("update memcache", u -> updateMemcache(u));
53+
54+
55+
// shareable-1
56+
// final Task<Response> google = HttpClient.get("http://google.com").task();
57+
// final Task<Response> bing = HttpClient.get("http://bing.com").task();
58+
//
59+
// final Task<?> both = Task.par(google.withTimeout(10, TimeUnit.MILLISECONDS), bing);
60+
61+
// shareable-2
62+
// final Task<Response> google = HttpClient.get("http://google.com").task();
63+
// final Task<Response> bing = HttpClient.get("http://bing.com").task();
64+
//
65+
// final Task<?> both = Task.par(google.shareable().withTimeout(10, TimeUnit.MILLISECONDS),
66+
// bing.shareable());
67+
68+
// andThen-1
69+
// Task<String> hello = Task.value("greeting", "Hello World");
70+
//
71+
// // this task will print "Hello World"
72+
// Task<String> sayHello = hello.andThen("say", System.out::println);
73+
74+
// andThen-2
75+
// Task<String> failing = Task.callable("greeting", () -> {
76+
// return "Hello World".substring(100);
77+
// });
78+
//
79+
// // this task will fail with java.lang.StringIndexOutOfBoundsException
80+
// Task<String> sayHello = failing.andThen("say", System.out::println);
81+
82+
83+
//andThen-3
84+
// // task that processes payment
85+
// Task<?> processPayment = Task.callable("processPayment", () -> "");
86+
//
87+
// // task that ships product
88+
// Task<?> shipProduct = Task.action("ship", () -> {});
89+
//
90+
// // this task will ship product only if payment was
91+
// // successfully processed
92+
// Task<?> shipAfterPayment =
93+
// processPayment.andThen("shipProductAterPayment", shipProduct);
94+
95+
96+
//recover-1
97+
// long id = 1234L;
98+
//
99+
// // this task will fetch Person object and transform it into "<first name> <last name>"
100+
// // if fetching Person failed then form "Member <id>" will be return
101+
// Task<?> userName = fetchPerson(id)
102+
// .map("toSignature", p -> p.getFirstName() + " " + p.getLastName())
103+
// .recover(e -> "Member " + id);
104+
105+
106+
//onFailure-1
107+
// Task<String> failing = Task.callable("greeting", () -> {
108+
// return "Hello World".substring(100);
109+
// });
110+
//
111+
// // this task will print out java.lang.StringIndexOutOfBoundsException
112+
// // and complete with that exception as a reason for failure
113+
// Task<String> sayHello = failing.onFailure("printFailure", System.out::println);
114+
115+
//onFailure-2
116+
// Task<String> hello = Task.value("greeting", "Hello World");
117+
//
118+
// // this task will return "Hello World"
119+
// Task<String> sayHello = hello.onFailure(System.out::println);
120+
121+
//toTry-1
122+
// Task<String> hello = Task.value("greeting", "Hello World");
123+
//
124+
// // this task will complete with Success("Hello World")
125+
// Task<Try<String>> helloTry = hello.toTry("try");
126+
127+
//toTry-2
128+
// Task<String> failing = Task.callable("greeting", () -> {
129+
// return "Hello World".substring(100);
130+
// } );
131+
//
132+
// // this task will complete successfully with Failure(java.lang.StringIndexOutOfBoundsException)
133+
// Task<Try<String>> failingTry = failing.toTry("try");
134+
135+
136+
//transform-1
137+
// Task<Integer> num = Task.value("num", 10);
138+
//
139+
// // this task will complete with either complete successfully
140+
// // with String representation of num or fail with MyLibException
141+
// Task<String> text = num.transform("toString", t -> {
142+
// if (t.isFailed()) {
143+
// return Failure.of(new MyLibException(t.getError()));
144+
// } else {
145+
// return Success.of(String.valueOf(t.get()));
146+
// }
147+
// });
148+
149+
//recoverWith-1
150+
// long id = 1;
151+
//
152+
// // this task will try to fetch Person from cache and
153+
// // if it fails for any reason it will attempt to fetch from DB
154+
// Task<Person> user = fetchFromCache(id)
155+
// .recoverWith(e -> fetchFromDB(id));
156+
157+
//withtimeout-1
158+
// final Task<Response> google = HttpClient.get("http://google.com").task()
159+
// .withTimeout(10, TimeUnit.MILLISECONDS);
160+
161+
engine.run(userName);
162+
163+
userName.await();
164+
165+
166+
ExampleUtil.printTracingResults(userName);
167+
}
168+
169+
Task<Person> fetchFromCache(Long id) {
170+
return Task.callable("fetchFromCache", () -> { throw new Exception(); });
171+
}
172+
173+
Task<Person> fetchFromDB(Long id) {
174+
return Task.callable("fetchFromDB", () -> { return null; });
175+
}
176+
177+
Task<Person> fetchPerson(Long id) {
178+
return Task.callable("fetchPerson", () -> { throw new Exception(); });
179+
}
180+
181+
private class MyLibException extends Exception {
182+
public MyLibException(Throwable error) {
183+
super(error);
184+
}
185+
};
186+
187+
private Task<?> updateMemcache(String u) {
188+
return Task.callable("updateMemcache", () -> "");
189+
}
190+
191+
private Task<String> fetch(URI uri) {
192+
return Task.callable("fetch", () -> "");
193+
}
194+
195+
private Task<String> fetch(Long id) {
196+
return Task.callable("fetch", () -> "");
197+
}
198+
}

contrib/parseq-examples/src/main/java/com/linkedin/parseq/example/simple/BranchExecutedExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected void doRunExample(final Engine engine) throws Exception {
3030
final int toAdd = 42 - x;
3131
return add(x, toAdd);
3232
} else {
33-
return fetchX;
33+
return Task.value("x", x);
3434
}
3535
} );
3636

contrib/parseq-examples/src/main/java/com/linkedin/parseq/example/simple/BranchSkippedExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected void doRunExample(final Engine engine) throws Exception {
3030
final int toAdd = 42 - x;
3131
return add(x, toAdd);
3232
} else {
33-
return fetchX;
33+
return Task.value("x", x);
3434
}
3535
} );
3636

contrib/parseq-examples/src/main/java/com/linkedin/parseq/example/simple/CalcellationExample.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ protected void doRunExample(final Engine engine) throws Exception {
2525
final MockService<String> httpClient = getService();
2626

2727
final Task<Integer> fetchAndLength =
28-
fetchUrl(httpClient, "http://www.google.com", 10000).withTimeout(5000, TimeUnit.MILLISECONDS)
29-
.recover("default", t -> "").map("length", s -> s.length()).andThen("big bang", x -> System.exit(1));
28+
fetchUrl(httpClient, "http://www.google.com", 10000)
29+
.withTimeout(5000, TimeUnit.MILLISECONDS)
30+
.recover("default", t -> "")
31+
.map("length", s -> s.length())
32+
.andThen("big bang", x -> System.exit(1));
3033

3134
engine.run(fetchAndLength);
3235
Thread.sleep(20);

contrib/parseq-examples/src/main/java/com/linkedin/parseq/example/simple/DegradedExperienceExample.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ public static void main(String[] args) throws Exception {
2424
protected void doRunExample(final Engine engine) throws Exception {
2525
final MockService<String> httpClient = getService();
2626

27-
final Task<Integer> fetchAndLength = fetchUrl(httpClient, "http://www.google.com", 100)
28-
.withTimeout(50, TimeUnit.MILLISECONDS).recover("default", t -> "").map("length", s -> s.length());
27+
final Task<Integer> fetchAndLength =
28+
fetchUrl(httpClient, "http://www.google.com", 100)
29+
.withTimeout(50, TimeUnit.MILLISECONDS)
30+
.recover("default", t -> "")
31+
.map("length", s -> s.length());
2932

3033
engine.run(fetchAndLength);
3134

contrib/parseq-examples/src/main/java/com/linkedin/parseq/example/simple/ErrorPropagationExample.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ protected void doRunExample(final Engine engine) throws Exception {
2424
final MockService<String> httpClient = getService();
2525

2626
final Task<Integer> fetchAndLength =
27-
fetch404Url(httpClient, "http://www.google.com").map("length", x -> x.length());
27+
fetch404Url(httpClient, "http://www.google.com")
28+
.map("length", x -> x.length());
2829

2930
engine.run(fetchAndLength);
3031

contrib/parseq-examples/src/main/java/com/linkedin/parseq/example/simple/ErrorRecoveryExample.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ protected void doRunExample(final Engine engine) throws Exception {
2424
final MockService<String> httpClient = getService();
2525

2626
final Task<Integer> fetchAndLength =
27-
fetch404Url(httpClient, "http://www.google.com").recover("default", t -> "").map("length", s -> s.length());
27+
fetch404Url(httpClient, "http://www.google.com")
28+
.recover("default", t -> "")
29+
.map("length", s -> s.length());
2830

2931
engine.run(fetchAndLength);
3032

contrib/parseq-examples/src/main/java/com/linkedin/parseq/example/simple/FanInExample.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ protected void doRunExample(final Engine engine) throws Exception {
2727
final Task<String> fetchYahoo = fetchUrl(httpClient, "http://www.yahoo.com");
2828
final Task<String> fetchGoogle = fetchUrl(httpClient, "http://www.google.com");
2929

30-
final Task<?> fanIn = Task.par(fetchBing, fetchGoogle, fetchYahoo).andThen((bing, google, yahoo) -> {
31-
System.out.println("Bing => " + bing);
32-
System.out.println("Yahoo => " + yahoo);
33-
System.out.println("Google => " + google);
34-
} );
30+
final Task<?> fanIn = Task.par(fetchBing, fetchGoogle, fetchYahoo)
31+
.andThen((bing, google, yahoo) -> {
32+
System.out.println("Bing => " + bing);
33+
System.out.println("Yahoo => " + yahoo);
34+
System.out.println("Google => " + google);
35+
});
3536
engine.run(fanIn);
3637

3738
fanIn.await();

contrib/parseq-examples/src/main/java/com/linkedin/parseq/example/simple/TimeoutWithErrorExample.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ protected void doRunExample(final Engine engine) throws Exception {
2626
final MockService<String> httpClient = getService();
2727

2828
final Task<String> fetchWithTimeout =
29-
fetchUrl(httpClient, "http://www.google.com").withTimeout(50, TimeUnit.MILLISECONDS);
29+
fetchUrl(httpClient, "http://www.google.com")
30+
.withTimeout(50, TimeUnit.MILLISECONDS);
3031

3132
engine.run(fetchWithTimeout);
3233

0 commit comments

Comments
 (0)