步骤 5:查找长期运行的事务

复杂事件,如上述讨论的,由 SAGA 规范定义的特殊组件管理。SAGA 通常被定义为“long-lived business transaction or process/长期业务交易或流程”。其背后的基础理论是避免在横跨大量资源时使用阻塞(和锁定)事务。

在我们的购买示例中,当 ProductPurchased 事件以某种方式生成时,与之关联的 SAGA 对象将发送 CreateInvoice 命令和 PrepareDelivery 命令。这是执行购买过程(或至少部分)的方式。

请注意,SAGA 对象包含业务行为,但仅以过程的形式。这是一个关键点:在最纯粹的形式中,SAGA 对象不包含业务逻辑。

图 4. SAGA 示例

步骤 5:查找长期运行的事务 - 图1

在我的例子中,我写了以下 SAGA 对象来管理订单:

  1. public class PurchaseManagementSaga extends AbstractAnnotatedSaga {
  2. @StartSaga
  3. @SagaEventHandler(associationProperty = "itemId")
  4. public void handle(ProductPurchasedEvent event) {
  5. // identifiers
  6. String deliveryId = createDeliveryId();
  7. String invoiceId = createInvoiceId();
  8. // associate the Saga with these values before sending the commands
  9. associateWith("shipmentId", deliveryId);
  10. associateWith("invoiceId", invoiceId);
  11. // send the commands
  12. commandGateway.send(new PrepareDeliveryCommand(deliveryId));
  13. commandGateway.send(new CreateInvoiceCommand(invoiceId));
  14. }
  15. // ...
  16. }