akka streams 1.0 and http routing - in java

96 views
Skip to first unread message

paweł kamiński

unread,
Aug 10, 2015, 4:38:40 PM8/10/15
to Akka User List
hi,
probably again I ve overlooked it in documentation but I cannot find a way to respond to a http request with streamed response. probably HttpEntityChunked should be used but again I cannot figure out how to set entities and flows correctly.

this is basic example

private RouteResult getEvents(RequestContext ctx)
{

final Source<Integer, BoxedUnit> source = producer.produce(10, 100);
return ctx.complete(HttpResponse.create());
}

thanks for any hint on that

Johannes Rudolph

unread,
Aug 11, 2015, 3:44:15 AM8/11/15
to Akka User List
Hi paweł,

you are on the right track. You need to provide the data as a `Source<ByteString, Object>`. Then, you can create a response like this:

Source<ByteString, Object> data = ...
HttpResponse response = HttpResponse.create().withEntity(HttpEntities.createChunked(contentType, data));

If you know the length of the data (i.e. the aggregated number of octets in all the ByteStrings the Source provides), you can also use

HttpEntityies.create(contentType, length, data)

to create a (non-chunked) streamed default entity.

HTH
Johannes

paweł kamiński

unread,
Aug 11, 2015, 3:51:33 AM8/11/15
to Akka User List
aaaaah :]

thanks, I guess my bad was that I tried to use Source<T, BoxedUnit> and it wasnt accepted... and I was blind :))

paweł kamiński

unread,
Aug 25, 2015, 5:19:29 PM8/25/15
to Akka User List
OK I give up again :)

so if I have 

Source<Integer, BoxedUnit> source = producer.produce(10, 100);

and encoder that transforms integer to ByteStrings

final Flow<Integer, ByteString, BoxedUnit> encoder = Flow
.of(Integer.class)
.map(number -> {
final ByteStringBuilder builder = new ByteStringBuilder();
builder.putByte((byte) 4);
builder.putInt(number, ByteOrder.BIG_ENDIAN);

return builder.result();
})
.named("encoder");

I apply transformation as Source<ByteString, BoxedUnit> data = source.via(encoder) but how do I get Source<ByteString, Object> data.

Flow.of(Integer.class) creates Flow that materializes to BoxUnit

what Object stands for?

On Tuesday, 11 August 2015 09:44:15 UTC+2, Johannes Rudolph wrote:

Johannes Rudolph

unread,
Aug 26, 2015, 4:27:44 AM8/26/15
to akka-user
Hi paweł,

yes, that's an unfortunate detail of the Java API. Whatever you put in
there as the Object value it will be ignored. So, to convert your flow
you should be able to use `.<Object>mapMaterializedValue(m -> m)` to
convert the type.

Johannes
> --
>>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>>> Check the FAQ:
>>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "Akka User List" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/akka-user/kHNRr-Vqhzc/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> akka-user+...@googlegroups.com.
> To post to this group, send email to akka...@googlegroups.com.
> Visit this group at http://groups.google.com/group/akka-user.
> For more options, visit https://groups.google.com/d/optout.



--
Johannes

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net

paweł kamiński

unread,
Aug 26, 2015, 12:03:35 PM8/26/15
to Akka User List
that worked! you've saved my life again.
Reply all
Reply to author
Forward
0 new messages