简体   繁体   English

当Source具有大量记录时,Akka流不会运行

[英]Akka streams don't run when Source has large number of records

I'm trying to write a very simple introductory example of using Akka Streams. 我正在尝试编写一个使用Akka Streams的非常简单的入门示例。 I'm attempting to basically create a stream that takes a range of integers as a source and filters out all the integers that are not prime, producing a stream of prime integers as its output. 我试图从根本上创建一个流,该流以一定范围的整数作为源,并过滤掉所有非素数的整数,从而生成素数流作为其输出。

The class that constructs the stream is rather simple; 构造流的类非常简单。 for that I have the following. 为此,我有以下几点。

import akka.NotUsed;
import akka.actor.ActorSystem;
import akka.stream.javadsl.Flow;
import com.aparapi.Kernel;
import com.aparapi.Range;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class PrimeStream {
    private final AverageRepository averageRepository = new AverageRepository();
    private final ActorSystem actorSystem;

    public PrimeStream(ActorSystem actorSystem) {
        this.actorSystem = actorSystem;
    }

    public Flow<Integer, Integer, NotUsed> filterPrimes() {
        return Flow.of(Integer.class).grouped(10000).mapConcat(PrimeKernel::filterPrimes).filter( v -> v != 0);
    }
}

When I run the following test, it works fine. 当我运行以下测试时,它工作正常。

private final ActorSystem actorSystem = ActorSystem.create("Sys");

@Test
public void testStreams() {
    Flow<Integer, Integer, NotUsed> filterStream = new PrimeStream(actorSystem).filterPrimes();
    Source<Integer, NotUsed> flow = Source.range(10000000, 10001000).via(filterStream);
    flow.runForeach(System.out::println, ActorMaterializer.create(actorSystem));
}

However, when I increase the range by a factor of x10 by changing the line in the test to the following, it no longer works. 但是,当我通过将测试中的行更改为以下内容将范围增加到x10倍时,它不再起作用。

Source<Integer, NotUsed> flow = Source.range(10000000, 10010000).via(filterStream);

Now when the test runs, no exceptions are thrown, no warnings. 现在,运行测试时,不会引发任何异常,没有警告。 It simply runs, then exits, without displaying any text to the console at all. 它只是运行,然后退出,根本不向控制台显示任何文本。

Just to be extra certain that the problem wasn't in my primality test itself, I ran the test over the same range without using Akka Streams, and it runs fine. 为了进一步确定问题不是我的原始测试本身,我在不使用Akka Streams的情况下在相同范围内运行了测试,并且运行正常。 The following code runs without a problem. 以下代码可以正常运行。

@Test
public void testPlain() {
    List<Integer> in = IntStream.rangeClosed(10000000, 10010000).boxed().collect(Collectors.toList());
    List<Integer> out = PrimeKernel.filterPrimes(in);
    System.out.println(out);
}

Just for the sake of clarity, the primality test itself takes in a list of integers and sets any element in the list to 0 if it is not prime. 只是为了清楚起见,素数测试本身会接受一个整数列表,并将列表中的任何元素(如果不是素数)设置为0。

As suggested by @RamonJRomeroyVigil if i remove the mapConcat part all together but leave everythig the same it does, in fact, print out 10,000 integers. 正如@RamonJRomeroyVigil所建议的,如果我一起删除了mapConcat部分,但让everythig保持不变,那么实际上将打印出10,000个整数。 However If i leave everything the same but simply replace filterPrimes with a method that just returns the method parameter as is without touching it, then it doesnt print anything to the screen at all. 但是,如果我将所有内容保持不变,而只是将filterPrimes替换为只返回方法参数而不接触它的方法,那么它根本不会在屏幕上显示任何内容。 I've also tried adding a println to the begining filterPrime to debug it. 我还尝试将println添加到开始的filterPrime进行调试。 Whenever it doesnt print any output that includes the debugging statement. 每当它不打印任何包含调试语句的输出时。 So no attempt is even made to call filterPrimes at all. 因此,根本没有尝试调用filterPrimes。

runForeach returns a CompletionStage , so if you want to see all the numbers getting printed then you have to await on the CompletionStage otherwise the test function returns and the program terminates without the CompletionStage getting completed. runForeach返回一个CompletionStage ,因此,如果您想查看所有已打印的数字,则必须在CompletionStage上等待,否则测试函数将返回并且程序将在CompletionStageCompletionStage情况下终止。

Example: 例:

flow.runForeach(System.out::println, ActorMaterializer.create(actorSystem)).toCompletableFuture().join();

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

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