本文整理汇总了Scala中org.apache.kafka.clients.producer.Callback类的典型用法代码示例。如果您正苦于以下问题:Scala Callback类的具体用法?Scala Callback怎么用?Scala Callback使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Callback类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: sendToKafkaWithNewProducer
//设置package包名称以及导入依赖的类
package pl.touk.nussknacker.engine.kafka
import org.apache.kafka.clients.producer.{Callback, KafkaProducer, ProducerRecord, RecordMetadata}
import scala.concurrent.{Future, Promise}
import scala.util.{Failure, Success, Try}
trait EspSimpleKafkaProducer {
val kafkaConfig: KafkaConfig
def sendToKafkaWithNewProducer(topic: String, key: Array[Byte], value: Array[Byte]): Future[RecordMetadata] = {
var producer: KafkaProducer[Array[Byte], Array[Byte]] = null
try {
producer = createProducer()
sendToKafka(topic, key, value)(producer)
} finally {
if (producer != null) {
producer.close()
}
}
}
//method with such signature already exists in "net.cakesolutions" %% "scala-kafka-client" % "0.9.0.0" but I struggled to add this dependency...
def sendToKafka(topic: String, key: Array[Byte], value: Array[Byte])(producer: KafkaProducer[Array[Byte], Array[Byte]]): Future[RecordMetadata] = {
val promise = Promise[RecordMetadata]()
producer.send(new ProducerRecord(topic, key, value), producerCallback(promise))
promise.future
}
def createProducer(): KafkaProducer[Array[Byte], Array[Byte]] = {
new KafkaProducer[Array[Byte], Array[Byte]](KafkaEspUtils.toProducerProperties(kafkaConfig))
}
private def producerCallback(promise: Promise[RecordMetadata]): Callback =
new Callback {
override def onCompletion(metadata: RecordMetadata, exception: Exception): Unit = {
val result = if (exception == null) Success(metadata) else Failure(exception)
promise.complete(result)
}
}
}
示例2: RecordCallback
//设置package包名称以及导入依赖的类
package articlestreamer.kafka
import org.apache.kafka.clients.producer.{Callback, RecordMetadata}
class RecordCallback extends Callback {
override def onCompletion(metadata: RecordMetadata, ex: Exception) = {
if (ex != null) {
handleException(ex)
} else {
println(s"Successfully sent message : $metadata")
}
}
def handleException(exception: Exception): Unit = {
Console.err.println(s"Error while attempting to send message : $exception")
}
}
示例3: SimpleProducer
//设置package包名称以及导入依赖的类
package de.choffmeister.microserviceutils.kafka
import akka.NotUsed
import akka.kafka.ProducerMessage.Result
import akka.kafka.{ProducerMessage, ProducerSettings}
import org.apache.kafka.clients.producer.{Callback, ProducerRecord, RecordMetadata}
import scala.concurrent.{Future, Promise}
class SimpleProducer[K, V](producerSettings: ProducerSettings[K, V]) {
private val producer = producerSettings.createKafkaProducer()
def send(record: ProducerRecord[K, V]): Future[Result[K, V, NotUsed]] = {
val promise = Promise[Result[K, V, NotUsed]]
val callback = new Callback {
override def onCompletion(metadata: RecordMetadata, exception: Exception): Unit = {
Option(exception) match {
case Some(err) => promise.failure(err)
case None => promise.success(Result(metadata, ProducerMessage.Message(record, NotUsed)))
}
}
}
producer.send(record, callback)
promise.future
}
}
示例4: Bidder
//设置package包名称以及导入依赖的类
package bidding.client.console
import org.apache.kafka.clients.producer.{Callback, KafkaProducer, ProducerRecord, RecordMetadata}
class Bidder(producer: KafkaProducer[String, String], itemId: String, startPrice: BigDecimal) {
private var lastPrice: BigDecimal = startPrice
private val callback = new Callback {
override def onCompletion(metadata: RecordMetadata, exception: Exception): Unit = {
println("published: " + metadata.toString)
}
}
def bid(): Unit = {
val producerRecord = new ProducerRecord("my-replicated-topic", itemId, lastPrice.toString())
producer.send(producerRecord, callback)
lastPrice += BigDecimal(scala.util.Random.nextDouble * 2)
}
}
示例5: RecordCallback
//设置package包名称以及导入依赖的类
package articlestreamer.shared.kafka
import com.typesafe.scalalogging.LazyLogging
import org.apache.kafka.clients.producer.{Callback, RecordMetadata}
class RecordCallback extends Callback with LazyLogging {
override def onCompletion(metadata: RecordMetadata, ex: Exception) = {
if (ex != null) {
handleException(ex)
} else {
logger.info(s"Successfully sent message : $metadata")
}
}
private def handleException(exception: Exception): Unit = {
logger.error("Error while attempting to send message", exception)
}
}