2016-01-13 16:25:24 +01:00
|
|
|
/**
|
2016-01-25 10:16:14 +01:00
|
|
|
* Copyright (C) 2015-2016 Typesafe <http://typesafe.com/>
|
2016-01-13 16:25:24 +01:00
|
|
|
*/
|
|
|
|
|
package docs.stream.javadsl.cookbook;
|
|
|
|
|
|
2016-01-20 10:00:37 +02:00
|
|
|
import akka.NotUsed;
|
2016-01-13 16:25:24 +01:00
|
|
|
import akka.actor.ActorSystem;
|
|
|
|
|
import akka.stream.ActorMaterializer;
|
|
|
|
|
import akka.stream.Materializer;
|
|
|
|
|
import akka.stream.io.Framing;
|
|
|
|
|
import akka.stream.javadsl.Sink;
|
|
|
|
|
import akka.stream.javadsl.Source;
|
|
|
|
|
import akka.testkit.JavaTestKit;
|
|
|
|
|
import akka.util.ByteString;
|
|
|
|
|
import org.junit.AfterClass;
|
|
|
|
|
import org.junit.BeforeClass;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
public class RecipeParseLines extends RecipeTest {
|
|
|
|
|
|
|
|
|
|
static ActorSystem system;
|
|
|
|
|
|
|
|
|
|
@BeforeClass
|
|
|
|
|
public static void setup() {
|
|
|
|
|
system = ActorSystem.create("RecipeLoggingElements");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@AfterClass
|
|
|
|
|
public static void tearDown() {
|
|
|
|
|
JavaTestKit.shutdownActorSystem(system);
|
|
|
|
|
system = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final Materializer mat = ActorMaterializer.create(system);
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void parseLines() throws Exception {
|
2016-01-20 10:00:37 +02:00
|
|
|
final Source<ByteString, NotUsed> rawData = Source.from(Arrays.asList(
|
2016-01-13 16:25:24 +01:00
|
|
|
ByteString.fromString("Hello World"),
|
|
|
|
|
ByteString.fromString("\r"),
|
|
|
|
|
ByteString.fromString("!\r"),
|
|
|
|
|
ByteString.fromString("\nHello Akka!\r\nHello Streams!"),
|
|
|
|
|
ByteString.fromString("\r\n\r\n")));
|
|
|
|
|
|
|
|
|
|
//#parse-lines
|
2016-01-20 10:00:37 +02:00
|
|
|
final Source<String, NotUsed> lines = rawData
|
2016-01-13 16:25:24 +01:00
|
|
|
.via(Framing.delimiter(ByteString.fromString("\r\n"), 100, true))
|
|
|
|
|
.map(b -> b.utf8String());
|
|
|
|
|
//#parse-lines
|
|
|
|
|
|
2016-01-21 16:37:26 +01:00
|
|
|
lines.grouped(10).runWith(Sink.head(), mat).toCompletableFuture().get(1, TimeUnit.SECONDS);
|
2016-01-13 16:25:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|