简体   繁体   English

更改 akka 流的源数据

[英]Changing source data for akka streams

I'm learning about Java Akka streams and using https://doc.akka.io/docs/akka/current/stream/stream-flows-and-basics.html have defined the following: I'm learning about Java Akka streams and using https://doc.akka.io/docs/akka/current/stream/stream-flows-and-basics.html have defined the following:

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;

public class SourceExample {

    static ActorSystem system = ActorSystem.create("SourceExample");

    public static void main(String args[]) throws ExecutionException, InterruptedException {

        final List<Integer> sourceData = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

        final Source<Integer, NotUsed> source =
                Source.from(sourceData);
        final Sink<Integer, CompletionStage<Integer>> sink =
                Sink.<Integer, Integer>fold(0, (agg, next) -> agg + next);

        final CompletionStage<Integer> sum = source.runWith(sink, system);

        System.out.println(sum.toCompletableFuture().get());
    }

}

Running this code behaves as expected.运行此代码按预期运行。

Is the problem Akka Streams is resolving that this code can be executed repeatedly.问题是 Akka Streams 正在解决此代码可以重复执行。

In a real-world scenario, sourceData will not be static, does Akka Streams have an opinion as to how changing data should be handled or is it determined by the developer?在实际场景中, sourceData不会是 static,Akka Streams 对如何处理变化的数据有意见还是由开发人员决定?

In the simplest case just re-execute the streaming Flow every X minutes (using a scheduled Task for example) when the source data changes.在最简单的情况下,只需在源数据更改时每 X 分钟重新执行一次流式流(例如使用计划任务)。 Or are Akka streams long-lived, the source data changes and the stream computations are re-executed according to some parameters?还是 Akka 流长期存在,源数据发生变化并且 stream 计算根据某些参数重新执行?

The Akka Streams documentation defines multiple sources of data but I don't understand how Akka Streams should be utilised to handle changing source data. Akka Streams 文档定义了多个数据源,但我不明白如何使用 Akka Streams 来处理不断变化的源数据。

Akka Streams can, and often do, run until (shortly before) your app stops. Akka Streams 可以并且经常运行,直到(不久之前)您的应用程序停止。 For instance, it's common to have a stream consuming (eg using a Kafka consumer source from Alpakka Kafka) Kafka records start very early in an app and not stop until the app is killed.例如,使用 stream 的情况很常见(例如,使用来自 Alpakka Kafka 的 Kafka 消费者源)Kafka 记录在应用程序中很早就开始,直到应用程序被终止才停止。

To elaborate, a stream runs until such time as:详细地说,stream 一直运行到以下时间:

  • a stage signals completion (eg in your example, Source.from would signal completion after emitting 10 )阶段信号完成(例如,在您的示例中, Source.from将在发出10后发出完成信号)
  • a stage fails (typically throwing an exception)阶段失败(通常抛出异常)

An example source that's useful for dynamic data (without introducing Alpakka or Akka HTTP) is Source.queue which materializes as a queue for which enqueued elements become available to the stream.一个对动态数据有用的示例源(不引入 Alpakka 或 Akka HTTP)是Source.queue ,它具体化为一个队列,排队的元素可用于 stream。

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

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