Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在阿克卡溪流下沉后获得原始元素的参考?_Java_Scala_Akka_Akka Stream_Alpakka - Fatal编程技术网

Java 在阿克卡溪流下沉后获得原始元素的参考?

Java 在阿克卡溪流下沉后获得原始元素的参考?,java,scala,akka,akka-stream,alpakka,Java,Scala,Akka,Akka Stream,Alpakka,我正在尝试使用Amqp alpakka连接器作为源和汇 Source<CommittableReadResult, NotUsed> AMQP_SOURCE -> processAndGetResponse -> Sink<ByteString, CompletionStage<Done>> AMQP_SINK 我怎样才能做到这一点?基本上,我只想在sink操作成功后将消息标记为acknowledge。通常,如果您

我正在尝试使用Amqp alpakka连接器作为源和汇

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

我怎样才能做到这一点?基本上,我只想在sink操作成功后将消息标记为acknowledge。

通常,如果您有一个
sink
,它具体化为
未来的[Done]
(Scala API,我相信Java等价物是
CompletionStage[Done]
),您只需在
mapsync
阶段内使用该
Sink
运行子流即可。数据只有在成功时才会通过

快速阅读alpakkaamqpapi后,
Sink
s都是以这种方式实现的,因此这种方法可以工作

假设您的
doSomethingWithMessage
函数同时产生
CommitteableReadResult
WriteMessage
,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)

谢谢你的回复。这很有帮助。在这种情况下,是否会重新初始化接收器,即为流中的每个元素创建到amqp服务器的新连接?如果您使用akka stream
v2.5.20+
,我建议查看
SourceWithContext
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)