Java 如何使用MergeHub w/Flow.mapsync()
我有点困惑如何使用Java 如何使用MergeHub w/Flow.mapsync(),java,akka-stream,Java,Akka Stream,我有点困惑如何使用MergeHub 我正在设计一个使用flow.mapsync()的流图,其中给定函数创建另一个流图,然后使用Sink.ignore()运行它,并返回该CompletionStage作为flow.mapsync()等待的值。嵌套流将通过具体化MergeHub返回的Sink返回元素 问题是,我需要提供函数,当我创建顶级流图时,该函数将嵌套流启动到flow.mapsync(),但这要求它能够访问通过具体化MergeHub.of()的结果返回的具体化值。在启动流程图之前,如何获得具体化
MergeHub
我正在设计一个使用flow.mapsync()
的流图,其中给定函数创建另一个流图,然后使用Sink.ignore()
运行它,并返回该CompletionStage
作为flow.mapsync()
等待的值。嵌套流将通过具体化MergeHub
返回的Sink
返回元素
问题是,我需要提供函数
,当我创建顶级流图时,该函数将嵌套流启动到flow.mapsync()
,但这要求它能够访问通过具体化MergeHub.of()
的结果返回的具体化值。在启动流程图之前,如何获得具体化的值
我现在能看到的唯一方法是实现函数
,直到提供了接收器
(在启动顶级流程图之后),但这似乎很难实现
比如
class MapAsyncFunctor implements Function<T, CompletionStage<Done>> {...}
MapAsyncFunctor mapAsyncFunctor = new MapAsyncFunctor();
RunnableGraph<Sink<T>> graph = createGraph(mapAsyncFunctor);
Sink<T> sink = materializer.materialize(graph);
mapAsyncFunctor.setSink(sink); // Graph execution blocked in background in call to mapAsyncFunctor.apply() until this is done
类MapAsyncFunctor实现函数{…}
MapAsyncFunctor MapAsyncFunctor=新的MapAsyncFunctor();
RunnableGraph graph=createGraph(mapAsyncFunctor);
Sink-Sink=materializer.materialize(图);
mapAsyncFunctor.setSink(接收器);//在调用mapAsyncFunctor.apply()之前,图形执行在后台被阻止
编辑:我创建了以下类
public final class Channel<T>
{
private final Sink<T, NotUsed> m_channelIn;
private final Source<T, NotUsed> m_channelOut;
private final UniqueKillSwitch m_killSwitch;
public Channel(Class<T> in_class, Materializer in_materializer)
{
final Source<T, Sink<T, NotUsed>> source = MergeHub.of(in_class);
final Sink<T, Source<T, NotUsed>> sink = BroadcastHub.of(in_class);
final Pair<Pair<Sink<T, NotUsed>, UniqueKillSwitch>, Source<T, NotUsed>> matVals = in_materializer.materialize(source.viaMat(KillSwitches.single(), Keep.both()).toMat(sink, Keep.both()));
m_channelIn = matVals.first().first();
m_channelOut = matVals.second();
m_killSwitch = matVals.first().second();
}
public Sink<T, NotUsed> in()
{
return m_channelIn;
}
public Source<T, NotUsed> out()
{
return m_channelOut;
}
public void close()
{
m_killSwitch.shutdown();
}
}
公共最终课程频道
{
私人最终水槽m_channelIn;
私有最终源m_信道输出;
专用最终UniqueKillSwitch Mu killSwitch;
公共通道(类在类中,物化器在物化器中)
{
最终震源=合并集线器of(在_类中);
最终接收器=BroadcastHub.of(在_类中);
final Pair matVals=in_materializer.materialize(source.viaMat(KillSwitches.single(),Keep.both()).toMat(sink,Keep.both());
m_channelIn=matVals.first().first();
m_channelOut=matVals.second();
m_killSwitch=matVals.first().second();
}
公共水槽
{
返回m_channelIn;
}
公共资源输出()
{
返回m_channelOut;
}
公众假期结束()
{
m_killSwitch.shutdown();
}
}
这样我就可以得到一个源/接收器对,用于构建图形。这是个好主意吗?如果不显式关闭()我会“泄漏”这些通道吗
他们
对于我的用例,我只需要使用一次.out()。使用
MergeHub
时,您总是需要在执行任何其他操作之前具体化hub接收器
Sink<T, NotUsed> toConsumer = MergeHub.of(String.class, 16).to(consumer).run(materializer);
有关
MergeHub
的更多信息可以在中找到。我想使用MergeHub在我的流程图中创建一个循环,但这在这里并不起作用。也许我可以同时使用MergeHub和BroadcastHub,并创建一个源/接收器对,用于创建图表。。。
class MapAsyncFunctor implements Function<T, CompletionStage<Done>> {
private Sink<T, NotUsed> sink;
public MapAsyncFunctor(Sink<T, NotUsed> sink) {
this.sink = sink;
}
@Override
public CompletionStage<Done> apply(T t) { /* run substream into sink */ }
}
MapAsyncFunctor mapAsyncFunctor = new MapAsyncFunctor(toConsumer);
// run your flow with mapAsync on the above functor