Quarkus Mongodb 在本机构建中更改流 NullPointerException

Quarkus Mongodb change stream NullPointerException in native build

我正在构建一个 quarkus 应用程序,它使用 mongodb 更改流功能和反应式客户端。

如果我从 Intellij 在本地启动应用程序,一切正常,但是当我构建本机应用程序并 运行 它在 docker 图像中时,我收到此错误

2021-05-24 14:32:51,983 INFO [org.mon.dri.connection] (main) Opened connection [connectionId{localValue:13, serverValue:220678}] to cluster0-shard-00-02.plt2x.mongodb.net:27017
2021-05-24 14:32:52,146 INFO [org.mon.dri.connection] (main) Closed connection [connectionId{localValue:13, serverValue:220678}] to cluster0-shard-00-02.plt2x.mongodb.net:27017 because the pool has been closed.
2021-05-24 14:32:52,197 ERROR [io.qua.run.Application] (main) Failed to start application (with profile prod): java.lang.NullPointerException
at com.mongodb.client.model.changestream.ChangeStreamDocumentCodec.<init>(ChangeStreamDocumentCodec.java:45)
at com.mongodb.client.model.changestream.ChangeStreamDocument.createCodec(ChangeStreamDocument.java:296)
at com.mongodb.reactivestreams.client.internal.ChangeStreamPublisherImpl.<init>(ChangeStreamPublisherImpl.java:65)
at com.mongodb.reactivestreams.client.internal.MongoCollectionImpl.watch(MongoCollectionImpl.java:277)
at io.quarkus.mongodb.impl.ReactiveMongoCollectionImpl.watch(ReactiveMongoCollectionImpl.java:360)
at com.eventmanager.event.EventService.initOrderStream(EventService.java:89)
at com.eventmanager.event.EventService.init(EventService.java:46)
at com.eventmanager.event.EventService_Bean.create(EventService_Bean.zig:376)
at com.eventmanager.event.EventService_Bean.create(EventService_Bean.zig:392)
at io.quarkus.arc.impl.AbstractSharedContext.createInstanceHandle(AbstractSharedContext.java:96)
at io.quarkus.arc.impl.AbstractSharedContext.access[=11=]0(AbstractSharedContext.java:14)
at io.quarkus.arc.impl.AbstractSharedContext.get(AbstractSharedContext.java:29)
at io.quarkus.arc.impl.AbstractSharedContext.get(AbstractSharedContext.java:26)
at io.quarkus.arc.impl.LazyValue.get(LazyValue.java:26)
at io.quarkus.arc.impl.ComputingCache.computeIfAbsent(ComputingCache.java:69)
at io.quarkus.arc.impl.AbstractSharedContext.get(AbstractSharedContext.java:26)
at io.quarkus.arc.impl.ClientProxies.getApplicationScopedDelegate(ClientProxies.java:17)
at com.eventmanager.event.EventService_ClientProxy.arc$delegate(EventService_ClientProxy.zig:67)
at com.eventmanager.event.EventService_ClientProxy.arc_contextualInstance(EventService_ClientProxy.zig:82)
at com.eventmanager.event.EventService_Observer_Synthetic_d70cd75bf32ab6598217b9a64a8473d65e248c05.notify(EventService_Observer_Synthetic_d70cd75bf32ab6598217b9a64a8473d65e248c05.zig:94)
at io.quarkus.arc.impl.EventImpl$Notifier.notifyObservers(EventImpl.java:283)
at io.quarkus.arc.impl.EventImpl$Notifier.notify(EventImpl.java:268)
at io.quarkus.arc.impl.EventImpl.fire(EventImpl.java:70)
at io.quarkus.arc.runtime.ArcRecorder.fireLifecycleEvent(ArcRecorder.java:128)
at io.quarkus.arc.runtime.ArcRecorder.handleLifecycleEvents(ArcRecorder.java:97)
at io.quarkus.deployment.steps.LifecycleEventsBuildStep$startupEvent1144526294.deploy_0(LifecycleEventsBuildStep$startupEvent1144526294.zig:87)
at io.quarkus.deployment.steps.LifecycleEventsBuildStep$startupEvent1144526294.deploy(LifecycleEventsBuildStep$startupEvent1144526294.zig:40)
at io.quarkus.runner.ApplicationImpl.doStart(ApplicationImpl.zig:609)
at io.quarkus.runtime.Application.start(Application.java:90)
at io.quarkus.runtime.ApplicationLifecycleManager.run(ApplicationLifecycleManager.java:100)
at io.quarkus.runtime.Quarkus.run(Quarkus.java:66)
at io.quarkus.runtime.Quarkus.run(Quarkus.java:42)
at io.quarkus.runtime.Quarkus.run(Quarkus.java:119)
at io.quarkus.runner.GeneratedMain.main(GeneratedMain.zig:29)

