简体   繁体   English

如何通过Java库在Akka中创建一个自定义的Uniform Fan Out Shape

[英]How to create a custom Uniform Fan Out Shape in Akka through the Java library

So I am building an Akka custom component to learn the framework.所以我正在构建一个 Akka 自定义组件来学习框架。 I need to create a UnifromFanOut shape custom component.我需要创建一个 UnifromFanOut 形状自定义组件。 So I tried to extend the shape but the issue is how do I create the shape with 1 input and 3 outputs.所以我尝试扩展形状,但问题是如何创建具有 1 个输入和 3 个输出的形状。 The class requires a seq object which is a part of Scala but I am a bit confused on whether the whole shape itself is correct. class 需要一个 seq object,它是 Scala 的一部分,但我对整个形状本身是否正确感到有点困惑。 I just started learning Akka so I can understand if my approach is wrong.我刚开始学习 Akka 所以我可以理解我的方法是否错误。 Also I the purpose of the exercise was to create a custom component and I know I can get the logic done through GraphDSL but it needs to be a custom component.此外,我练习的目的是创建一个自定义组件,我知道我可以通过 GraphDSL 完成逻辑,但它需要是一个自定义组件。 My question is how do I create this shape correctly.我的问题是如何正确创建此形状。 (the documentation isn't the best for custom components) (文档不是自定义组件的最佳选择)

    public final Inlet<DeviceInfo> in = Inlet.create("Map.in");
    public final Outlet<DeviceInfo> temp_out = Outlet.create("Map.out");
    public final Outlet<DeviceInfo> humidity_out = Outlet.create("Map.out");
    public final Outlet<DeviceInfo> illumination_out = Outlet.create("Map.out");
    //Does not work
    private final UniformFanOutShape<DeviceInfo, DeviceInfo> shape = UniformFanOutShape.apply(in, Arrays.asList(temp_out, humidity_out));

    @Override
    public UniformFanOutShape<DeviceInfo, DeviceInfo> shape() {     
        return shape;
    }

[EDIT] TL;DR You basically need to work around calling Scala vararg method from Java. You can do it like this using scala.collection.mutable.ArrayBuffer [编辑] TL; DR 您基本上需要解决从 Java 调用 Scala vararg 方法的问题。您可以使用scala.collection.mutable.ArrayBuffer这样做

    ArrayBuffer<Outlet<DeviceInfo>> arrayBuffer = new ArrayBuffer<>();
    arrayBuffer.append(temp_out);
    arrayBuffer.append(humidity_out);
    arrayBuffer.append(illumination_out);

    final UniformFanOutShape<DeviceInfo, DeviceInfo> shape =
      UniformFanOutShape.apply(in, arrayBuffer.toSeq());

source: How to use Scala varargs from Java code资料来源: 如何使用 Java 代码中的 Scala 可变参数

[/EDIT] [/编辑]

(the documentation isn't the best for custom components) (文档不是自定义组件的最佳选择)

I disagree.我不同意。 The documentation is pretty comprehensive ( https://doc.akka.io/docs/akka/current/stream/stream-customize.html )文档非常全面( https://doc.akka.io/docs/akka/current/stream/stream-customize.html

You shouldn't extend UniformFanOutShape<DeviceInfo, DeviceInfo> , but rather GraphStage<UniformFanOutShape<DeviceInfo, DeviceInfo>> .您不应扩展UniformFanOutShape<DeviceInfo, DeviceInfo> ,而应扩展 GraphStage GraphStage<UniformFanOutShape<DeviceInfo, DeviceInfo>>

You then need to override two methods:然后你需要覆盖两个方法:

 public UniformFanOutShape<DeviceInfo, DeviceInfo> shape() {
    //implement
 }

 @Override
 public GraphStageLogic createLogic(Attributes inheritedAttributes) {
   //implement
 }

the documentation shows you how to create shapes and how to implement GraphStageLogic with detailed information about handling backpressure, etc.该文档向您展示了如何创建形状以及如何使用有关处理背压等的详细信息来实现GraphStageLogic

The documentation shows examples for SourceShape , SinkShape and FlowShape .该文档显示了SourceShapeSinkShapeFlowShape的示例。 If you understand those examples, but need some more guidance you can look at the source code.如果您了解这些示例,但需要更多指导,您可以查看源代码。 In akka.stream.scaladsl.Graph.scala you can find implementation of all built in GraphStage s Akka Streams provide.akka.stream.scaladsl.Graph.scala ,您可以找到所有内置GraphStage的 Akka Streams 提供的实现。 For example Broadcast is an example of a UniformFanOutShape stage and this is how the code for it starts:例如BroadcastUniformFanOutShape阶段的一个例子,它的代码是这样开始的:

final class Broadcast[T](val outputPorts: Int, val eagerCancel: Boolean) extends GraphStage[UniformFanOutShape[T, T]]

The implementation is under 100 lines of code and follows the same pattern like the documentation shows.该实现不到 100 行代码,并遵循与文档所示相同的模式。 The code is Scala, because Akka is implemented in that language with a thin Java layer meant to be used from Java code.代码是 Scala,因为 Akka 是用该语言实现的,带有一个薄的 Java 层,旨在从 Java 代码中使用。 So for example there's also akka.stream.javadsl.Graph.scala , but you will see that the code there is delegating to implementations in scaladsl package. You will see that pattern all over Akka code.因此,例如还有akka.stream.javadsl.Graph.scala ,但您会看到那里的代码委托给scaladsl package 中的实现。您会在整个 Akka 代码中看到该模式。 So in short you will need to understand a bit of scala to understand the implementation but in this case here it's very similar to what Java implementation could look like.所以简而言之,您需要了解一些 scala 才能理解其实现,但在本例中,它与 Java 的实现非常相似。

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

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