springcloud 接口调用失败重试调用3次

文章讲述了在SpringMVC的controller层中,如何处理数据迁移请求,使用了`@PostMapping`注解的`/tenantMigrate`接口。当迁移失败时,通过`doPostPv`方法进行重试,最多尝试3次。涉及到的技术包括线程池、异常处理和RESTfulAPI调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  controller 层

@ApiOperation("数据迁移")
    @PostMapping("/tenantMigrate")
    public void tenantMigrate(@Valid @RequestBody TenantMigrateDto tenantMigrateDto, BindingResult bindingResult) {
        poolExecutor.execute(() -> {
            TenantMigrateResultDto resultDto = new TenantMigrateResultDto();
            try {
                
                //发送失败次数
                AtomicInteger errorNum = new AtomicInteger(ICommonConstants.INT_FALSE);
                //租户数据迁移结果信息
                doPostPv(resultDto, errorNum);
            } catch (Exception exception) {
                
                //发送失败次数
                AtomicInteger errorNum = new AtomicInteger(ICommonConstants.INT_FALSE);
                //租户数据迁移结果信息
                doPostPv(resultDto, errorNum);
            }
        });
    }

 失败重试调用

private void doPostPv(TenantMigrateResultDto resultDto, AtomicInteger errorNum) {
        //重试3次结束
        if (errorNum.get() > 2) {
            log.error("数据迁移失败3次结束通知tenantMigrate.doPostPv resultDto = {}", JSON.toJSONString(resultDto));
            return;
        }
        try {
            String reqUrl = pvService + ICommonConstants.PV_URI;
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
            HttpEntity entity = new HttpEntity<>(resultDto, headers);
            ParameterizedTypeReference<ActionResult<Object>> typeReference = new ParameterizedTypeReference<ActionResult<Object>>() {};
            ResponseEntity<ActionResult<Object>> responseEntity = restTemplate.exchange(reqUrl, HttpMethod.POST, entity, typeReference);
            log.info("调用第三方服务返回结果:{}", JSON.toJSONString(responseEntity));
        } catch (Exception exception) {
            errorNum.addAndGet(ICommonConstants.INT_TRUE);
            doPostPv(resultDto, errorNum);
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值