当前位置: 首页 > 知识库问答 >
问题:

如何创建一个可以在以后通过方法调用接收元素的源?

贺飞
2023-03-14

我想创建一个source和稍后在其上的推送元素,如:

val src = ... // create the Source here
// and then, do something like this
pushElement(x1, src)
pushElement(x2, src)

推荐的方法是什么?

谢谢!

共有1个答案

夔博
2023-03-14

有三种方法可以实现这一点:

1.使用SourceQueue进行后物化

您可以使用source.queue将流具体化为sourcequeue:

case class Weather(zipCode : String, temperature : Double, raining : Boolean)

val bufferSize = 100

//if the buffer fills up then this strategy drops the oldest elements
//upon the arrival of a new element.
val overflowStrategy = akka.stream.OverflowStrategy.dropHead

val queue = Source.queue(bufferSize, overflowStrategy)
                  .filter(!_.raining)
                  .to(Sink foreach println)
                  .run() // in order to "keep" the queue Materialized value instead of the Sink's

queue offer Weather("02139", 32.0, true)
val ref = Source.actorRef[Weather](Int.MaxValue, fail)
                .filter(!_.raining)
                .to(Sink foreach println )
                .run() // in order to "keep" the ref Materialized value instead of the Sink's

ref ! Weather("02139", 32.0, true)
object WeatherForwarder {
  def props : Props = Props[WeatherForwarder]
}

//see provided link for example definition
class WeatherForwarder extends Actor {...}

val actorRef = actorSystem actorOf WeatherForwarder.props 

//note the stream has not been instatiated yet
actorRef ! Weather("02139", 32.0, true) 

//stream already has 1 Weather value to process which is sitting in the 
//ActorRef's internal buffer
val stream = Source(ActorPublisher[Weather](actorRef)).runWith{...}
 类似资料:
  • 我是个新手,学习面向对象的Java。我有一个关于通过调用方法为多个数组添加元素的问题。我只是试图避免多次创建循环,对于多个数组,我想创建一个方法,它将包含必要的循环来添加元素(甚至将它们显示为输出),这样我就可以在需要的时候调用它。希望你明白我想说的话。请原谅我的英语不好。

  • 问题内容: 通过,我们可以创建/修改连接的参数,但是我想知道是否可以通过API进行相同的操作,以便可以以编程方式设置连接 似乎它只处理实际连接到实例,而不是将其保存到列表中。似乎应该已经实现了一个功能,但是我不确定在哪里可以找到该特定功能的文档。 问题答案: 连接实际上是一个模型,可以用来查询和插入新连接

  • 我尝试用Spring Security创建登录页面。我需要实现某种类型的登录。登录的URL必须相同。例如 我打开主登录页面-< code > localhost:8080/log in 然后我输入一些信息并按下“下一步”按钮 我移动到 在这个页面上,我有两种类型的登录-通过ip、通过短信、通过等等。如果我按下按钮“通过ip”,我需要看到url- 但调用控制器方法 如果我按按钮"通过短信"我需要看到

  • 在正方形应该移动的地方初始化。KeyListener继承自GameAssistant(JFrame是用KeyListener创建的) }

  • 问题内容: 假设我有类,并且其中的类具有可读和可写的属性: 是否可以只使用该方法而只使用该方法? 问题答案: 不可以,无法分配每个类别的访问权限。 考虑将您的类分为单独的接口,以便每个类仅获得一个具有所需接口的对象。例如: 当然,这涉及安全性,但这应该使您朝正确的方向进行思考。

  • 我得到了如下html代码片段: 该元素在html中唯一唯一的标识是属性,因此我想使用类似以下的Python selenium方法来定位它: 我已经查看了selenium(python)文档以查找元素,但没有找到如何通过attr来定位此元素的线索。在python selenium中是否有明确的方法来定位这个元素?