Browsing Queues with the Java API

Queue browsing allows your client applications to look at guaranteed messages, from oldest to newest, without removing them from a queue. You may optionally remove browsed messages from a queue or leave them as needed. For more information see Browsing Guaranteed Messages. The following steps explain how to setup a queue browser using the Java API.

  1. Use a MessagingService object to create a MessageQueueBrowser object and then call the start() method to connect your queue browser to the event broker:

    final MessageQueueBrowser queueBrowser = messagingService
    .createMessageQueueBrowserBuilder().build(queueToBrowse).start();
  2. Browse messages that are spooled on the queue by calling the receiveMessage() method with your queue browser object:

    final InboundMessage message = queueBrowser.receiveMessage(timeout); // timeout is the time in milliseconds that the receive() method blocks before exiting.
  3. You can use your queue browser object to remove messages from the event broker queue using the remove() method:

    queueBrowser.remove(message);
    
  4. You can use the withMessageSelector() method to only browse messages on the queue that match user-specified message properties:

    final String filterSelectorExpression = "myMessageProperty01 = 'someValue' AND myMessageProperty02 = 'someValue'";
    final MessageQueueBrowser queueBrowser = messagingService
    	.createMessageQueueBrowserBuilder().withMessageSelector(filterSelectorExpression)
    	.build(queueToBrowse).start();
     
    final InboundMessage message = browser.receiveMessage(timeout);  // Only messages with matching message properties from a selector expression will be received.			

The following sample code shows how to browse a queue:

package com.solace.sampler;
 
import com.solace.messaging.MessagingService;
import com.solace.messaging.receiver.InboundMessage;
import com.solace.messaging.receiver.MessageQueueBrowser;
import com.solace.messaging.resources.Queue;
 
public class HowToBrowseAQueue {
 
	public static void startMessageQueueBrowser(MessagingService service, Queue queueToBrowse) {
		final MessageQueueBrowser receiver = service
			.createMessageQueueBrowserBuilder().build(queueToBrowse).start();
	}
 
	public static void browseQueue(MessagingService service,
		Queue queueToBrowse, long timeout) {
	final MessageQueueBrowser browser = service
		.createMessageQueueBrowserBuilder().build(queueToBrowse).start();
 
	final InboundMessage message = browser.receiveMessage(timeout);
 
	}
 
	public static void browseAndRemoveFromQueue(MessagingService service,
		Queue queueToBrowse, long timeout) {
	final MessageQueueBrowser browser = service
		.createMessageQueueBrowserBuilder().build(queueToBrowse).start();
 
	final InboundMessage message = browser.receiveMessage(timeout);
	// message can be requested for removal from a queue
	browser.remove(message);
 
	}

	public static void browseQueueUsingMessageSelector(MessagingService service,
		Queue queueToBrowse, long timeout) {
	final String filterSelectorExpression = "myMessageProperty01 = 'someValue' AND myMessageProperty02 = 'someValue'";
	final MessageQueueBrowser browser = service
		.createMessageQueueBrowserBuilder().withMessageSelector(filterSelectorExpression)
		.build(queueToBrowse).start();
 
	// ONLY messages with matching message properties from a selector expression will be received
	final InboundMessage message = browser.receiveMessage(timeout);
 

	}

}