在方法内部“回滚”REST 方法调用的最佳实践

tje*_*net 6 java rest undo rollback spring-boot

标题可能不正确,但我会尽力解释我的问题。我的项目是一个 Spring Boot 项目。我有调用外部 REST 端点的服务。

我有一个服务方法,其中包含对我拥有的其他服务的多个方法调用。每个单独的方法调用都可能成功与否。每个方法调用都对 REST 端点进行,并且可能存在一些问题,例如 Web 服务不可用或在极少数情况下抛出未知异常。无论发生什么,我都需要能够跟踪哪些方法调用成功,如果其中任何一个失败,我想回滚到原始状态,就好像什么也没发生一样,将其视为@Transactional 注释。所有 REST 调用都是不同的端点,需要单独调用,并且来自我没有影响的外部方。例子:

public MyServiceImpl implements MyService {
@Autowired
private Process1Service;
@Autowired
private Process2Service;
@Autowired
private Process3Service;
@Autowired
private Process4Service;

public void bundledProcess() {
       process1Service.createFileRESTcall();
       process2Service.addFilePermissionsRESTcall();
       process3Service.addFileMetadataRESTcall(); <-- might fail for example
       process4Service.addFileTimestampRESTcall();       
  }
}
Run Code Online (Sandbox Code Playgroud)

例如,如果process3Service.addFileMetadataRESTcall失败,我想对 process3 之前的每个步骤执行类似撤消(以相反顺序)的操作:

process2Service.removeFilePermissionsRESTcall();
process1Service.deleteFileRESTcall();
Run Code Online (Sandbox Code Playgroud)

我读过命令模式,但它似乎用于应用程序内的撤消操作,作为一种已执行操作的历史记录,而不是在 Spring Web 应用程序内。这对我的用例是否也正确,或者如果成功,我应该跟踪每个方法/网络服务调用吗?有没有这样做的最佳实践?

我想无论我如何跟踪它,我都需要知道哪个方法调用失败,然后从那里执行我的“撤消”方法 REST 调用。尽管从理论上讲,即使这些调用当然也可能会失败。

我的主要目标是不创建(在我的示例中)未执行任何进一步处理的文件。它要么全部成功,要么一无所获。一种交易。

更新 1:基于评论改进的伪实现:

public Process1ServiceImpl implements Process1Service {
    public void createFileRESTcall() throws MyException {
         // Call an external REST api, pseudo code:
         if (REST-call fails) {
            throw new MyException("External REST api failed");
         }                         
    }
}

public class BundledProcessEvent {
    private boolean createFileSuccess;
    private boolean addFilePermissionsSuccess;
    private boolean addFileMetadataSuccess;
    private boolean addFileTimestampSuccess;

    // Getters and setters
}

public MyServiceImpl implements MyService {
@Autowired
private Process1Service;
@Autowired
private Process2Service;
@Autowired
private Process3Service;
@Autowired
private Process4Service;
@Autowired
private ApplicationEventPublisher applicationEventPublisher;

@Transactional(rollbackOn = MyException.class)
public void bundledProcess() {
   BundleProcessEvent bundleProcessEvent = new BundleProcessEvent();
   this.applicationEventPublisher.publishEvent(bundleProcessEvent);

   bundleProcessEvent.setCreateFileSuccess = bundprocess1Service.createFileRESTcall();
   bundleProcessEvent.setAddFilePermissionsSuccess = process2Service.addFilePermissionsRESTcall();
   bundleProcessEvent.setAddFileMetadataSuccess = process3Service.addFileMetadataRESTcall();
   bundleProcessEvent.setAddFileTimestampSuccess = process4Service.addFileTimestampRESTcall();
}

@TransactionalEventListener(phase = TransactionPhase.AFTER_ROLLBACK)
public void rollback(BundleProcessEvent bundleProcessEvent) {
      // If the last process event is successful, we should not
      // be in this rollback method even
      //if (bundleProcessEvent.isAddFileTimestampSuccess()) {
         // remove timestamp
      //}

      if (bundleProcessEvent.isAddFileMetadataSuccess()) {
         // remove metadata
      }

      if (bundleProcessEvent.isAddFilePermissionsSuccess()) {
         // remove file permissions
      }

      if (bundleProcessEvent.isCreateFileSuccess()) {
         // remove file
      }
}
Run Code Online (Sandbox Code Playgroud)

小智 2

你的操作看起来像一个事务,所以你可以使用@Transactional注释。从您的代码中,我无法真正告诉您如何管理每个操作的 HTTP 响应调用,但您应该考虑让您的服务方法返回它们,然后根据响应调用进行回滚。您可以像这样创建一系列方法,但您想要的逻辑究竟如何取决于您。

private Process[] restCalls = new Process[] {
        new Process() { public void call() { process1Service.createFileRESTcall(); } },
        new Process() { public void call() { process2Service.addFilePermissionsRESTcall(); } },
        new Process() { public void call() { process3Service.addFileMetadataRESTcall(); } },
        new Process() { public void call() { process4Service.addFileTimestampRESTcall(); } },
};

interface Process {
    void call();
}

@Transactional(rollbackOn = Exception.class)
public void bundledProcess() {
    restCalls[0].call(); 
    ... // say, see which process returned wrong response code
}

@TransactionalEventListener(phase = TransactionPhase.AFTER_ROLLBACK)
public void rollback() {
    // handle rollback according to failed method index
}
Run Code Online (Sandbox Code Playgroud)

检查这篇文章。可能会派上用场。