copyright | lastupdated | ||
---|---|---|---|
|
2017-03-02 |
{:new_window: target="_blank"} {:shortdesc: .shortdesc} {:screen: .screen} {:codeblock: .codeblock} {:pre: .pre}
{: #messagehub}
{{site.data.keyword.messagehub_full}} is a scalable, distributed, high throughput message bus to unite your on-premises and off-premises cloud technologies. {:shortdesc}
Using {{site.data.keyword.messagehub}}, you can complete the following tasks:
- Wire microservices together by using open protocols
- Connect stream data to analytics to realize powerful insights
- Feed event data to multiple applications to react in real time
For example, you can use {{site.data.keyword.messagehub}} to publish inventory changes, create a centralized bus for real-time data, or enable apps to offload work to back-end worker processes.
To get started with {{site.data.keyword.messagehub}} and start sending and receiving messages, use the Java™ sample. The sample shows how a producer sends messages to a consumer using a topic. The same sample program is used to consume messages and produce messages.
Complete the following steps: {: #getting_started_steps}
- Create a {{site.data.keyword.messagehub}} service instance:
a. Log in to {{site.data.keyword.Bluemix_notm}} using the web user interface.
b. Click CATALOG.
c. In the Application Services section, click {{site.data.keyword.messagehub}}. The {{site.data.keyword.messagehub}} service instance page opens.
d. Leave the service unbound in the Connect to menu and enter names for your service and your credentials. You can use the default values.
e. Click Create.
-
If you don't already have them, install the following prerequisites:
-
Clone the message-hub-samples git repository by running the following command from the command line:
git clone https://github.com/ibm-messaging/message-hub-samples.git
{: codeblock}
-
Change directory to the java console sample by running the following command:
cd message-hub-samples/kafka-java-console-sample
{: codeblock}
-
Run the following build commands:
gradle clean && gradle build
{: codeblock}
-
Start the consumer on your console by running the following command:
java -jar build/libs/kafka-java-console-sample-2.0.jar kafka_brokers_sasl kafka_admin_url api_key -consumer
{: codeblock}
The sample uses a topic named
kafka-java-console-sample-topic
. If the topic does not already exist, the sample creates it using the {{site.data.keyword.messagehub}} Administration API. To send and receive messages, the sample uses the Apache Kafka Java API.To find the values for kafka_brokers_sasl, kafka_admin_url, and api_key, go to your {{site.data.keyword.messagehub}} instance in {{site.data.keyword.Bluemix_notm}}, go to the Service Credentials tab, and select the Credentials that you want to use.
Important: kafka_brokers_sasl must be a single string and you must enclose it in quotes. For example:
"host1:port1,host2:port2"
{: codeblock}
We recommend using all the Kafka hosts listed in the Credentials that you selected.
-
Start the producer on your console by running the following command:
java -jar build/libs/kafka-java-console-sample-2.0.jar kafka_brokers_sasl kafka_admin_url api_key -producer
{: codeblock}
-
You should now see the messages sent by the producer appearing in the consumer. The following is some sample output:
[2016-11-30 17:30:53,492] INFO Running in local mode. (com.messagehub.samples.MessageHubConsoleSample) [2016-11-30 17:30:53,492] INFO Updating JAAS configuration (com.messagehub.samples.MessageHubConsoleSample) [2016-11-30 17:30:53,506] INFO Kafka Endpoints: kafka01-prod01.messagehub.services.us-south.bluemix.net:9093,kafka02-prod01.messagehub.services.us-south.bluemix.net:9093,kafka03-prod01.messagehub.services.us-south.bluemix.net:9093,kafka04-prod01.messagehub.services.us-south.bluemix.net:9093,kafka05-prod01.messagehub.services.us-south.bluemix.net:9093 (com.messagehub.samples.MessageHubConsoleSample) [2016-11-30 17:30:53,506] INFO Admin REST Endpoint: https://kafka-admin-prod01.messagehub.services.us-south.bluemix.net:443 (com.messagehub.samples.MessageHubConsoleSample) [2016-11-30 17:30:53,506] INFO Creating the topic kafka-java-console-sample-topic (com.messagehub.samples.MessageHubConsoleSample) (com.messagehub.samples.MessageHubConsoleSample)e :{} [2016-11-30 17:30:54,947] INFO Admin REST Listing Topics: [{"name":"kafka-java-console-sample-topic","partitions":1,"retentionMs":"86400000","markedForDeletion":false}] (com.messagehub.samples.MessageHubConsoleSample) [2016-11-30 17:30:55,952] INFO [Partition(topic = kafka-java-console-sample-topic, partition = 0, leader = 0, replicas = [0,1,4,], isr = [0,4,1,]] (com.messagehub.samples.ConsumerRunnable) [2016-11-30 17:30:55,953] INFO class com.messagehub.samples.ConsumerRunnable is starting. (com.messagehub.samples.ConsumerRunnable) [2016-11-30 17:30:57,023] INFO [Partition(topic = kafka-java-console-sample-topic, partition = 0, leader = 0, replicas = [0,1,4,], isr = [0,4,1,]] (com.messagehub.samples.ProducerRunnable) [2016-11-30 17:30:57,024] INFO MessageHubConsoleSample will run until interrupted. (com.messagehub.samples.MessageHubConsoleSample) [2016-11-30 17:30:57,024] INFO class com.messagehub.samples.ProducerRunnable is starting. (com.messagehub.samples.ProducerRunnable) [2016-11-30 17:30:58,018] INFO Message produced, offset: 0 (com.messagehub.samples.ProducerRunnable) [2016-11-30 17:30:58,956] INFO No messages consumed (com.messagehub.samples.ConsumerRunnable) [2016-11-30 17:31:00,301] INFO Message consumed: ConsumerRecord(topic = kafka-java-console-sample-topic, partition = 0, offset = 1, CreateTime = 1480527060022, checksum = 1906962734, serialized key size = 3, serialized value size = 25, key = key, value = This is a test message #1) (com.messagehub.samples.ConsumerRunnable) [2016-11-30 17:31:00,397] INFO Message produced, offset: 1 (com.messagehub.samples.ProducerRunnable) [2016-11-30 17:31:02,550] INFO Message consumed: ConsumerRecord(topic = kafka-java-console-sample-topic, partition = 0, offset = 2, CreateTime = 1480527062401, checksum = 3801731428, serialized key size = 3, serialized value size = 25, key = key, value = This is a test message #2) (com.messagehub.samples.ConsumerRunnable)
{: codeblock}
-
The sample runs indefinitely until you stop it. To stop the process, run a command like the following:
Ctrl+C
To find out more about running a {{site.data.keyword.messagehub}} sample using Python, see Python console sample application {:new_window}. You can also find samples that demonstrate other APIs and features at {{site.data.keyword.messagehub}} samples {:new_window}.
To watch a video that walks you through getting a Java sample to run against {{site.data.keyword.messagehub}}, see {{site.data.keyword.messagehub}} - Getting started with IBM's Kafka in the cloud {:new_window}.