How can I extract Future[T] value in unmarshaller without blocking ?

62 views
Skip to first unread message

Jack Daniels

unread,
Jul 5, 2015, 5:12:29 PM7/5/15
to akka...@googlegroups.com
I'm using akka-http 1.0.-RC4.

How can I extract Future[T] value in unmarshaller without blocking ? 


I extended ScalaXmlSupport you created to support unmarshalling http requests using Unmarshaller[NodeSeq, T] unmarshallers by adding this

implicit def xmlUnmarshallerConverter[T](marshaller: Unmarshaller[NodeSeq, T])(implicit mat: Materializer): FromEntityUnmarshaller[T] =
xmlUnmarshaller(marshaller, mat)

implicit def xmlUnmarshaller[T](implicit marshaller: Unmarshaller[NodeSeq, T], mat: Materializer): FromEntityUnmarshaller[T] = {
implicit val exec = mat.executionContext
defaultNodeSeqUnmarshaller.map(v => {
val future = marshaller(v)(mat.executionContext)
val result = Await.result(future, Duration(10, TimeUnit.SECONDS))
result
})
}

The only problem is that prototype of Unmarshaller.apply is this

def apply[A, B](f: ExecutionContext ⇒ A ⇒ Future[B]): Unmarshaller[A, B]

It requires me to returns Future which I do by creating unmarshallers of model classes like

implicit val articleBodyUnmarshaller = Unmarshaller[NodeSeq, ArticleBody] { xml =>
Future(ArticleBody(xml.toString()))
}

But I can't use it in xmlUnmarshaller : FromEntityUnmarshaller[Tbecause it requires me to return concrete object which forces me to do ugly blocking

How should I write it properly ?

Dmitry Vorobiov

unread,
Jul 6, 2015, 4:41:55 AM7/6/15
to akka...@googlegroups.com
Have you looked at onComplete / onSuccess directives that help processing futures?
Reply all
Reply to author
Forward
0 new messages