[英]Sending actorRefWithAck inside stream
我正在使用此線程的答案,因為我需要特別對待第一個元素。 問題是,我需要將此數據發送到另一個Actor或在本地持久化(這不是可能的)。
因此,我的信息流看起來像這樣:
val flow: Flow[Message, Message, (Future[Done], Promise[Option[Message]])] = Flow.fromSinkAndSourceMat(
Flow[Message].mapAsync[Trade](1) {
case TextMessage.Strict(text) =>
Unmarshal(text).to[Trade]
case streamed: TextMessage.Streamed =>
streamed.textStream.runFold("")(_ ++ _).flatMap(Unmarshal(_).to[Trade])
}.groupBy(pairs.size, _.s).prefixAndTail(1).flatMapConcat {
case (head, tail) =>
// sending first element here
val result = Source(head).to(Sink.actorRefWithAck(
ref = actor,
onInitMessage = Init,
ackMessage = Ack,
onCompleteMessage = "done"
)).run()
// some kind of operation on the result
Source(head).concat(tail)
}.mergeSubstreams.toMat(sink)(Keep.right),
Source.maybe[Message])(Keep.both)
這是一個好習慣嗎? 會產生意想不到的后果嗎? 不幸的是,我無法在流內部調用persist
,因此我想將此數據發送到外部系統。
您當前的方法完全不使用result
,因此一種更簡單的選擇是觸發並忘記向演員發送的第一條Message
:
groupBy(pairs.size, _.s).prefixAndTail(1).flatMapConcat {
case (head, tail) =>
// sending first element here
actor ! head.head
Source(head).concat(tail)
}
這樣,參與者就不必擔心處理Init
和發送Ack
消息,而只需要考慮持久化Message
實例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.