I’m creating small demos to develop my understanding of streaming objects over http using akka. My end goal is a route that streams a generated object (an image from a webcam to be precise) over http. My attempt at a smaller version of this is a route that contains a Source.tick with a call to a method that returns a string.
My route:
path("test", () ->
get(() -> extractRequest(request -> complete(testHandler.handleTextSource(request))))
),
My handler
public HttpResponse handleTextSource(HttpRequest httpRequest) {
final Source<ByteString, Cancellable> source
= Source.tick(Duration.ofSeconds(1), Duration.ofSeconds(2), getText()).map(ByteString::fromString);
HttpEntity.Chunked textEntity = HttpEntities.create(ContentTypes.TEXT_PLAIN_UTF8, source);
return HttpResponse.create().withEntity(textEntity);
}
public String getText() {
System.out.println("getText()");
return "text";
}
I ran a curl command on this route and notice getText() is called only once and nothing is displayed in the output. Once I kill the server all of a sudden a number of responses arrive in the terminal. From looking at the twitter streaming example online I noticed they use completeOkWithSource. In the example they use a JsonEntityStreamingSupport and a Jackson.marshaller().
a) Why is source.tick not being called every duration? I would expect getText() to be called every 2 seconds. Am I not using the source correctly?
b) If I use a source do I need to use completeOkWithSource, if so must I create my own EntityStreamingSupport (creating my own requires I implement many methods I don’t understand so I was avoiding this).
Thank you very much
David