locked
How to receive pending message in Azure Service Bus queue? RRS feed

  • Question

  • Hi All,

    I am working on receiving a message from Azure Service Bus queue.
    I have created a queue (SampleQueue) and I am able to send the message successfull in that queue via POSTMAN using a SAS token which I generate with my Java program.
    I am also getting a 201 created status after hitting my service bus queue api url.

    I want to receive the message pending in my Service bus queue, how can I get those messages ( java code would be preferable)?
    I went through some links about receiving message (https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-java-how-to-use-queues), but this does not contain information about how I can receive and view those messages. Is there any way we can receive pending message on service bus queue?
    Would be grateful for any help here.

    My Java code that receives the message from Service bus queue looks like below:-

    public class Test2 {
    	
    	public static void main(String[] args) throws ServiceException {
    
    	    String namespace        = "SampleNamespace";
    	    String sharedKeyName    = "RootManageSharedAccessKey";
    	    String sharedSecretKey  = "t+U5ERMAnIyxgEUDUouGOKn6ADM/CuLWzEJZtauwVsc=";
    	    String queueName        = "QueueName";      
    
    	    // Azure Service Bus Service
    	    com.microsoft.windowsazure.Configuration config = ServiceBusConfiguration.configureWithSASAuthentication(namespace, sharedKeyName, sharedSecretKey, ".servicebus.windows.net");
    	    ServiceBusContract service = ServiceBusService.create(config);
    
    	    // Receive and Delete Messages
    	    ReceiveMessageOptions opts = ReceiveMessageOptions.DEFAULT;
    	    opts.setReceiveMode(ReceiveMode.RECEIVE_AND_DELETE);
    
    	    while (true) {
    	       
    	        ReceiveQueueMessageResult resultQM = service.receiveQueueMessage(queueName , opts);
    	        BrokeredMessage message = resultQM.getValue();
    	        if (message != null && message.getMessageId() != null) {
    	        	 System.out.println("Body: " + message.toString());
    	            System.out.println("MessageID: " + message.getMessageId());
    	        } else {
    	            System.out.println("No more messages.");
    	            break;
    	        }
    	    }
    	}
    
    }

    After resolving all dependencies erros , I run my code, I just displays "No more messages". Its not displaying my pending message body.

    Help here would be appreciated.


    Rudra



    • Edited by RockyRudra Tuesday, July 30, 2019 3:56 PM Code clarity and modification
    Tuesday, July 30, 2019 8:34 AM

All replies

  • The document that you are referring to is the right one. Specifically this part covers the receiving messages part.

    If the intention is just to view messages that are in the queue, you are looking for the Peek/Browse functionality. You can refer this sample for that.

    Also, you could use ServiceBusExplorer if the intent is just to view the messages for development/debugging.

    Wednesday, July 31, 2019 6:38 AM
  • Hi All,

    I am able to come up with a Java Program that uses "PeekLock" method to fetch the pending messages in Azure Service Bus Queue.

    Below is my Java Program that uses "BrokeredMessaging" to fetch the messages :-

    import java.io.StringWriter;
    import java.nio.charset.StandardCharsets;
    
    
    import java.util.Scanner;
    
    import com.microsoft.windowsazure.services.servicebus.ServiceBusConfiguration;
    import com.microsoft.windowsazure.services.servicebus.ServiceBusContract;
    import com.microsoft.windowsazure.services.servicebus.ServiceBusService;
    import com.microsoft.windowsazure.services.servicebus.models.BrokeredMessage;
    import com.microsoft.windowsazure.services.servicebus.models.ReceiveMessageOptions;
    import com.microsoft.windowsazure.services.servicebus.models.ReceiveMode;
    import com.microsoft.windowsazure.services.servicebus.models.ReceiveQueueMessageResult;
    
    
    public class Test1 {
    
        //static StringWriter writer = new StringWriter();
    
        public static void main(String...s) throws Exception{
    
            com.microsoft.windowsazure.Configuration config = ServiceBusConfiguration.configureWithSASAuthentication("Your_NameSpace", "RootManageSharedAccessKey", "Mkf1H3g9qg0LrNEP1QbZ/EJKSARmJZQdOI6ek6obalI=", ".servicebus.windows.net");
            ServiceBusContract service = ServiceBusService.create(config);
    
            ReceiveMessageOptions opts = ReceiveMessageOptions.DEFAULT;
            opts.setReceiveMode(ReceiveMode.PEEK_LOCK);
            while(true)
            { 
                 ReceiveQueueMessageResult resultQM = service.receiveQueueMessage("Queue_Name", opts);
                 BrokeredMessage message = resultQM.getValue(); 
                 if (message != null && message.getMessageId() != null)
                 {
                    try 
                    {
                    //  IOUtils.copy(message.getBody(), writer, encoding);
    
                        Scanner s1 = new Scanner(message.getBody()).useDelimiter("\\A");
                        String result = s1.hasNext() ? s1.next() : "";
    
                        //above will convert InputStream in String
    
                       System.out.println("Body: " + message.toString());
                       System.out.println("MainBody : " + result );
                       System.out.println("MessageID: " + message.getMessageId());
                       System.out.println("Custom Property: " + 
                            message.getProperty("TestProperty"));
                       // Remove message from queue
                       System.out.println("Deleting this message.");
                       service.deleteMessage(message);
                    }
                    catch (Exception ex)
                    {
                       // Indicate a problem, unlock message in queue
                       System.out.println("Inner exception encountered!");
                       service.unlockMessage(message);
                    }
                 }
                 else
                 {
                    System.out.println("Finishing up - no more messages.");
                    break; 
                    // Added to handle no more messages in the queue.
                    // Could instead wait for more messages to be added.
                 }
            }
    
        }
    
    
    }

    The BrokeredMessaging Maven dependencies will be required for this. Make sure to get that to make code error free.

    Thanks,

    Rudra

    Tuesday, August 6, 2019 2:49 PM
  • Hi Pramod,

    I am able to come up with a simple but effective Java Program that. Have posted that above.

    Thanks,

    Rudra

    Tuesday, August 6, 2019 2:52 PM