I am trying to use the Amqp alpakka connector as a source and sink.
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. 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. 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.
Assuming that your doSomethingWithMessage
function results in both the CommittableReadResult
and a WriteMessage
, something like the following in Scala will work:
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.