我使用此命令构建本机应用程序,因为我需要在 windows 上本地构建它并在 heroku 上部署它:

mvn package -Pnative -Dquarkus.native.container-build=true -Dquarkus.native.builder-image=quay.io/quarkus/ubi-quarkus-native-image:21.0.0-java11

这是 class 和方法,我在其中初始化更改流并在集合上启动监视

@Inject
ReactiveMongoClient mongoClient;

private void initOrderStream() {
    ReactiveMongoDatabase database = mongoClient.getDatabase("database");
    ReactiveMongoCollection<Order> dataCollection = database.getCollection("order", Order.class);
    ChangeStreamOptions options = new ChangeStreamOptions().fullDocument(FullDocument.UPDATE_LOOKUP);


    List<Bson> pipeline = Collections.singletonList(
            Aggregates.match(
                    Filters.and(
                            Filters.eq("operationType", "update"),
                            Filters.eq("updateDescription.updatedFields.orderStatus", "PENDING")
                    )
            )
    );

    Multi<ChangeStreamDocument<Order>> publisher = dataCollection.watch(pipeline, Order.class, options);
    publisher.subscribe().with(eventListener.getOrderListener());
}

这是监听方法

public Consumer<ChangeStreamDocument<Order>> getOrderListener() {
    return message -> {
        Order order = message.getFullDocument();
        saveEvent(order);
    };
}

没看懂报错,查看空指针异常所在的mongo客户端库的class源码,发现错误在构造函数

ChangeStreamDocumentCodec(final Class<TResult> fullDocumentClass, final CodecRegistry codecRegistry) {

    ClassModelBuilder<ChangeStreamDocument> classModelBuilder = ClassModel.builder(ChangeStreamDocument.class);
    ((PropertyModelBuilder<TResult>) classModelBuilder.getProperty("fullDocument")).codec(codecRegistry.get(fullDocumentClass));
    ((PropertyModelBuilder<OperationType>) classModelBuilder.getProperty("operationType")).codec(OPERATION_TYPE_CODEC);
    ClassModel<ChangeStreamDocument> changeStreamDocumentClassModel = classModelBuilder.build();

    PojoCodecProvider provider = PojoCodecProvider.builder()
            .register(MongoNamespace.class)
            .register(UpdateDescription.class)
            .register(TruncatedArray.class)
            .register(changeStreamDocumentClassModel)
            .build();

    CodecRegistry registry = fromRegistries(fromProviders(provider, new BsonValueCodecProvider()), codecRegistry);
    this.codec = (Codec<ChangeStreamDocument<TResult>>) (Codec<? extends ChangeStreamDocument>) registry.get(ChangeStreamDocument.class);
}

空指针所在的行是这一行:

((PropertyModelBuilder<TResult>) classModelBuilder.getProperty("fullDocument")).codec(codecRegistry.get(fullDocumentClass));

所以我尝试删除 fullDocument 选项,但错误仍然存​​在。

知道只有当我 运行 应用程序在 docker 中时才会导致此错误吗? 谢谢

这刚刚修复 here,将在 1.13.52.0.0.Alpha4

中可用