相关:java.util.concurrent.Future 的 scala.concurrent.Future 包装器
这来自我的另一个问题:
如何将akka流kafka(reactive-kafka)集成到akka http应用程序中?
我有一个 AKKA HTTP 应用程序,我想在我的路由中的 onComplete 函数中向 Kafka 发送消息/ProducerRecord,如下所示:
val producer : KafkaProducer = new KafkaProducer(producerSettings)
val routes : Route =
post {
entity(as[User]) { user =>
val createUser = userService.create(user)
onSuccess(createUser) {
case Invalid(y: NonEmptyList[Err]) =>
complete(BadRequest -> "invalid user")
case Valid(u: User) => {
val producerRecord =
new ProducerRecord[Array[Byte], String]("topic1","some message")
onComplete(producer.send(producerRecord)) { _ =>
complete(ToResponseMarshallable((StatusCodes.Created, u)))
}
}
}
}
}
但是,onComplete(producer send producerRecord)正在生成以下类型不匹配错误:
[错误] 发现:Future[org.apache.kafka.clients.producer.RecordMetadata](在 java.util.concurrent 中)[错误] 需要:Future[org.apache.kafka.clients.producer.RecordMetadata](在 scala.并发)[错误] onCompleteRecordMetadata { _ =>
有没有办法解决这个问题,也许通过使用 Producer 作为接收器(http://doc.akka.io/docs/akka-stream-kafka/current/producer.html#producer-as-a-sink)而不是java producer.send函数?