All docs This doc

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Reverted from v. 17

The operation " publishMessage" operation allows you to publish messages to Kafka brokers using Kafka topics.

Code Block
languagexml
titlepublishMessages
<kafkaTransport.publishMessages>
    	<topic>topicName</topic>

   <partitionNo>partitionNo</partitionNo>
</kafkaTransport.publishMessages>
Properties
  • topic: The name of the topic.
  • partitionNo: The value
  • of the
  • partition Number for a particular
  • topic.

Sample scenario

Following is a sample scenario that demonstrates how to send messages to a Kafka broker using Kafka topics.

Prerequisites
  • Download and
  • install
  • install Apache Kafka. For more information,
  • see
  •  see Apache Kafka documentation. 
  • Copy the following client libraries from

  • the
  • the <KAFKA_HOME>/lib

  • directory to
  • directory to the <ESB_HOME>/repository/components/lib

  • directory
  •  directory.

  • Kafka
    • kafka_2.
  • 12-0.10
    • 9.2
  • .1.jarKafka
    • -
  • clients-
    • 0.
  • 10
    • 8.
  • 2
    • 1.1.jar
  • Metrics-core-3.2.2.jar

  • Scala
    • scala-library-2.
  • 12
    • 9.2.jar
  • Zkclient
    • zkclient-0.
  • 10
    • 3.jar
    • zookeeper-3.3.4
  • .10
    • .jar
    • metrics-core-2.2.0.jar
  • Run the following command to start the ZooKeeper server:

    Code Block
    bin/zookeeper-server-start.sh config/zookeeper.properties

    You will see the following log:
    Image Removed Image Added

  • Run the following command to start the Kafka server:

    Code Block
    bin/kafka-server-start.sh config/server.properties

    You will see the following log:
    Image Removed Image Added

Building the sample

...

Code Block
languagexml
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="KafkaTransport"
       transports="https http"
       startOnLoad="true"
       trace="disable">
   <description/>
   <target>
      <inSequence>
  <inSequence>       	<kafkaTransport.init>
        	<bootstrapServers>localhost:9092</bootstrapServers>         	<keySerializerClass>org.apache.kafka.common.serialization.StringSerializer</keySerializerClass><brokerList>localhost:9092</brokerList>
         </kafkaTransport.init>
         <kafkaTransport.publishMessages>
                                                                         
        	<valueSerializerClass>org.apache.kafka.common.serialization.StringSerializer</valueSerializerClass>                                    <topic>test</topic>
                                                   
     	</kafkaTransport.init>
     	<kafkaTransport.publishMessages>
        	<topic>test</topic>
     	</kafkaTransport.publishMessages>
  	</inSequence>
   </target>
</proxy>   

Following is a sample client(JMeter) screen that displays how to send a message:

Image RemovedImage Added

Analyzing the output

  • Run the following command to verify the messages:

    Code Block
    bin/kafka-console-consumer.sh --bootstrap-serverzookeeper localhost:90922181 --topic test --from-beginning

    You will see the following message content:

          {"name":"sample"} <name>sample</name>

         This demonstrates that the Kafka connector publishes messages to Kafka brokers.