Как 1) сохранить в БД (транзакционным способом) 2) отправить об этом уведомление в случае успеха и наоборот? [закрыто]JAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Как 1) сохранить в БД (транзакционным способом) 2) отправить об этом уведомление в случае успеха и наоборот? [закрыто]

Сообщение Anonymous »

Процесс метода:
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 request = WebClient.create()
.post()
.body(BodyInserters.fromValue(body))
.retrieve()
.toBodilessEntity();

scoreRepository.saveScore(s);
});
}
}

class Candidate {
private final String name;
private final List tasksSolvedId;

public Candidate(String name, List tasksSolvedId) {
this.name = name;
this.tasksSolvedId = tasksSolvedId;
}

public String getName() {
return name;
}

public List 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;
}
}


Подробнее здесь: https://stackoverflow.com/questions/798 ... tion-about
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»