| copyright |
|
||
|---|---|---|---|
| lastupdated | 2018-06-22 |
{:new_window: target="_blank"} {:shortdesc: .shortdesc} {:screen: .screen} {:codeblock: .codeblock} {:pre: .pre}
{: #kafka_using}
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 {{site.data.keyword.Bluemix_short}}.
The sample code is in the event-streams-samples GitHub project {:new_window}. Although the sample uses
the Kafka API to send and receive messages, the sample uses the {{site.data.keyword.messagehub}} 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 {:new_window}.
For a detailed walkthrough of how to run the sample, see Getting started with {{site.data.keyword.messagehub}}.
{: #liberty_sample notoc}
The Liberty for Java sample implements a simple application that is deployed onto the Liberty runtime. The application uses the Kafka API for {{site.data.keyword.messagehub}} 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 {:new_window}.
{: #sasl_prop notoc}
If you're using 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 {{site.data.keyword.messagehub}}, set sasl.jaas.config as follows:
sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required \
username="USERNAME" \
password="PASSWORD";
{:codeblock}
where USERNAME and PASSWORD are the values from your {{site.data.keyword.messagehub}} Service Credentials tab in {{site.data.keyword.Bluemix_notm}}.
If you use sasl.jaas.config, clients running in the same JVM can use different credentials. For more information, see
Configuring Kafka clients {:new_window}
For an earlier Kafka client, you must use a JAAS configuration file to specify the credentials. This mechanism is less convenient therefore we recommend using the sasl.jaas.config property instead.
{: #kafka_migrate}
If you're using the Java clients, you can use
the publicly available Kafka clients at 0.10 or later. You are strongly encouraged to move from 0.9.X to the
latest version. You can download a Kafka client from
https://kafka.apache.org/downloads {:new_window}
From 0.10.2, you can configure SASL authentication directly in the client's properties instead of using a JAAS file. This simplicifcation allows you to run multiple clients in the same JVM using different sets of credentials, which is not possible with a JAAS file.
Complete the following steps:
-
Delete the JAAS file. Note that the JVM property java.security.auth.login.config= is also no longer required.
-
If you're migrating from 0.9.X, delete the {{site.data.keyword.messagehub}} login jar module.
-
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";where USERNAME and PASSWORD are the values from your {{site.data.keyword.messagehub}} Service Credentials tab in {{site.data.keyword.Bluemix_notm}}.
Complete the following steps:
-
Delete the {{site.data.keyword.messagehub}} login jar module.
-
Change your
jaas.conffile to the following:KafkaClient { org.apache.kafka.common.security.plain.PlainLoginModule required serviceName="kafka" username="USERNAME" password="PASSWORD"; };{: codeblock}
where USERNAME and PASSWORD are the values from your {{site.data.keyword.messagehub}} Service Credentials tab in {{site.data.keyword.Bluemix_notm}}.
-
Add this line to your consumer and producer properties:
sasl.mechanism=PLAIN