-
Notifications
You must be signed in to change notification settings - Fork 243
Implement AsyncRequestProcessor using CompletableFuture. #255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,35 @@ | ||
| package mate.academy; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.Executor; | ||
|
|
||
| public class AsyncRequestProcessor { | ||
| private final Executor executor; | ||
| private final Map<String, UserData> cache; | ||
|
|
||
| public AsyncRequestProcessor(Executor executor) { | ||
| this.executor = executor; | ||
| this.cache = new ConcurrentHashMap<>(); | ||
| } | ||
|
|
||
| public CompletableFuture<UserData> processRequest(String userId) { | ||
| return null; | ||
| if (cache.containsKey(userId)) { | ||
| return CompletableFuture.completedFuture(cache.get(userId)); | ||
| } | ||
|
|
||
| return CompletableFuture.supplyAsync(() -> { | ||
| try { | ||
| Thread.sleep(200); | ||
| } catch (InterruptedException e) { | ||
|
Comment on lines
+21
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The toString() method uses curly braces |
||
| throw new RuntimeException(e); | ||
| } | ||
| return "Details for " + userId; | ||
| }, executor).thenApply(details -> { | ||
| UserData userData = new UserData(userId, details); | ||
| cache.put(userId, userData); | ||
| return userData; | ||
| }); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,27 @@ | ||
| package mate.academy; | ||
|
|
||
| public record UserData(String userId, String details) { | ||
| public class UserData { | ||
| private final String userId; | ||
| private String details; | ||
|
|
||
| public UserData(String userId, String details) { | ||
| this.userId = userId; | ||
| this.details = details; | ||
| } | ||
|
|
||
| public String userId() { | ||
| return userId; | ||
| } | ||
|
|
||
| public String details() { | ||
| return details; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "UserData{" | ||
| + "userId=" + userId | ||
| + ", details=" + details | ||
| + '}'; | ||
|
Comment on lines
+21
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The toString() format uses curly braces |
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.