|
| 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 | +} |
0 commit comments