|
| 1 | +package com.contactsunny.poc.SimpleKafkaProducer; |
| 2 | + |
| 3 | +import com.contactsunny.poc.SimpleKafkaProducer.kafkaConsumers.SimpleKafkaConsumer; |
| 4 | +import org.apache.kafka.clients.producer.KafkaProducer; |
| 5 | +import org.apache.kafka.clients.producer.ProducerRecord; |
| 6 | +import org.apache.log4j.Logger; |
| 7 | +import org.codehaus.jettison.json.JSONException; |
| 8 | +import org.codehaus.jettison.json.JSONObject; |
| 9 | +import org.springframework.beans.factory.annotation.Value; |
| 10 | +import org.springframework.boot.CommandLineRunner; |
| 11 | +import org.springframework.boot.SpringApplication; |
| 12 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 13 | + |
| 14 | +import java.util.Properties; |
| 15 | + |
| 16 | +@SpringBootApplication |
| 17 | +public class SimpleKafkaProducerApplication implements CommandLineRunner { |
| 18 | + |
| 19 | + @Value("${kafka.topic.thetechcheck}") |
| 20 | + private String theTechCheckTopicName; |
| 21 | + |
| 22 | + @Value("${kafka.bootstrap.servers}") |
| 23 | + private String kafkaBootstrapServers; |
| 24 | + |
| 25 | + @Value("${zookeeper.groupId}") |
| 26 | + private String zookeeperGroupId; |
| 27 | + |
| 28 | + @Value("${zookeeper.host}") |
| 29 | + String zookeeperHost; |
| 30 | + |
| 31 | + private static final Logger logger = Logger.getLogger(SimpleKafkaProducerApplication.class); |
| 32 | + |
| 33 | + public static void main( String[] args ) { |
| 34 | + SpringApplication.run(SimpleKafkaProducerApplication.class, args); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void run(String... args) { |
| 39 | + |
| 40 | + /* |
| 41 | + * Defining producer properties. |
| 42 | + */ |
| 43 | + Properties props = new Properties(); |
| 44 | + props.put("bootstrap.servers", kafkaBootstrapServers); |
| 45 | + props.put("acks", "all"); |
| 46 | + props.put("retries", 0); |
| 47 | + props.put("batch.size", 16384); |
| 48 | + props.put("linger.ms", 1); |
| 49 | + props.put("buffer.memory", 33554432); |
| 50 | + props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); |
| 51 | + props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); |
| 52 | + |
| 53 | + /* |
| 54 | + Creating a Kafka Producer object with the configuration above. |
| 55 | + */ |
| 56 | + KafkaProducer<String, String> producer = new KafkaProducer<>(props); |
| 57 | + |
| 58 | + /* |
| 59 | + The sendTestMessagesToKafka method will generate some random test messages |
| 60 | + and send them to Kafka. |
| 61 | + */ |
| 62 | + sendTestMessagesToKafka(producer); |
| 63 | + |
| 64 | + /* |
| 65 | + Now that we've produced some test messages, let's see how to consume them using a Kafka consumer object. |
| 66 | + */ |
| 67 | + /* |
| 68 | + * Defining Kafka consumer properties. |
| 69 | + */ |
| 70 | + Properties consumerProperties = new Properties(); |
| 71 | + consumerProperties.put("bootstrap.servers", kafkaBootstrapServers); |
| 72 | + consumerProperties.put("group.id", zookeeperGroupId); |
| 73 | + consumerProperties.put("zookeeper.session.timeout.ms", "6000"); |
| 74 | + consumerProperties.put("zookeeper.sync.time.ms","2000"); |
| 75 | + consumerProperties.put("auto.commit.enable", "false"); |
| 76 | + consumerProperties.put("auto.commit.interval.ms", "1000"); |
| 77 | + consumerProperties.put("consumer.timeout.ms", "-1"); |
| 78 | + consumerProperties.put("max.poll.records", "1"); |
| 79 | + consumerProperties.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); |
| 80 | + consumerProperties.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); |
| 81 | + |
| 82 | + /* |
| 83 | + * Creating a thread to listen to the kafka topic |
| 84 | + */ |
| 85 | + Thread kafkaConsumerThread = new Thread(() -> { |
| 86 | + logger.info("Starting Kafka consumer thread."); |
| 87 | + |
| 88 | + SimpleKafkaConsumer simpleKafkaConsumer = new SimpleKafkaConsumer( |
| 89 | + theTechCheckTopicName, |
| 90 | + consumerProperties |
| 91 | + ); |
| 92 | + |
| 93 | + simpleKafkaConsumer.runSingleWorker(); |
| 94 | + }); |
| 95 | + |
| 96 | + /* |
| 97 | + * Starting the first thread. |
| 98 | + */ |
| 99 | + kafkaConsumerThread.start(); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Function to send some test messages to Kafka. |
| 104 | + * We'll get the Kafka producer object as a parameter to this function. |
| 105 | + * We'll generate some test messages, both simple strings and JSON objects, in a couple of |
| 106 | + * loops inside the function. We'll send these test messages to the topic in Kafka. |
| 107 | + * |
| 108 | + * @param producer The Kafka producer we created in the run() method earlier. |
| 109 | + */ |
| 110 | + private void sendTestMessagesToKafka(KafkaProducer<String, String> producer) { |
| 111 | + /* |
| 112 | + Creating a loop which iterates 10 times, from 0 to 9, and sending a |
| 113 | + simple message to Kafka. |
| 114 | + */ |
| 115 | + for (int index = 0; index < 10; index++) { |
| 116 | + sendKafkaMessage("The index is now: " + index, producer, theTechCheckTopicName); |
| 117 | + } |
| 118 | + |
| 119 | + /* |
| 120 | + Creating a loop which iterates 10 times, from 0 to 9, and creates an instance of JSONObject |
| 121 | + in each iteration. We'll use this simple JSON object to illustrate how we can send a JSON |
| 122 | + object as a message in Kafka. |
| 123 | + */ |
| 124 | + for (int index = 0; index < 10; index++) { |
| 125 | + |
| 126 | + /* |
| 127 | + We'll create a JSON object which will have a bunch of fields, and another JSON object, |
| 128 | + which will be nested inside the first JSON object. This is just to demonstrate how |
| 129 | + complex objects could be serialized and sent to topics in Kafka. |
| 130 | + */ |
| 131 | + JSONObject jsonObject = new JSONObject(); |
| 132 | + JSONObject nestedJsonObject = new JSONObject(); |
| 133 | + |
| 134 | + try { |
| 135 | + /* |
| 136 | + Adding some random data into the JSON object. |
| 137 | + */ |
| 138 | + jsonObject.put("index", index); |
| 139 | + jsonObject.put("message", "The index is now: " + index); |
| 140 | + |
| 141 | + /* |
| 142 | + We're adding a field in the nested JSON object. |
| 143 | + */ |
| 144 | + nestedJsonObject.put("nestedObjectMessage", "This is a nested JSON object with index: " + index); |
| 145 | + |
| 146 | + /* |
| 147 | + Adding the nexted JSON object to the main JSON object. |
| 148 | + */ |
| 149 | + jsonObject.put("nestedJsonObject", nestedJsonObject); |
| 150 | + |
| 151 | + } catch (JSONException e) { |
| 152 | + logger.error(e.getMessage()); |
| 153 | + } |
| 154 | + |
| 155 | + /* |
| 156 | + We'll now serialize the JSON object we created above, and send it to the same topic in Kafka, |
| 157 | + using the same function we used earlier. |
| 158 | + You can use any JSON library for this, just make sure it serializes your objects properly. |
| 159 | + A popular alternative to the one I've used is Gson. |
| 160 | + */ |
| 161 | + sendKafkaMessage(jsonObject.toString(), producer, theTechCheckTopicName); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Function to send a message to Kafka |
| 167 | + * @param payload |
| 168 | + * @param producer |
| 169 | + * @param topic |
| 170 | + */ |
| 171 | + private static void sendKafkaMessage(String payload, |
| 172 | + KafkaProducer<String, String> producer, |
| 173 | + String topic) |
| 174 | + { |
| 175 | + logger.info("Sending Kafka message: " + payload); |
| 176 | + producer.send(new ProducerRecord<>(topic, payload)); |
| 177 | + } |
| 178 | +} |
0 commit comments