Created
November 15, 2018 20:44
-
-
Save mcupak/a1a9955558dfcdbe33df36e9b25e0193 to your computer and use it in GitHub Desktop.
JShell transcript from my Exploring reactive programming with Java session at Devoxx Belgium 2018.
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
Thread t = new Thread(() -> System.out.println("hello devoxx")) | |
t.start() | |
ExecutorService e = Executors.newSingleThreadExecutor() | |
Future<String> f = e.submit(() -> "hello devoxx") | |
f | |
f.get() | |
ExecutorService e = ForkJoinPool.commonPool() | |
Future<String> f = e.submit(() -> "hello devoxx") | |
f.get() | |
CompletableFuture<String> cf = new CompletableFuture<String>() | |
cf.complete("done") | |
cf.get() | |
CompletableFuture<String> cf = new CompletableFuture<String>() | |
cf.get() | |
cf.completeExceptionally(new IllegalStateException()) | |
cf.get() | |
CompletableFuture.supplyAsync(() -> "hello").thenApplyAsync(x -> x + " devoxx").thenAcceptAsync(System.out::println) | |
CompletableFuture.supplyAsync(() -> "hello").thenCombineAsync(CompletableFuture.supplyAsync(() -> " devoxx"), (x, y) -> x + y).thenAcceptAsync(System.out::println) | |
CompletableFuture.supplyAsync(() -> "hello").thenCombineAsync(CompletableFuture.supplyAsync(() -> " devoxx"), (x, y) -> x + y).exceptionally(t -> "We have an error: "+t.getMessage()).thenAcceptAsync(System.out::println) | |
CompletableFuture.failedFuture(new IllegalStateException()).thenCombineAsync(CompletableFuture.supplyAsync(() -> " devoxx"), (x, y) -> x + y).exceptionally(t -> "We have an error: "+t.getMessage()).thenAcceptAsync(System.out::println) | |
CompletableFuture<String> cf = new CompletableFuture<String>() | |
cf.completeOnTimeout("timed out", 5, TimeUnit.SECONDS) | |
cf.get() | |
CompletableFuture<String> cf = new CompletableFuture<String>() | |
cf.orTimeout(5, TimeUnit.SECONDS) | |
cf.get() | |
CompletableFuture<String> cf = new CompletableFuture<String>() | |
CompletableFuture<String> copy = cf.copy() | |
cf.complete("done") | |
cf | |
copy | |
copy.get() | |
CompletableFuture<String> cf = new CompletableFuture<String>() | |
CompletableFuture<String> copy = cf.copy() | |
copy.complete("done") | |
copy | |
cf | |
class SimpleSubscriber implements Subscriber<String> { | |
public void onSubscribe(Subscription sub) {} | |
public void onNext(String item) {} | |
public void onError(Throwable t) {} | |
public void onComplete() {} | |
} | |
/ed SimpleSubscriber | |
SimpleSubscriber sub = new SimpleSubscriber() | |
SubmissionPublisher<String> pub = new SubmissionPublisher<String>() | |
pub.subscribe(sub) | |
pub.getSubscribers() | |
pub.submit("hello devoxx") | |
pub.submit("hello devoxx") | |
pub.submit("hello devoxx") | |
pub.submit("hello devoxx") | |
pub.submit("hello devoxx") | |
pub.close() | |
pub.submit("hello devoxx") | |
HttpHandler handler = he -> { | |
String body = "hello devoxx"; | |
he.sendResponseHeaders(200, body.length()); | |
try (OutputStream os = he.getResponseBody()) { | |
os.write(body.getBytes()); | |
} | |
} | |
/l handler | |
HttpServer hs = HttpServer.create(new InetSocketAddress(8000), 0) | |
hs.createContext("/hello", handler) | |
hs.start() | |
HttpClient hc = HttpClient.newHttpClient() | |
hc.version() | |
HttpRequest request = HttpRequest.newBuilder().uri(URI.create("http://localhost:8000/hello")).GET().build() | |
HttpResponse<String> response = hc.send(request, BodyHandlers.ofString()) | |
response.statusCode() | |
response.body() | |
CompletableFuture<HttpResponse<String>> response = hc.sendAsync(request, BodyHandlers.ofString()) | |
response.get().body() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment