Search Results

Apache Kafka® Java Quick Start

Contents

Getting Started

This quick start guide provides basic instructions for writing Apache Kafka applications in Java for TIBCO Cloud Messaging.

Building and running samples

Follow the steps below to build and run the Java samples:

  • Unzip kafka-java-samples.zip
  • Change directories into kafka-java-samples/
  • Build project: gradle build
  • Use -f option to specify the tcm-config.yaml.
  • Run samples: gradle KafkaConsumer:run --args="-f <full-path-to>/tcm-config.yaml"

Description of sample applications

Producer

The producer demonstrates connecting an Apache Kafka Java application to Cloud Messaging and publishing Kafka messages on a topic:

usage: KafkaProducer [-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 Kafka Java application to Cloud Messaging, creating a subscription, and receiving Kafka messages on a topic:

usage: KafkaConsumer [-c <count>] [-f <config-file>] [-group <group>] [-h] [-id <client-id>] [-latest] [-t <timeout>]
    [-topic <topic>]
 -c <count>         the number of messages received before exiting
 -f <config-file>   the path to a configuration yaml
 -group <group>     consumer group identifier
 -h,--help          this usage
 -id <client-id>    client identifier
 -latest            set offset position to 'latest'
 -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

Properties props = new Properties();
props.put("bootstrap.servers", options.get("kafka_broker"));
props.put("client.id", "KafkaProducer");
props.put("acks", "all");
props.put("security.protocol", "SASL_SSL");
props.put("sasl.mechanism", "PLAIN");
props.put("sasl.jaas.config", "org.apache.kafka.common.security.plain.PlainLoginModule required username=\""
    + options.get("kafka_username") + "\" password=\"token:"
    + options.get("tcm_authentication_key") + "\";");
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "MessageSerializer");

KafkaProducer<String, Message> producer = new org.apache.kafka.clients.producer.KafkaProducer<>(props);