Logical and physical ordering
Messages on queues can occur (within each priority level) in physical or logical order. Physical order is the order in which messages arrive on a queue. Logical order is when all of the messages and segments within a group are in their logical sequence, adjacent to each other, in the position determined by the physical position of the first item belonging to the group.
For a description of groups, messages, and segments, see Message groups. These physical and logical orders can differ because:
- Groups can arrive at a destination at similar times from different applications, therefore losing any distinct physical order.
- Even within a single group, messages can get out of order because of rerouting or delay of some of the messages in the group.
For example, the logical order might look like Figure Figure 1:
Figure 1. Logical order on a queue
These messages would appear in the following logical order on a queue:
- Message A (not in a group)
- Logical message 1 of group Y
- Logical message 2 of group Y
- Segment 1 of (last) logical message 3 of group Y
- (Last) segment 2 of (last) logical message 3 of group Y
- Logical message 1 of group Z
- (Last) logical message 2 of group Z
- Message B (not in a group)
The physical order, however, might be entirely different. As stated in topic Logical and physical ordering, the physical position of the first item within each group determines the logical position of the whole group. For example, if groups Y and Z arrived at similar times, and message 2 of group Z overtook message 1 of the same group, the physical order would look like Figure Figure 2:
Figure 2. Physical order on a queue
These messages appear in the following physical order on the queue:
- Message A (not in a group)
- Logical message 1 of group Y
- Logical message 2 of group Z
- Logical message 2 of group Y
- Segment 1 of (last) logical message 3 of group Y
- (Last) segment 2 of (last) logical message 3 of group Y
- Logical message 1 of group Z
- Message B (not in a group)
On WebSphere MQ for z/OS, the physical order of messages on the queue is not guaranteed if the queue is indexed by GROUPID.
When getting messages, we can specify MQGMO_LOGICAL_ORDER to retrieve messages in logical rather than physical order.
If you issue an MQGET call with MQGMO_BROWSE_FIRST and MQGMO_LOGICAL_ORDER, subsequent MQGET calls with MQGMO_BROWSE_NEXT must also specify this option. Conversely, if the MQGET with MQGMO_BROWSE_FIRST does not specify MQGMO_LOGICAL_ORDER, neither must the following MQGETs with MQGMO_BROWSE_NEXT.
The group and segment information that the queue manager retains for MQGET calls that browse messages on the queue is separate from the group and segment information that the queue manager retains for MQGET calls that remove messages from the queue. When you specify MQGMO_BROWSE_FIRST, the queue manager ignores the group and segment information for browsing, and scans the queue as though there were no current group and no current logical message.
Take special care if you use an MQGET call to browse beyond the end of a message group (or logical message not in a group) without specifying MQGMO_LOGICAL_ORDER. For example, if the last message in the group precedes the first message in the group on the queue, using MQGMO_BROWSE_NEXT to browse beyond the end of the group, specifying MQMO_MATCH_MSG_SEQ_NUMBER with MsgSeqNumber set to 1 (to find the first message of the next group) returns again the first message in the group already browsed. This could happen immediately, or a number of MQGET calls later (if there are intervening groups).
Avoid the possibility of an infinite loop by opening the queue twice for browse:
- Use the first handle to browse only the first message in each group.
- Use the second handle to browse only the messages within a specific group.
- Use the MQMO_* options to move the second browse cursor to the position of the first browse cursor, before browsing the messages in the group.
- Do not use the MQGMO_BROWSE_NEXT browse beyond the end of a group.
For further information about this, see the Application Programming Reference.
For most applications you will probably choose either logical or physical ordering when browsing. However, if you want to switch between these modes, remember that when you first issue a browse with MQGMO_LOGICAL_ORDER, your position within the logical sequence is established.
If the first item within the group is not present at this time, the group that you are in is not considered to be part of the logical sequence.
Once the browse cursor is within a group, it can continue within the same group, even if the first message is removed. Initially though, we can never move into a group using MQGMO_LOGICAL_ORDER where the first item is not present.
Parent topic:
The order in which messages are retrieved from a queue
fg12620_