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. From the navigation bar, select Cluster Manager.
  3. Select the event broker service that you want to configure.
  4. On the Service Details page, select the Try Me! tab.
  5. Click Open PubSub+ Broker Manager.
  6. In Broker Manager, on the left navigation bar, select Try Me!.
  7. In the Publisher panel, in the Establish Connection section, click the expand icon .
  8. 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
  9. 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.
  10. Under the Publisher panel, click Connect. Once the connection is established, in the Subscriber panel, click Connect.
  11. In the Subscriber panel, click Bind to an endpoint to receive guaranteed messages to expand the section.
  12. 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.
  13. Under Publisher, click Publish. The messages appear at the bottom of the Subscriber panel.
  14. 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.

  15. To stop consuming messages from the queue, click Stop Consume.

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. In the left navigation bar, select Try Me!
  3. If necessary, re-establish a connection for the publisher, and then publish three persistent messages.
  4. 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.
  5. 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.
  6. The stored messages in the queue are immediately sent and consumed by the subscriber.

  7. 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.

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.