带有 AMQP 的 Azure 服务总线 - 如何指定会话 ID

Azure Service bus with AMQP - how to specify the session ID

我正在尝试使用 AMQP QPID java 库向服务总线发送消息

我收到这个错误:

"SessionId needs to be set for all brokered messages to a Partitioned Topic that supports Ordering"

我的主题已 "Enforce Message ordering" 开启(我猜这是我收到此错误的方式)

当使用 Azure 服务总线 java 库(而不是 AMQP)时,我有这个功能:

this.entity.setSessionId(...);

使用 AMQP 库时,我没有看到用于在我要发送的消息上设置会话 ID 的选项

请注意,如果我取消选中 "Enforce Message ordering" 选项,消息将成功发送

这是我的代码

private boolean sendServiceBusMsg(MessageProducer sender,Session sendSession) {

        try {
            // generate message

            BytesMessage createBytesMessage = (BytesMessage)sendSession.createBytesMessage();

            createBytesMessage.setStringProperty(CAMPAIGN_ID, campaignKey);             
            createBytesMessage.setJMSMessageID("ID:" + bm.getMessageId());                                                    
      createBytesMessage.setContentType(Symbol.getSymbol("application/octet-stream"));

            /*message is the actual data i send / not seen here*/
            createBytesMessage.writeBytes(message.toByteArray());

            sender.send(createBytesMessage);

        } catch (JMSException e) {
    }

SessionId 属性 映射到 AMQP 消息 properties.group-id。 Qpid JMS 客户端应将其映射到 JMSXGroupID 属性,因此请尝试以下操作, createBytesMessage.setStringProperty("JMSXGroupID", "session-1");

如您所料,有一个类似的 SO 线程 verified that to disable the feature Enforce Message Ordering via set SupportOrdering with false can solve the issue, but it can't be done via Azure Service Bus Java library because the property supportsOrdering 现在是私有的。

你可以尝试设置属性 Group 就像@XinChen说的使用AMQP,下面的内容来自here.

Service Bus Sessions, also called 'Groups' in the AMQP 1.0 protocol, are unbounded sequences of related messages. ServiceBus guarantees ordering of messages in a session.

希望对您有所帮助。