1

我有一个 Quarkus 反应式应用程序,我在其中使用自定义 Jackson 解串器。在这个反序列化器中,我必须调用一个数据库。

public class MyTypeDeserializer extends StdDeserializer<MyType> {

    public MyTypeDeserializer() {
        this(null);
    }

    public MyTypeDeserializer(Class<?> vc) {
        super(vc);
    }

    @Override
    public MyType deserialize(
            JsonParser jsonparser, DeserializationContext context)
            throws IOException {
        // Lookup the instantiated db service which has been added to jackson on application startup
        MyTypeService service = (MyTypeService) context
                .findInjectableValue("myTypeServiceBean", null, null);
        // Get the info from json
        String info = jsonparser.getText();
        // Find myType instance in database
        return service.find("info = :info", Parameters.with("info", info)).await().indefinitely();
    }

运行此代码会导致:

 java.lang.IllegalStateException: The current thread cannot be blocked: vert.x-eventloop-thread-5

我了解此错误告诉我调用线程因await().indefinitely()调用而被阻塞。但是我找不到从数据库中检索实例的另一种方法。我必须等到从数据库中加载数据。如何做到这一点?

4

1 回答 1

0

你不能在 a 中真正做到这一点,StdDeserializer因为没有办法不阻止该方法的实现。

我建议将负载从数据库转移到反序列化发生后运行的代码的一部分。

于 2021-10-29T05:21:23.460 回答