9.4.2. 使用自定义标识符和选项
您可以使用 Saga 选项来注册自定义标识符。例如,重构了 credit 服务,如下所示:
- 生成自定义 ID 并在正文中设置,如下所示:
from("direct:reserveCredit") .bean(idService, "generateCustomId") .to("direct:creditReservation")
- 委派操作并根据需要标记当前正文(compensating 操作)。
from("direct:creditReservation") .saga() .propagation(SagaPropagation.SUPPORTS) .option("CreditId", body()) .compensation("direct:creditRefund") .bean(creditService, "reserveCredit") .log("Credit ${header.amount} reserved. Custom Id used is ${body}");
- 只有在取消 saga 时才会从标头检索 creditId 选项。
from("direct:creditRefund") .transform(header("CreditId")) // retrieve the CreditId option from headers .bean(creditService, "refundCredit") .log("Credit for Custom Id ${body} refunded");
通过将传播模式设置为 SUPPORTS,可以在 Saga 之外调用 direct:creditReservation 端点。这样,可在 Saga 路由中声明多个选项。