Apache Pulsar™ Java Quick Start
Contents
- Getting Started
- Building and running samples
- Description of sample applications
- Connecting to TIBCO Cloud Messaging
- Related Links
Getting Started
This quick start guide provides basic instructions for writing Apache Pulsar applications in Java for TIBCO Cloud™ Messaging.
- Download the Pulsar sample applications for Java.
- Gradle 6.7+ and JDK 1.8 are required to build and run the sample applications.
- Download a client configuration file using the roles REST API or user interface.
- Read the official Pulsar Java client guide.
- Review the Pulsar javadoc.
Building and running samples
Follow the steps below to build and run the Java samples:
- Unzip
pulsar-java-samples.zip
- Change directories into
pulsar-java-samples
- Build project:
gradle build
- Use
-f
option to specify thetcm-config.yaml
. - Run samples:
gradle PulsarConsumer:run --args="-f <full-path-to>/tcm-config.yaml"
Description of sample applications
Producer
The producer demonstrates connecting an Apache Pulsar Java application to Cloud Messaging and publishing Pulsar messages on a topic:
usage: PulsarProducer [-c <count>] [-f <config-file>] [-h] [-i <interval>] [-id <client-id>] [-t <timeout>]
[-topic <topic>]
-c <count> the number of messages to send before exiting
-f <config-file> the path to a configuration yaml
-h,--help this usage
-i <interval> the interval between sends (milliseconds)
-id <client-id> client identifier
-t <timeout> the duration before exiting (seconds)
-topic <topic> topic to send on
Consumer
The consumer demonstrates connecting an Apache Pulsar Java application to Cloud Messaging, creating a subscription, and receiving Pulsar messages on a topic:
usage: PulsarConsumer [-c <count>] [-f <config-file>] [-h] [-id <client-id>] [-latest] [-sub <subscription-name>]
[-subtype <subtype>] [-t <timeout>] [-topic <topic>]
-c <count> the number of messages received before exiting
-f <config-file> the path to a configuration yaml
-h,--help this usage
-id <client-id> client identifier
-latest set subscription position to 'latest'
-sub <subscription-name> subscription name
-subtype <subtype> set subscription type to 'shared', 'exclusive' or 'failover'
-t <timeout> the duration before exiting (seconds)
-topic <topic> the topic to consume
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
client = PulsarClient.builder().serviceUrl(options.get("pulsar_url"))
.connectionTimeout(10, TimeUnit.SECONDS)
.authentication(AuthenticationFactory.token(options.get("tcm_authentication_key"))).build();