我正在依次向同一目的地添加两个JMS消息。这两个消息将按照我添加它们的顺序接收,或者存在反向排序的可能性,也就是说,无论哪个消息先到达目的地,都会先被检索出来。
我的添加方式如下:
producer.send(Msg1);
producer.send(Msg2);
Msg1
和Msg2
会按顺序添加在所有情况下(例如网络故障和延迟等)吗?
消息的顺序不能保证(规范也没有要求),Total JMS Message ordering详细解释了其中的原因。此外,Stack Overflow这篇文章 How to handle order of messages in JMS?也提供了相关信息。
JMS defines that messages sent by a session to a destination must be received
in the order in which they were sent. This defines a partial ordering
constraint on a session’s input message stream.
JMS does not define order of message receipt across destinations or across
a destination’s messages sent from multiple sessions. This aspect of a
session’s input message stream order is timing-dependent. It is not under
application control.
Although clients loosely view the messages they produce within a session
as forming a serial stream of sent messages, the total ordering of this stream
is not significant. The only ordering that is visible to receiving clients is
the order of messages a session sends to a particular destination.
Several things can affect this order like message priority,
persistent/non persistent etc.
那么,回答你的问题,消息将按照发送顺序接收,并带有以上信息。然而,消息传递到服务器的顺序会受到诸如消息优先级、持久性/非持久性等限制的约束。