Play/Akka integration with Java OutputStreams

断了今生、忘了曾经 提交于 2019-12-07 08:47:38

问题


I'm writing a Play! application which exposes a REST API allowing users to generate PDF reports. I'm constrained by the requirements to use a old Java API to do the actual report generation. That library has a method generate(OutputStream out, ...), i.e. it takes a java.io.OutputStream where it writes the resulting report.

My problem is integrating this with Play/Akka to serve the contents in Chunked Encoding. To that end, I need to create an Enumerator[Array[Byte]] which somehow contains the OutputStream from the Java library. I have come up with a working solution that uses a PipedOutputSteam/PipedInputStream duo to pipe the output from the library to Enumerator using Enumerator.fromStream.

I'm wondering if there is a better way to achieve this, but I can't seem to find an explicit example in the Akka or Play! documentations that integrates Enumerators with OutputStreams. I'm aware that the Java library's blocking IO is a limiting factor on designing a better solution, but maybe there's a neater way to do this. Any thoughts?

Follow up

Assuming I use Enumerator.outputStream, what would be a safe way to move the actual report generation to another actor (possibly on another machine)? For instance, I'm guessing sending the OutputStream isn't safe (and would only work locally).


回答1:


Play 2.1 has the following method: Enumerator.outputStream(a: (OutputStream) ⇒ Unit): Enumerator[Array[Byte]] which probably does exactly what you want.

It's implementation is specifically using other Play 2.1 classes, so if you are using Play 2.0 you would need to do some extra digging to achieve the same thing.



来源:https://stackoverflow.com/questions/16065424/play-akka-integration-with-java-outputstreams

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!