Browse and Lock

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.

Note:
See the WebSphere MQ Everyplace Configuration Guide for special considerations with WebSphere MQ bridge queues.
The following examples demonstrate the use of BrowseAndLock:

Java example

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:

Note:
You must supply a confirmID, in case the action of locating messages fails. It must be possible to undo the location, and this action requires the confirmID.
	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);
	}
 

C example
The C codebase example gets the actual message. Note the additional parameters, a confirmID in case the operation needs undoing, and the lockID.
	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);
	}
						


© IBM Corporation 2002. All Rights Reserved