Produce and consume messages

In this tutorial, we will:

  • Configure the Pulsar client
  • Create a subscription
  • Create a producer
  • Send test messages
  • Verify the results

Prerequisites

Produce and consume messages

To produce and consume messages, complete the following steps.

  1. In the ${PULSAR_HOME}/conf/client.conf file, replace webServiceUrl and brokerServiceUrl with your service URL.

  2. Create a subscription to consume messages from apache/pulsar/test-topic.

    1. bin/pulsar-client consume -s sub apache/pulsar/test-topic -n 0
  3. In a new terminal, create a producer and send 10 messages to test-topic.

    1. bin/pulsar-client produce apache/pulsar/test-topic -m "---------hello apache pulsar-------" -n 10
  4. Verify the results.

    1. ----- got message -----
    2. ---------hello apache pulsar-------
    3. ----- got message -----
    4. ---------hello apache pulsar-------
    5. ----- got message -----
    6. ---------hello apache pulsar-------
    7. ----- got message -----
    8. ---------hello apache pulsar-------
    9. ----- got message -----
    10. ---------hello apache pulsar-------
    11. ----- got message -----
    12. ---------hello apache pulsar-------
    13. ----- got message -----
    14. ---------hello apache pulsar-------
    15. ----- got message -----
    16. ---------hello apache pulsar-------
    17. ----- got message -----
    18. ---------hello apache pulsar-------
    19. ----- got message -----
    20. ---------hello apache pulsar-------
    21. Output from the producer side shows the messages have been produced successfully:
    22. 18:15:15.489 [main] INFO org.apache.pulsar.client.cli.PulsarClientTool - 10 messages successfully produced.