Consuming Messages from a Queue

In this tutorial you'll learn how to consume guaranteed messages from the queue you created in Creating Your First Queue. To consume guaranteed messages, you connect to the event broker service and choose the queue to consume from.

The Try-Me! in the Cloud Console doesn't include the ability to connect to a queue, so you'll use the Try Me! feature in PubSub+ Broker Manager.

This tutorial shows you how to: 

  1. Connect to Broker Manager and Consume Messages 
  2. Understand How Queues Store Messages 

Before You Begin

Complete the previous tutorial, Creating Your First Queue.

Connect to Broker Manager and Consume Messages

To try out consuming messages from a queue, we'll use the Try-Me! tool in Broker Manager to connect to the event broker and bind to the queue.

  1. Log in to the PubSub+ Cloud Console if you have not done so yet. The URL to access the Cloud Console differs based on your authentication scheme. For more information, see Logging In to the PubSub+ Cloud Console.

  2. In the Cloud Console, click the Cluster Manager card or the icon on the left navigation bar.
  3. On the Services page, in the top-left, select the environment that contains your event broker service.
  4. Click the card for your event broker service. The event broker service details are displayed.
  5. On the Service Details page, select the Try Me! tab.
  6. Click Open PubSub+ Broker Manager.
  7. In Broker Manager, on the left navigation bar, select Try Me!.
  8. In the Publisher panel, in the Establish Connection section, click the expand icon .
  9. Connect to the event broker service by entering the connection information from the Service Details page:
    1. Copy the value for Client Username and paste it into the Client Username field.
    2. Copy the value for Client Password and paste it into the Client Password field
  10. In the Subscriber panel, in the Establish Connection section, click the expand icon and make sure that Same as Publisher is selected for each field.
  11. Under the Publisher panel, click Connect. Once the connection is established, in the Subscriber panel, click Connect.
  12. Screenshot showing the elements described in the surrounding text

  13. In the Subscriber panel, click Bind to an endpoint to receive guaranteed messages to expand the section.
  14. Screenshot showing the elements described in the surrounding text

  15. If you changed the Subscriber queue to a queue you created on the event broker service, make sure to change the Publisher queue to match it.
  16. Screenshot showing the elements described in the surrounding text

  17. Under Publisher, click Publish. The messages appear at the bottom of the Subscriber panel.
  18. Screenshot showing the elements described in the surrounding text

  19. To consume guaranteed messages from a Queue, type the name of the queue and click Start Consume. For example, use the default queue name or the name of the queue you created in the previous tutorial.

    Screenshot showing the elements described in the surrounding text

  20. To stop consuming messages from the queue, click Stop Consume.
  21. Screenshot showing the elements described in the surrounding text

Understand How Queues Store Messages

When a subscriber is not connected to the queue, the queue stores any messages it receives to send them to the subscriber when it reconnects. Guaranteed messages are not lost because the queue retains the message until the subscriber can receive them.

In the last step of the previous task, you stopped consuming messages, which simulates the subscriber stopping the connection to the queue. To see how queues store messages, perform these steps:

  1. In Broker Manager, in the left navigation bar, select Queues, then select the Queues tab to see the status of your queue. You should see in the Consumers column that there are zero consumers.

  2. Screenshot showing the elements described in the surrounding text

  3. In the left navigation bar, select Try Me!
  4. If necessary, re-establish a connection for the publisher, and then publish three persistent messages.
  5. Return to the queue by selecting Queues in the left navigation bar. You should see that there are now three messages shown in the Messaged Queued (msgs) column.
  6. Screenshot showing the elements described in the surrounding text

  7. Restore the connection between the queue and the subscriber. On left navigation, select the Try Me! tab, and under Subscriber, expand Bind to an endpoint to receive guaranteed messages and click Start Consume.
  8. The stored messages in the queue are immediately sent and consumed by the subscriber.

    Screenshot showing the elements described in the surrounding text

  9. Return to the queue again by selecting Queues in the left navigation bar. You should see: 
    • The Messages Queued (msgs) column displays zero because the messages have been sent to the subscriber.
    • The Consumers column displays 1 because the subscriber is consuming messages.

    Screenshot showing the elements described in the surrounding text

Next Steps

You learned how to obtain the credentials to connect to event broker service, how to consume messages, and how messages are queued. After, you learned how the queue prevents the loss of messages, and the behaviors associated with it.