如何使用Smallrye Mutiny在vert.x事件总线中抛出异常

问题描述

我正在尝试使用quarkus事件总线作为消息总线来实现简单的命令模式实现。 我正在使用请求/回复机制。我将消息从资源发送到侦听同一频道的命令处理程序。

我正在尝试在命令处理程序中模拟异常,将Uni失败事件发送回资源​​,但是,如果我不使用recoveryWithItem,则会收到“未处理的异步异常”,我可以理解,因为命令处理程序正在阻塞,因此它必须在另一个工作线程中执行。

在and处,我只想重新抛出此异常,以便ExceptionMapper可以自定义一个方便的http响应,总之,我也希望在主线程中也抛出命令处理程序verticle抛出的异常。 >

有人可以指点我吗?谢谢!

以下是资源代码

@POST
@Path("/{idList}/items")
public Uni<Response> addItemToList(@PathParam("idList") UUID idList,AddItemToListDTO addItemToListDTO) throws RuntimeException {

    return bus.<Response>request(
        "add-item-command",new AddItemToListCommand(idList,addItemToListDTO.getItemName()))
        .onItem()
        .transform(Message::body);
        // If i dont recover the exception here,i get a "Unhandled asynchronous exception,sending back 500: (RECIPIENT_FAILURE,8191) TodoList b8f1562d-32c3-43fc-aa3d-672a6b92d9a7 not found"
        // I want the TodoListException be thrown in this thread context
}

这是命令处理程序代码

@Transactional
@ConsumeEvent(value = "add-item-command",blocking = true)
public Uni<TodoList> handle(AddItemToListCommand cmd) {

    TodoList todoList = todoListRepository
        .find("id",cmd.getIdTodoList())
        .firstResult();

    if (todoList == null) {
        return Uni.createFrom().failure(new TodoListNotFoundException(cmd.getIdTodoList()));
        // Here i am explicitly failing the Uni
    }

    TodoListItem item = new TodoListItem(cmd.getItemName());
    todoList.addItem(item);
    todoListRepository.persist(todoList);

    return Uni.createFrom().item(todoList);
}

例外:

package org.max.todo_list.domain;
import java.util.UUID;

public class TodoListNotFoundException extends RuntimeException {

    private final UUID todoListId;

    public TodoListNotFoundException(UUID todoListId) {
        super(String.format("TodoList %s not found",todoListId));
        this.todoListId = todoListId;
    }

    public UUID getTodoListId() {
        return todoListId;
    }
}

已修复! ================================================== ==================

这确实是quarkus eventBus中的错误。对于遇到相同问题的任何人,此问题已经解决

https://github.com/quarkusio/quarkus/pull/12616/commits/60fbca9b9f7ecc5a4873856495e0f24001b7b950

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)