ARTICLE AD BOX
The method process:
1) calculates how many points a candidate has earned, 2) saves the result to the database, 3) sends a callback about it to an external service. --- need to send a callback in case of items 1 and 2 are successfully completed, and don't send otherwise import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.transaction.support.TransactionTemplate; import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Mono; import java.util.List; import java.util.Map; @Component public class InterviewService { private final ScoreRepository scoreRepository; private final TransactionTemplate transactionTemplate; private final InterviewScoreMLService interviewScoreMLService; private final ObjectMapper objectMapper = new ObjectMapper(); public InterviewService(ScoreRepository scoreRepository, TransactionTemplate transactionTemplate, InterviewScoreMLService interviewScoreMLService) { this.scoreRepository = scoreRepository; this.transactionTemplate = transactionTemplate; this.interviewScoreMLService = interviewScoreMLService; } /** * The method 1) calculates how many points a candidate has earned, 2) saves the result to the database, 3) sends a callback about it to an external service. */ public void process(Candidate c) { transactionTemplate.executeWithoutResult(status -> { Score s = interviewScoreMLService.compute(c); String body = objectMapper.writeValueAsString(Map.of(c.getName(), s)); Mono<ResponseEntity<Void>> request = WebClient.create() .post() .body(BodyInserters.fromValue(body)) .retrieve() .toBodilessEntity(); scoreRepository.saveScore(s); }); } } class Candidate { private final String name; private final List<Integer> tasksSolvedId; public Candidate(String name, List<Integer> tasksSolvedId) { this.name = name; this.tasksSolvedId = tasksSolvedId; } public String getName() { return name; } public List<Integer> getTasksSolvedId() { return tasksSolvedId; } } class Score { private final String name; private final int score; public Score(String name, int score) { this.name = name; this.score = score; } public String getName() { return name; } public int getScore() { return score; } }