首页 > 解决方案 > 使用@Exceptionhandler 捕获消息处理异常

问题描述

我有两个应用程序,例如)A,B

A 有一个 Saga B 只是 Web 应用程序

A 向 B 发送命令消息,B 向 A 的 Saga 发送该命令的异常,并且 A 的 Saga 接收良好

和 B 有一个@ExceptionHandler我希望被调用但它不工作

我怎样才能让它们被调用?


编辑

这是 A 应用程序的 Saga,它向 B 应用程序发送命令消息并处理 B 发送的异常

@Saga
public class OrderSaga {

    @Autowired
    private transient CommandGateway commandGateway;

    @StartSaga
    @SagaEventHandler(associationProperty = "orderId")
    public void handle(CreateOrderEvent evt) {

        String paymentId = UUID.randomUUID().toString();

        SagaLifecycle.associateWith("paymentId", paymentId);
        commandGateway.send(new CreatedPaymentCommand(paymentId, evt.getUserId(),evt.getFoodPrice())).exceptionally(exp -> {
            System.out.println("got it");
            System.out.println(exp.getMessage());
            return null;
        });
    }

}

这是 B 应用程序,它为测试抛出异常

@Aggregate
@NoArgsConstructor
public class PaymentAggregate {

    @AggregateIdentifier
    private String paymentId;
    private String userId;

    private PaymentStatus status;

    @CommandHandler
    public PaymentAggregate(CreatedPaymentCommand cmd) {

        throw new IllegalStateException("this exception was came from payment aggregates");
        // AggregateLifecycle.apply(new CreatedPaymentEvent(cmd.getPaymentId(),
        // cmd.getUserId(),cmd.getMoney()));
    }

    @ExceptionHandler(resultType = IllegalStateException.class)
    public void error(IllegalStateException exp) {
        System.out.println(exp.getMessage());
    }
    // I want this @ExceptionHandler to be invoked


    @EventSourcingHandler
    public void on(CreatedPaymentEvent evt) {
        this.paymentId = evt.getPaymentId();
        this.userId = evt.getUserId();
    }

}

一个应用程序捕获异常,如下所示

2021-08-24 11:46:43.534  WARN 14244 --- [ault-executor-2] o.a.c.gateway.DefaultCommandGateway      : Command 'com.common.cmd.CreatedPaymentCommand' resulted in org.axonframework.commandhandling.CommandExecutionException(this exception was came from payment aggregates)
got it
this exception was came from payment aggregates

但是 B 不是我认为 B 的 @ExceptionHandler 会捕获该异常

简而言之,我怎样才能调用 B 的 @ExceptionHandler

标签: axon

解决方案


它现在不起作用,因为异常是从聚合的构造函数中抛出的。当您使用构造函数命令处理程序时,还没有实例存在。如果没有实例,Axon Framework 无法发现@ExceptionHandler您设置的带注释的方法。

这是现阶段异常处理程序的唯一缺失点。老实说,参考指南应该对此更具体一些。不过,我相信这将在未来发生变化。

有一个不同的方法可以让命令处理程序构造聚合并且可以使用@ExceptionHandler: 和@CreationPolicy注释。顺便说一句,参考指南对此有话要说。

因此,您无需使用构造函数命令处理程序,而是使用 AggregateCreationPolicy.ALWAYS. 这将像这样调整您的样本:

@Aggregate
@NoArgsConstructor
public class PaymentAggregate {

    @AggregateIdentifier
    private String paymentId;
    private String userId;

    private PaymentStatus status;

    @CommandHandler
    @CreationPolicy(AggregateCreationPolicy.ALWAYS)
    public void handle(CreatedPaymentCommand cmd) {
        throw new IllegalStateException("this exception was came from payment aggregates");
        // AggregateLifecycle.apply(new CreatedPaymentEvent(cmd.getPaymentId(),
        // cmd.getUserId(),cmd.getMoney()));
    }

    @ExceptionHandler(resultType = IllegalStateException.class)
    public void error(IllegalStateException exp) {
        System.out.println(exp.getMessage());
    }
    // I want this @ExceptionHandler to be invoked


    @EventSourcingHandler
    public void on(CreatedPaymentEvent evt) {
        this.paymentId = evt.getPaymentId();
        this.userId = evt.getUserId();
    }

}

请在您的应用程序中尝试一下,@YongD。


推荐阅读