我正在使用Akka Streams Kafka库与Kafka经纪人进行互动。我有一个UnBounded缓冲区的以下流定义:
def producerStream[T: MessageType](producerProperties: Map[String, String]) = {
val streamSource = source[T](producerProperties)
val streamFlow = flow[T](producerProperties)
val streamSink = sink(producerProperties)
streamSource.via(streamFlow).to(streamSink).run()
}
我从Actor实例中调用producerStream。我有一个类型的消息:
case class InitiateStream[T](publisher: ActorRef, anyMessage: T)
在我的演员中,我有以下接收块:
override def receive: Receive = super.receive orElse {
case StartProducerStream(publisherActor, DefaultMessage) =>
producerStream[DefaultMessage](cfg.producerProps)
// context.become(active)
case other => println(s"SHIT !! Got unknown message: $other")
}
publisherActor最终将获取应该推送到相应Kafka主题的消息。
现在我的问题是,我是否需要打扰关闭producerStream?
我正在考虑在启动流时执行context.become(active(producerStream)),在活动方法中,我将根据DestroyStream消息处理流终止。这需要吗?你们觉得怎么样?
答案 0 :(得分:1)
如果您使用Akka Kafka使用者,则只要主题中有可用的事件,源就会无限期地发出事件。你为什么需要关闭制片人?
我建议打电话给以下人员开始你的来源:
def producerSource[T: MessageType](producerProperties: Map[String, String]) = {
val streamSource = source[T](producerProperties)
val streamFlow = flow[T](producerProperties)
val streamSink = sink(producerProperties)
streamSource.via(streamFlow)
}
def startStream[A](source: Source[A, NotUsed])(
implicit am: ActorMaterializer): (UniqueKillSwitch, Future[Done]) = {
source
.viaMat(KillSwitches.single)(Keep.right)
.toMat(Sink.ignore)(Keep.both)
.run
}
var streamHandler : Option[UniqueKillSwitch] = None
override def receive: Receive = super.receive orElse {
case StartProducerStream(publisherActor, DefaultMessage) =>
streamHandler = Some(producerStream[DefaultMessage](cfg.producerProps))
context.become(active)
case other => println(s"SHIT !! Got unknown message: $other")
}
和活跃行为:
val active: PartialFunction[Any, Unit] =
case DestroyStream =>
if(streamHandler.isDefined)
streamHandler.shutdown