Процесс метода:
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
Как 1) сохранить в БД (транзакционным способом) 2) отправить об этом уведомление в случае успеха и наоборот? [закрыто] ⇐ JAVA
Программисты JAVA общаются здесь
1769466955
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;
}
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79876705/how-to-1-save-to-db-in-a-transactional-manner-2-send-the-notification-about[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия