在自定义jackson反序列化器中以响应式方式从db加载实体



我有一个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()调用,调用线程被阻塞。但是我找不到从数据库中检索实例的其他方法。我必须等到数据从数据库中加载。如何才能做到这一点?

你不能在StdDeserializer中这样做,因为在该方法的实现中没有办法不阻塞。

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

最新更新