Search Results

Apache Pulsar Python3 Quick Start

Contents

Getting Started

This quick start guide provides basic instructions for writing Apache Pulsar applications in Python 3 for TIBCO Cloud Messaging.

Building and Running samples

The Python client for Pulsar is not available on Windows

Follow the steps below to build and run the Python 3 samples:

  • Unzip pulsar-python3-samples.zip
  • (Optional) To preserve your default Python 3 environment use a virtual environment:
    • Create a virtual environment: python3 -m venv pulsar-python3-samples
    • Activate virtual environment: source pulsar-python3-samples/bin/activate
  • Change directories into pulsar-python3-samples
  • Upgrade pip: python3 -m pip install --upgrade pip
  • Install requirements: python3 -m pip install -r requirements.txt
  • Use -f option to specify the tcm-config.yaml.
  • Run samples: python3 pulsar_consumer.py -f <path-to>/tcm-config.yaml

Description of sample applications

Producer

The producer demonstrates connecting an Apache Pulsar Python 3 application to Cloud Messaging and publishing Pulsar messages on a topic:

usage: pulsar_producer.py [-h] [-f CONFIG_YAML] [-c COUNT] [-i INTERVAL] [-t TIMEOUT]
                          [-T TOPIC] [-id CLIENTID] [-schema]

optional arguments:
  -h, --help      show this help message and exit
  -f CONFIG_YAML  the path to a configuration yaml (default: tcm-config.yaml)
  -c COUNT        the number of messages to send before exiting (default: infinite)
  -i INTERVAL     the interval between sends (default: 1.0)
  -t TIMEOUT      stop after TIMEOUT seconds (default: never)
  -T TOPIC        topic to send on (default: demo_tcm)
  -id CLIENTID    client id (default: pulsar_producer.py)

Consumer

The consumer demonstrates connecting an Apache Pulsar Python 3 application to Cloud Messaging, creating a subscription, and receiving Pulsar messages on a topic:

usage: pulsar_consumer.py [-h] [-f CONFIG_YAML] [-c COUNT] [-t TIMEOUT] [-T TOPIC] [-S SUBNAME] [-id CLIENTID] [-latest] [-subtype {shared,exclusive,failover}]

optional arguments:
  -h, --help            show this help message and exit
  -f CONFIG_YAML        the path to a configuration yaml (default: tcm-config.yaml)
  -c COUNT              the number of messages to receive before exiting (default: infinite)
  -t TIMEOUT            stop after TIMEOUT seconds (default: never)
  -T TOPIC              topic to send on (default: demo_tcm)
  -S SUBNAME            subscription name (default: demo_tcm_sub)
  -id CLIENTID          client id (default: pulsar_consumer.py)
  -latest               set subscription position to 'latest'
  -subtype {shared,exclusive,failover}
                        set subscription type (default: shared)'

Connecting to TIBCO Cloud Messaging

The client configuration file contains all the information client applications need to securely connect to TIBCO Cloud Messaging. Generate the client configuration file using the roles REST API or user interface. Generate as many configuration files as needed for each Role.

Note: TIBCO Cloud Messaging samples require a client configuration file to run.

Connection Example

token = pulsar.AuthenticationToken(config.get('tcm_authentication_key'))
self.client = pulsar.Client(service_url=config.get('pulsar_url'), authentication=token)