Id: tutorials-produce-consume title: Produce and consume messages sidebar_label: “Produce and consume messages” description: Learn how to produce and consume messages in Pulsar.


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.

    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.

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

    ----- got message -----
    ---------hello apache pulsar-------
    ----- got message -----
    ---------hello apache pulsar-------
    ----- got message -----
    ---------hello apache pulsar-------
    ----- got message -----
    ---------hello apache pulsar-------
    ----- got message -----
    ---------hello apache pulsar-------
    ----- got message -----
    ---------hello apache pulsar-------
    ----- got message -----
    ---------hello apache pulsar-------
    ----- got message -----
    ---------hello apache pulsar-------
    ----- got message -----
    ---------hello apache pulsar-------
    ----- got message -----
    ---------hello apache pulsar-------
    
    Output from the producer side shows the messages have been produced successfully:
    18:15:15.489 [main] INFO  org.apache.pulsar.client.cli.PulsarClientTool - 10 messages successfully produced.
    

Related Topics