最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

spring boot - Exclusive Fallback Method Not Trigger - Stack Overflow

matteradmin3PV0评论
@Slf4j
@RequiredArgsConstructor
@Service("healthPolicyService")
public class HealthPolicyPurchaseImpl implements PolicyPurchaseService {
    
private final RestTemplate restTemplate;    
    

    @Override
    @CircuitBreaker(name = "purchase-service", fallbackMethod = "buyPolicyFallback")
    public Map<String, Object> buyPolicy(JsonObject json, Long userId) {
        Map<String, Object> responseMap = new HashMap<>();
        String policyId = json.get("policy_id").getAsString();
        String categoryId = json.get("category_id").getAsString();
        String coverId = json.get("cover_id").getAsString();
        String periodId = json.get("period_id").getAsString();
        
        // Call validatePolicy with circuit breaker and let the exception propagate
        Map<String, Object> dataMap = validatePolicy(policyId, categoryId);
       

    }

Here When I'm calling the validatePolicy method which is offline so I will be getting some exception so here fallback designed for this method is not working...instead fallback mentioned on main buyPolicy method triggers even when the exception is raised and caught in below method


    // Fallback method for the main buyPolicy method
    public Map<String, Object> buyPolicyFallback(JsonObject json, Long userId, Exception e) {
        log.error("[CIRCUIT BREAKER] Main buyPolicy fallback triggered: {}", e.getMessage());
        Map<String, Object> responseMap = new HashMap<>();
        responseMap.put("Message", "Service is currently unavailable. Please try again later.");
        responseMap.put("Status", false);
        responseMap.put("HTTP Status", HttpStatus.SERVICE_UNAVAILABLE);
        return responseMap;
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @CircuitBreaker(name = POLICY_SERVICE, fallbackMethod = "policyServiceFallback")
    private Map<String, Object> validatePolicy(String policyId, String categoryId) {
        log.info("[HealthPolicyPurchaseImpl-validatePolicy] Validating policy with id: {} and category: {}", policyId, categoryId);
        try {
            String url = "http://policy-service/inc/policy/validatePolicy/{policy_id}/{category_id}";
            ResponseEntity<Map> response = restTemplate.exchange(url, HttpMethod.GET, null, Map.class, policyId, categoryId);
            return response.getBody();
        } catch (Exception e) {
            // In case of an exception, handle it and fallback
            log.error("[HealthPolicyPurchaseImpl-validatePolicy] Error validating policy: {}", e.getMessage());
            throw e;  // Let the exception propagate so the circuit breaker on the calling method is triggered
        }
    }

    private Map<String, Object> policyServiceFallback(String policyId, String categoryId, Exception e) {
        log.error("[CIRCUIT BREAKER] Policy service fallback triggered for policyId: {} and categoryId: {}: {}", policyId, categoryId, e.getMessage());
        Map<String, Object> responseMap = new HashMap<>();
        responseMap.put("ServiceUnavailable", true);
        responseMap.put("Message", "Policy Service is unavailable. Please try again later.");
        responseMap.put("Status", false);
        responseMap.put("HTTP Status", HttpStatus.SERVICE_UNAVAILABLE);
        return responseMap;
    }
Post a comment

comment list (0)

  1. No comments so far