...
Will we be able to handle everything "as-is"?
Future (or alternative)
...
What are Futures?
A Java Future, java.util.concurrent.Future
, represents the result of an asynchronous computation. When the asynchronous task is created, a Java Future
object is returned. This Future
object functions as a handle to the result of the asynchronous task. Once the asynchronous task completes, the result can be accessed via the Future
object returned when the task was started
source: http://tutorials.jenkov.com/java-util-concurrent/java-future.html
CompletableFuture (Java8+)
...
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
publicCompletableFuture<Void> future Future<String> calculateAsync= CompletableFuture.runAsync(() throws-> InterruptedException { CompletableFuture<String> completableFuture = new CompletableFuture<>(); // Simulate a long-running Job Executors.newCachedThreadPool().submit(() ->try { ThreadTimeUnit.SECONDS.sleep(5001); } catch (InterruptedException completableFuture.complete("Hello"); e) { throw return nullnew IllegalStateException(e); }); return completableFuture; } System.out.println("I'll run in a separate thread than the main thread."); }); |
source: https://www.baeldungcallicoder.com/java-8-completablefuture-tutorial/
Alternatives
Thread
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
int number = 20; Thread newThread = new Thread(() -> { System.out.println("Factorial of " + number + " is: " + factorial(number)); }); newThread.start(); |
...
# | Type | Pros | Cons | Recommend |
---|---|---|---|---|
1 | Future | Futures return value | Y | |
2 | Thread | threads does not return anything as the run() method returns void . We could possibly implement mechanism to trigger a response but this is unnecessary as futures do this | N |
RequestID Generation
...
Type | Method | Ease of implementation | Recommend |
---|---|---|---|
UUID |
| Easy | Y |
Custom | We generate our own | Medium - Hard | N |
...
Async Request Option using Messaging
...
Demo/Test
...
Jira | ||||||
---|---|---|---|---|---|---|
|