0

我正在构建一个 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$000(AbstractSharedContext.java:14)
at io.quarkus.arc.impl.AbstractSharedContext$1.get(AbstractSharedContext.java:29)
at io.quarkus.arc.impl.AbstractSharedContext$1.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 上 delpoy:

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

这是我初始化更改流并在集合上启动监视的类和方法

@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客户端库的类的源码,发现报错在构造函数中

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 中运行应用程序时才会导致此错误的任何想法?谢谢

4

1 回答 1

1

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

于 2021-05-24T17:29:23.357 回答