Last active
March 19, 2025 05:00
-
-
Save digz6666/b26fad66d769a0e2f4648ddcb7f37036 to your computer and use it in GitHub Desktop.
Spring retry example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mn.astvision.smartcar.iib.client; | |
import com.fasterxml.jackson.core.JsonProcessingException; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import jakarta.annotation.PostConstruct; | |
import lombok.RequiredArgsConstructor; | |
import lombok.extern.slf4j.Slf4j; | |
import mn.astvision.smartcar.iib.request.calculate.IIBInsuranceCalculateRequest; | |
import mn.astvision.smartcar.iib.response.calculate.IIBInsuranceCalculateData; | |
import mn.astvision.smartcar.iib.response.calculate.driver.IIBDriverInsuranceCalculateResponse; | |
import mn.astvision.smartcar.iib.response.create.IIBInsuranceCreateResponse; | |
import mn.astvision.smartcar.iib.response.invoice.IIBInvoiceData; | |
import mn.astvision.smartcar.model.insurance.iib.InsuranceServiceDurationData; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.core.ParameterizedTypeReference; | |
import org.springframework.http.HttpEntity; | |
import org.springframework.http.HttpHeaders; | |
import org.springframework.http.HttpMethod; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.http.converter.HttpMessageConverter; | |
import org.springframework.http.converter.StringHttpMessageConverter; | |
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; | |
import org.springframework.stereotype.Component; | |
import org.springframework.web.client.HttpClientErrorException; | |
import org.springframework.web.client.RestTemplate; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* IIB-н API-с мэдээлэл дуудах клиент | |
* | |
* @author digz6666 | |
*/ | |
@Slf4j | |
@Component | |
@RequiredArgsConstructor | |
public class IIBClient { | |
@Value("${iibApi.baseUrl}") | |
private String baseUrl; | |
private final IIBTokenClient iibTokenClient; | |
private final ObjectMapper objectMapper; | |
private RestTemplate restTemplate; | |
@PostConstruct | |
public void setup() { | |
restTemplate = new RestTemplate(); | |
List<HttpMessageConverter<?>> messageConverters = new ArrayList<>(); | |
messageConverters.add(new StringHttpMessageConverter()); | |
messageConverters.add(new MappingJackson2HttpMessageConverter()); | |
restTemplate.setMessageConverters(messageConverters); | |
} | |
public IIBDriverInsuranceCalculateResponse calculateDriver(IIBInsuranceCalculateRequest calculateRequest) | |
throws HttpClientErrorException { | |
HttpEntity<?> requestEntity = new HttpEntity<>(calculateRequest, generateHeader()); | |
ResponseEntity<IIBDriverInsuranceCalculateResponse> calculateResponse = restTemplate.exchange( | |
baseUrl + "/orgList/resp/{serviceId}/2", | |
HttpMethod.POST, | |
requestEntity, | |
IIBDriverInsuranceCalculateResponse.class, | |
calculateRequest.getServiceId()); | |
return calculateResponse.getBody(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mn.astvision.smartcar.iib.service; | |
import com.fasterxml.jackson.core.JsonProcessingException; | |
import lombok.RequiredArgsConstructor; | |
import lombok.extern.slf4j.Slf4j; | |
import mn.astvision.smartcar.iib.client.IIBClient; | |
import mn.astvision.smartcar.iib.client.IIBTokenClient; | |
import mn.astvision.smartcar.iib.exception.IIBException; | |
import mn.astvision.smartcar.iib.request.calculate.IIBInsuranceCalculateRequest; | |
import mn.astvision.smartcar.iib.response.calculate.IIBInsuranceCalculateData; | |
import mn.astvision.smartcar.iib.response.calculate.driver.IIBDriverInsuranceCalculateResponse; | |
import mn.astvision.smartcar.iib.response.create.IIBInsuranceCreateResponse; | |
import org.springframework.http.HttpStatus; | |
import org.springframework.retry.annotation.Retryable; | |
import org.springframework.stereotype.Service; | |
import org.springframework.web.client.HttpClientErrorException; | |
import java.util.List; | |
@Slf4j | |
@Service | |
@RequiredArgsConstructor | |
public class IIBService { | |
private final IIBClient iibClient; | |
private final IIBTokenClient iibTokenClient; | |
@Retryable( | |
retryFor = HttpClientErrorException.class | |
) | |
public IIBDriverInsuranceCalculateResponse calculateDriver(IIBInsuranceCalculateRequest calculateRequest) | |
throws IIBException { | |
try { | |
IIBDriverInsuranceCalculateResponse calculateResponse = iibClient.calculateDriver(calculateRequest); | |
if (calculateResponse.getCode() == 0 && | |
calculateResponse.getMessage().equals("Хандалт буруу ! Token дахин авна уу !")) | |
throw new HttpClientErrorException(HttpStatus.UNAUTHORIZED); | |
return calculateResponse; | |
} catch (HttpClientErrorException e) { | |
if (e.getStatusCode() == HttpStatus.UNAUTHORIZED) | |
iibTokenClient.generateToken(); | |
throw e; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mn.astvision.smartcar.config; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.retry.annotation.EnableRetry; | |
@Configuration | |
@EnableRetry | |
public class RetryConfig { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Update name