Performing BrowseAndLock on a group of messages allows an application to ensure that no other application is able to process messages when they are locked. The messages remain locked until that application unlocks them. No other application can unlock the messages. Any messages that arrive on the queue after the BrowseAndLock operation are not locked.
An application can perform either a get or a delete operation on the messages to remove them from the queue. To do this, the application must supply the lockID that is returned with the enumeration of messages.
Specifying the lockID allows applications to work with locked messages without having to unlock them first.
Instead of removing the messages from the queue, it is also possible just to unlock them. This makes them visible once again to all WebSphere MQ Everyplace applications. You can achieve this by using the unlockMessage method.
The MQeEnumeration object contains all the messages that match the filter supplied to the browse. MQeEnumeration can be used in the same manner as the standard Java Enumeration. You can enumerate all the browsed messages as follows:
long confirmID = MQe.uniqueValue(); MQeEnumeration msgEnum = qmgr.browseMessagesAndLock( null, "MyQueue", null, null, confirmID, false); while( msgEnum.hasMoreElements() ) { MQeMsgObject msg = (MQeMsgObject)msgEnum.nextElement(); System.out.println( "Message from queue manager: " + msg.getAscii( MQe.Msg_OriginQMgr ) ); }The following code performs a delete on all the messages returned in the enumeration. The message's UniqueID and lockID are used as the filter on the delete operation:
while(msgEnum.hasMoreElements()) { MQeMsgObject msg = (MQeMsgObject) msgEnum.getNextMessage(null,0); processMessage(msg); MQeFields filter = msg.getMsgUIDFields(); filter.putLong(MQe.Msg_LockID, msgEnum.getLockId()); qmgr.deleteMessage(null, "MyQueue", filter); }
MQeVectorHndl hMessages; MQEINT64 lockID, confirmID=42; rc = mqeQueueManager_browseAndLock(hQueueManager, &exceptBlock, &hmessages, &lockID, hQueueManagerName, hQueueName, hFilter, NULL, /*No Attribute*/ confirmID, MQE_TRUE); /*Just UIDs*/ /*process vector*/ MQeFieldsHndl hGetFilter; rc = mqeFields_new(&exceptBlock, &hGetFilter); if (MQERETURN_OK == rc){ rc = mqeFields_putInt64(&hGetFilter, &exceptBlock, MQE_MSG_LOCKID, lockID); if (MQERETURN_OK == rc){ rc = mqeQueueManager_getMessage(&hQueueManager, &exceptBlock, hQueueManagerName, hQueueName, hGetFilter, &hMsg); }