Commit a8c26f2f authored by Amar Bogari's avatar Amar Bogari

added code for retry feature with timeout exception cases

parent 75bcbd2c
...@@ -19,7 +19,7 @@ configurations { ...@@ -19,7 +19,7 @@ configurations {
repositories { repositories {
flatDir { flatDir {
dirs '/Users/sivanagasomeswaragandhijatla/.m2/repository/com/nisum/exceptionservice/0.0.1' dirs 'C:\\Users\\abogari\\.m2\\repository\\com\\nisum\\exceptionservice\\0.0.1'
// dirs '/Users/sivanagagasomeswaragandhijatla/.m2/repository/com/nisum/exceptionservice/0.0.1' // dirs '/Users/sivanagagasomeswaragandhijatla/.m2/repository/com/nisum/exceptionservice/0.0.1'
} }
mavenCentral() mavenCentral()
......
package com.nisum.offertransactionservice.client; package com.nisum.offertransactionservice.client;
import com.nisum.offertransactionservice.config.FeignClientConfig;
import com.nisum.offertransactionservice.dto.PERequest; import com.nisum.offertransactionservice.dto.PERequest;
import com.nisum.offertransactionservice.dto.PEResponse; import com.nisum.offertransactionservice.dto.PEResponse;
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
@FeignClient(name = "${pe.application.name}") @FeignClient(name = "${pe.application.name}",configuration = FeignClientConfig.class)
public interface FeignClientService { public interface PromotionEngineFeignClient {
@PostMapping(value = "${endpoint.url.promotionEngineUrl}") @PostMapping(value = "${endpoint.url.promotionEngineUrl}")
ResponseEntity<PEResponse> callPEService(PERequest peRequest); ResponseEntity<PEResponse> callPEService(PERequest peRequest);
......
package com.nisum.offertransactionservice.config;
import feign.FeignException;
import feign.Request;
import feign.Response;
import feign.RetryableException;
import feign.codec.ErrorDecoder;
import org.springframework.http.HttpStatus;
import java.util.Date;
public class CustomErrorDecoder implements ErrorDecoder {
private final ErrorDecoder defaultErrorDecoder = new Default();
@Override
public Exception decode(String methodKey, Response response) {
Exception exception = defaultErrorDecoder.decode(methodKey, response);
if(response.status() == HttpStatus.REQUEST_TIMEOUT.value()){
return new RetryableException(408,"408 error", response.request().httpMethod(), new Date(),response.request());
}
return exception;
}
}
package com.nisum.offertransactionservice.config;
import feign.RetryableException;
import feign.Retryer;
public class CustomRetryer implements Retryer {
private final Integer maxAttempts;
private final Long backoff;
int attempt;
public CustomRetryer(Long backoff, Integer maxAttempts) {
this.backoff = backoff;
this.maxAttempts = maxAttempts;
this.attempt = 1;
}
public void continueOrPropagate(RetryableException e) {
if (attempt++ >= maxAttempts) {
throw e;
}
try {
Thread.sleep(backoff);
} catch (InterruptedException ignored) {
Thread.currentThread().interrupt();
}
}
@Override
public Retryer clone() {
return new CustomRetryer(backoff, maxAttempts);
}
}
package com.nisum.offertransactionservice.config;
import feign.Retryer;
import feign.codec.ErrorDecoder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeignClientConfig {
@Value("#{new Integer('${maxattempts}')}")
private Integer maxAttempts;
@Value("#{new Long('${backoff}')}")
private Long backoff;
@Bean
public Retryer retryer() {
return new CustomRetryer(backoff,maxAttempts);
}
@Bean
public ErrorDecoder errorDecoder() {
return new CustomErrorDecoder();
}
}
\ No newline at end of file
...@@ -2,23 +2,19 @@ package com.nisum.offertransactionservice.service; ...@@ -2,23 +2,19 @@ package com.nisum.offertransactionservice.service;
import com.nisum.offertransactionservice.client.ClientService; import com.nisum.offertransactionservice.client.ClientService;
import com.nisum.offertransactionservice.client.FeignClientService; import com.nisum.offertransactionservice.client.PromotionEngineFeignClient;
import com.nisum.offertransactionservice.converter.OfferConvertion; import com.nisum.offertransactionservice.converter.OfferConvertion;
import com.nisum.offertransactionservice.dao.OfferLookupRepo; import com.nisum.offertransactionservice.dao.OfferLookupRepo;
import com.nisum.offertransactionservice.dao.OfferMetaDataRepo; import com.nisum.offertransactionservice.dao.OfferMetaDataRepo;
import com.nisum.offertransactionservice.dto.*; import com.nisum.offertransactionservice.dto.*;
import com.nisum.offertransactionservice.genericexception.GlobalApiGenericException; import com.nisum.offertransactionservice.genericexception.GlobalApiGenericException;
import com.nisum.offertransactionservice.model.*; import com.nisum.offertransactionservice.model.*;
import com.nisum.offertransactionservice.util.ExceptionUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.ClientResponse;
import reactor.core.publisher.Flux;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.util.*; import java.util.*;
...@@ -32,7 +28,7 @@ public class OfferCallingPEService { ...@@ -32,7 +28,7 @@ public class OfferCallingPEService {
OfferConvertion INSTANCE = Mappers.getMapper(OfferConvertion.class); OfferConvertion INSTANCE = Mappers.getMapper(OfferConvertion.class);
@Autowired @Autowired
private FeignClientService feignClientService; private PromotionEngineFeignClient feignClientService;
@Autowired @Autowired
......
...@@ -9,4 +9,10 @@ endpoint.url.spBaseUrl=http://localhost:7070 ...@@ -9,4 +9,10 @@ endpoint.url.spBaseUrl=http://localhost:7070
server.port = 7072 server.port = 7072
spring.application.name=ots spring.application.name=ots
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
//TODO: add refresh eureka endpoint properties //TODO: add refresh eureka endpoint properties
\ No newline at end of file endpoints.restart.enabled=true
endpoints.shutdown.enabled=true
endpoints.health.sensitive=false
maxattempts=3
backoff=2000
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment