首页 > 解决方案 > Messages order for 2 different subscriptions

问题描述

I have following (simplified) setup

Consumer (single threaded, streamingConnection is singleton)

        val streamingConnection by instance<StreamingConnection>()
        streamingConnection.subscribe(Topics.OUTPUT_ORDER, OrderHandler())
        streamingConnection.subscribe(Topics.OUTPUT_TRANSACTION, TransactionHandler())

Producer (single threaded, streamingConnection is singleton)

Is nats streaming garantees, that consumer receives and handles messages in the same order? I have order id in transaction payload and I must know the id before transaction processing.

From nats java library description:

The new version minimizes threads. Only one thread is used for all callbacks, by relying on a dispatcher in the underlying NATS connection.

If there is only one thread, Topics.OUTPUT_TRANSACTION processing on consumer will be blocked until Topics.OUTPUT_ORDER onMessage() handler finishes. Am I right?

标签: javanats-streaming-server

解决方案


抱歉耽搁了。您正在使用 2 个不同的主题,这意味着在 NATS Streaming 中消息将进入不同的频道。您不能期望以任何特定顺序交付给应用程序。如果消息被发送到同一个通道,那么从单线程生产者发布的消息顺序将与流服务器发送给其消费者的顺序相同。


推荐阅读