Skip to content

Commit

Permalink
trial to reproduce with supplyAsync and get()
Browse files Browse the repository at this point in the history
  • Loading branch information
dsmf committed Jan 8, 2024
1 parent 56e28ab commit f1f2bd1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
15 changes: 15 additions & 0 deletions server/src/main/java/com/foo/server/ProcessController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.stream.IntStream;

@RestController
Expand All @@ -22,6 +23,20 @@ public ProcessController(final ProcessorService processorService) {
this.processorService = processorService;
}

@GetMapping("/hello2")
public String helloAsyncInService() {
final List<Person> persons = getPeople();
try {
// with this construct I get the NullPointerException: Cannot invoke "org.jxls.expression.ExpressionEvaluatorFactory.createExpressionEvaluator(String)"
// in the completable future
return processorService.processSupplyAsync(persons).get();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}

@GetMapping("/hello-async")
public ResponseEntity<String> helloAsync() {
final List<Person> persons = getPeople();
Expand Down
11 changes: 11 additions & 0 deletions server/src/main/java/com/foo/server/ProcessorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;

@Service
public class ProcessorService {
Expand All @@ -17,6 +18,16 @@ public class ProcessorService {
public ProcessorService() {
}

public CompletableFuture<String> processSupplyAsync(List<Person> persons) {
return CompletableFuture.supplyAsync(() -> {
LOG.debug("processSupplyAsync -- start");
final String result = process(persons);
LOG.info("result: {}", result);
LOG.debug("processSupplyAsync -- end");
return result;
});
}

@Async
public void processAsync(List<Person> persons) {
LOG.debug("processAsync -- start");
Expand Down

0 comments on commit f1f2bd1

Please sign in to comment.