Junior — Senior
Calculation service with transaction and retry logic
livecode
Task condition
Conducting a code review of a method that retrieves data, calculates a financial indicator, saves it to a repository, and sends a notification. The method is implemented within a transaction and contains a retry loop for data retrieval.
@Component
public class CalculateService {
@Autowired
private FileInfoService fileInfoService;
@Autowired
private SaveRepository saveRepository;
@Autowired
private Notification notification;
private static final int MAX_ATTEMPTS = 3;
@Transactional
public void calculation(GetFilesInfoRequest request) {
int attempts = 0;
double totalCount = 0;
double divisor = 6.85;
double commissionRate = 5.2534;
while (attempts < MAX_ATTEMPTS) {
SendForNotify fileInfo = fileInfoService.getFilesInfo(request);
if (fileInfo != null) {
totalCount = fileInfo.getCount();
break;
}
attempts++;
}
double result = (totalCount * commissionRate) / divisor;
saveRepository.save(result);
try {
notification.sendCalculate(result);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}