Spring 重试接口和回滚接口

Spring 重试接口和回滚接口

目录

    public class SupplyChainSagaService {

    @Autowired
    private SupplyChainClient supplyChainClient;@Autowired
    private SagaFailedTaskService failedTaskService;// 异步+重试执行SAGA
    @Async
    @Retryable(value = Exception.class,maxAttempts = 3, // 最大重试3次(含首次)backoff = @Backoff(delay = 1000)
    )
    public void asyncCommitWithRetry(Order order) {supplyChainClient.commit(null, order);
    }// 重试耗尽后执行:保存失败任务到数据库
    @Recover
    public void recover(Exception e, Order order) {System.err.println("SAGA重试耗尽,订单ID:" + order.getId() + ",异常:" + e.getMessage());// 保存失败任务到数据库,等待人工触发failedTaskService.saveFailedTask(order, e);
    }
    

    }