straightface
straightface
JCHJava Community | Help. Code. Learn.
Created by straightface on 2/1/2025 in #java-help
Backoff and retry for parallel api calls
I have a list of objects that i make parallel api calls and store the result in database. But the problem I am having now is the api goes down and subsequent calls fails I am not sure how I should implement backoff and retry since these are parallel calls I have already executed those
@Async
public CompletableFuture<String> apiCall(String payload){
... resttemplate
return CompletableFuture.completedFuture(..);
}

// Calling class

public List<String> getResponses(List<String> payloads){
List<CompletableFuture<String>> futures = new ArrayList<>();
for(String payload : payloads){
futures.add(apiService.apiCall(payload));
}

return futures.stream()
.map(CompletableFuture::join)
.toList();
}
@Async
public CompletableFuture<String> apiCall(String payload){
... resttemplate
return CompletableFuture.completedFuture(..);
}

// Calling class

public List<String> getResponses(List<String> payloads){
List<CompletableFuture<String>> futures = new ArrayList<>();
for(String payload : payloads){
futures.add(apiService.apiCall(payload));
}

return futures.stream()
.map(CompletableFuture::join)
.toList();
}
4 replies
JCHJava Community | Help. Code. Learn.
Created by straightface on 2/3/2024 in #java-help
RMI calls in CompletableFuture stream
is it safe to call rmi service using CompletableFuture like so
Registry registry = null;
try {
// Locate the RMI registry and obtain a reference to the remote object
registry = LocateRegistry.getRegistry("your_server_hostname", 1099);
} catch (Exception e) {
e.printStackTrace();
}
PersonService personService = (PersonService) registry.lookup("PersonService");
// Use CompletableFuture to perform RMI calls in parallel
List<Person> lastNameFutures = personList.stream()
.map(person -> CompletableFuture.supplyAsync(() -> {
person.setCompletableName(personService.getLastName(person)
return person;
}
), executorService))
.collect(Collectors.toList());
Registry registry = null;
try {
// Locate the RMI registry and obtain a reference to the remote object
registry = LocateRegistry.getRegistry("your_server_hostname", 1099);
} catch (Exception e) {
e.printStackTrace();
}
PersonService personService = (PersonService) registry.lookup("PersonService");
// Use CompletableFuture to perform RMI calls in parallel
List<Person> lastNameFutures = personList.stream()
.map(person -> CompletableFuture.supplyAsync(() -> {
person.setCompletableName(personService.getLastName(person)
return person;
}
), executorService))
.collect(Collectors.toList());
Notice i am mutating person object and saving future in Person
7 replies
JCHJava Community | Help. Code. Learn.
Created by straightface on 4/7/2023 in #java-help
Should I be storing dates in utc?
Lets say I have a venue booking form that requires start date and end date for an event, users can only book full day events. So should I be storing these dates in utc or local time?
10 replies