All docs This doc

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The operation "publishMessage" 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
  • 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:

  • 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:

Building the sample

Following is a sample proxy service that illustrates how you can connect to a Kakfa broker with the init operation and use the publishMessages operation to publish the messages with the topic.

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

Analyzing the output

  • Run the following command to verify the messages:

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

    You will see the following message content:

          {"name":"sample"}

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