Skip to content

Instantly share code, notes, and snippets.

@dnileshp
Last active May 26, 2017 06:54
Show Gist options
  • Select an option

  • Save dnileshp/7c29b7ecd9612762854d228661f2a808 to your computer and use it in GitHub Desktop.

Select an option

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