假设定义了如下这样的Akka流:
def tee = {
var writer: Writer = ???
Flow.fromFunction[String, String] { msg =>
writer.write(msg)
msg
}
}
上游完成后,需要冲洗关闭写入器。是否有一种方法可以不使用https://doc.akka.io/docs/akka/current/stream/stream-customize.html所述的GraphStageLogic
等?
答案 0 :(得分:1)
如果不将Flow转换成Sink,就不可能做到这一点。
如果可以使用接收器,请执行以下操作
java.lang.IllegalAccessException: access to public member failed:
my.IInterface.someDefaultMethod[Ljava.lang.Object;@172aedbe/invokeSpecial, from my.IInterface/2 (unnamed module @627551fb)
at java.base/java.lang.invoke.MemberName.makeAccessException(MemberName.java:942)
at java.base/java.lang.invoke.MethodHandles$Lookup.checkAccess(MethodHandles.java:2206)
at java.base/java.lang.invoke.MethodHandles$Lookup.checkMethod(MethodHandles.java:2146)
at java.base/java.lang.invoke.MethodHandles$Lookup.getDirectMethodCommon(MethodHandles.java:2290)
at java.base/java.lang.invoke.MethodHandles$Lookup.getDirectMethodNoSecurityManager(MethodHandles.java:2283)
at java.base/java.lang.invoke.MethodHandles$Lookup.unreflectSpecial(MethodHandles.java:1798)
at my.SomeService.invoke(SomeService.java:305)
在 def tee = {
val writer: Writer = new StringWriter()
Sink
.foreach[String] { msg =>
writer.write(msg)
}
.mapMaterializedValue(_.map { done =>
writer.close()
done
})
}
的帮助下,可以完成类似的操作
akka.stream.scaladsl.StreamConverters