Skip to main content
This document provides examples about how to use the Pulsar Java client to connect to a Pulsar cluster through a token or an OAuth2 credential file.
This document assumes that you have created a service account, and have granted the service account produce and consume permissions to the namespace for the target topic.

Connect to a Pulsar cluster using a token

This section describes how to connect to you Pulsar cluster using a token.

Prerequisites

Steps

To connect to your Pulsar cluster using a token, follow these steps.
  1. Connect to the Pulsar cluster.
    Set the SERVICE_URL and AUTH_PARAMS parameters based on the descriptions in the prepare to connect to a Pulsar cluster user guide.
  2. Create a Java consumer and use the Java consumer to consume messages.
  3. Create a Java producer and use the Java producer to produce messages.

Connect to a Pulsar cluster using an OAuth2 credential file

To connect to your Pulsar cluster using an OAuth2 credential file, follow these steps.
  1. Generate the App credentials by following similar instructions in configure OAuth2 authentication.
  2. Save the App credentials into an OAuth2 credential file.
  3. Connect to your Pulsar cluster through the OAuth2 credential file.
    • issuerUrl: the URL of your OAuth2 identity provider.
    • credentialsUrl: the path to your OAuth2 credential file.
    • audience: the audience of your Pulsar cluster.
    • serviceUrl: the URL of your Pulsar cluster.
  4. Create a Java consumer and use the Java consumer to consume messages. You can create and configure the Java consumer to consume messages using the OAuth2 credential file. Or, you can define a Base64 String when creating the Java consumer. Then, the consumer can extract the client ID and client Secret from the OAuth2 credential file and use them to consume messages.
    • Using the OAuth2 credential file
    • Using the OAuth2 client ID and client Secret
  5. Create a Java producer and use the Java producer to produce messages. You can create and configure the Java producer to produce messages using the OAuth2 credential file. Or, you can define a Base64 String when creating the Java producer. Then, the producer can extract the client ID and client Secret from the OAuth2 credential file and use them to produce messages.
    • Using the OAuth2 credential file
    • Using the OAuth2 client ID and client Secret