IBM Cloud Docs
Using the Kafka Java client

Using the Kafka Java client

The Java™ Kafka API sample is an example producer and consumer that is written in Java, which directly uses the Kafka API. You can run this sample locally or in IBM Cloud®.

For optimal performance with the Java client, you are recommended to use version 2.6.0, or later. For more information, see known restriction about Java Kafka calls.

The sample code is in the event-streams-samples GitHub project. Although the sample uses the Kafka API to send and receive messages, the sample uses the Event Streams Administration API to create the topic it sends messages to and receives messages from.

For more information about the setup and running of the sample, see the README.md.

For a detailed walkthrough of how to run the sample, see Getting started with Event Streams.

How to use, download, and run the Liberty for Java sample

The IBM® Liberty for Java™ for IBM Cloud® sample implements a simple application that is deployed onto the Liberty runtime. The application uses the Kafka API for Event Streams to produce and consume messages. The application also serves up a web front end that you can use for administration.

You can find the sample code in the event-streams-samples GitHub project.

Using the sasl.jaas.config property

If you use a Kafka client at 0.10.2.1 or later, you can use the sasl.jaas.config property for client configuration instead of a JAAS file. To connect to Event Streams, set sasl.jaas.config as in the following example:

    sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required \
    username="USERNAME" \
    password="PASSWORD";</code>

USERNAME and PASSWORD are the values from your Event Streams Service Credentials tab in IBM Cloud.

If you use sasl.jaas.config, clients that run in the same JVM can use different credentials. For more information, see Configuring Kafka clients.

For an earlier Kafka client, you must use a JAAS configuration file to specify the credentials. This mechanism is less convenient, therefore use the sasl.jaas.config property instead.

Migrating a Kafka client from version 0.9.X or 0.10.X to later client versions

If you use the Java clients, you can use the publicly available Kafka clients version 0.10 or later.

You are encouraged to move from version 0.9.X to the latest version. You can download a Kafka client from https://kafka.apache.org/downloads.

Migrating a Kafka client to version 0.10.2.X or later versions

From version 0.10.2, you can configure SASL authentication directly in the client's properties instead of using a JAAS file. This simplification lets you to run multiple clients in the same JVM by using different sets of credentials, which is not possible with a JAAS file.

Complete the following steps:

  1. Delete the JAAS file. The JVM property java.security.auth.login.config=<PATH TO JAAS> is also no longer required.

  2. Add the following to the client's properties:

    sasl.mechanism=PLAIN
    sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="USERNAME" password="PASSWORD";
    

    USERNAME and PASSWORD are the values from your Event Streams Service Credentials tab in IBM Cloud.