Last active
May 26, 2017 06:54
-
-
Save dnileshp/7c29b7ecd9612762854d228661f2a808 to your computer and use it in GitHub Desktop.
Kafka Producer in scala Kafka version : 0.10.1.0 scala : 2.11.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Properties | |
| import com.gta.config.GlobalVariables | |
| import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord} | |
| import scala.util.Random | |
| object KafkaProducer { | |
| def produceToKafka(message : String): Unit ={ | |
| val topic = "notifications" | |
| val rnd = new Random() | |
| val props = new Properties() | |
| props.put("bootstrap.servers", GlobalVariables.KAFKA_BROKER) | |
| props.put("client.id", "notificationProduce") | |
| props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer") | |
| props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer") | |
| val producer = new KafkaProducer[String, String](props) | |
| val t = System.currentTimeMillis() | |
| val data = new ProducerRecord[String, String](topic, message) | |
| //async | |
| //producer.send(data, (m,e) => {}) | |
| //sync | |
| producer.send(data) | |
| producer.close() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment