简体   繁体   English

在 Akka Streams Sink 之后获取原始元素的参考?

[英]Get reference of original element after Akka Streams Sink?

I am trying to use the Amqp alpakka connector as a source and sink.我正在尝试使用 Amqp alpakka 连接器作为源和接收器。

Source<CommittableReadResult, NotUsed> AMQP_SOURCE  
      -> processAndGetResponse 
      -> Sink<ByteString, CompletionStage<Done>> AMQP_SINK 

I want to acknowledge the message obtained from the Amqp queue, after the Sink's operation is successful. Sink的操作成功后,我想确认从Amqp队列中获取的消息。 like this:像这样:

amqp_source(committableReadResult) 
    -> processAndGetResponse 
    -> amqp_sink 
    -> IfSinkOperationSuccess.Then(committableReadResult.ack())

How can I achieve this?我怎样才能做到这一点? I basically want to mark the message as acknowledge only after the sink operation is successful.我基本上只想在接收器操作成功后才将消息标记为确认。

In general, if you have a Sink which materializes into a Future[Done] (Scala API, I believe the Java equivalent is CompletionStage[Done] ), you can just run a substream with that Sink within a mapAsync stage.一般来说,如果你有一个Sink实现了Future[Done] (Scala API,我相信 Java 等价物是CompletionStage[Done] ),你可以在mapAsync阶段中使用该Sink运行一个子流。 Data will only pass through when successful.数据只有在成功时才会通过。

From a quick perusal of the Alpakka AMQP API, the Sink s all materialize that way, so that approach will work.通过快速阅读 Alpakka AMQP API, Sink都以这种方式实现,因此该方法将起作用。

Assuming that your doSomethingWithMessage function results in both the CommittableReadResult and a WriteMessage , something like the following in Scala will work:假设您的doSomethingWithMessage函数导致CommittableReadResultWriteMessage ,Scala 中的以下内容将起作用:

def doSomethingWithMessage(in: CommittableReadResult): (CommittableReadResult, WriteMessage) = ???

amqpSource
  .map(doSomethingWithMessage)
  .mapAsync(parallelism) { tup =>
    val (crr, wm) = tup
    val fut = Source.single(crr, wm).runWith(amqpSink)
    fut.flatMap(_ => crr.ack().map(_ => crr.message))
  }.runWith(Sink.ignore)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM