Play / Akka与Java OutputStreams集成 [英] Play/Akka integration with Java OutputStreams

查看:139
本文介绍了Play / Akka与Java OutputStreams集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个游戏!暴露REST API的应用程序,允许用户生成PDF报告。我受到使用旧Java API进行实际报告生成的要求的限制。该库有一个方法 generate(OutputStream out,...),即它需要一个 java.io.OutputStream 在哪里写出结果报告。

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.

我的问题是将它与Play / Akka集成以提供Chunked Encoding中的内容。为此,我需要创建一个枚举器[Array [Byte]] ,它以某种方式包含来自Java的 OutputStream 图书馆。我想出了一个使用 PipedOutputSteam / PipedInputStream duo的工作解决方案来将库的输出传输到Enumerator使用 Enumerator.fromStream

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.

我想知道是否有更好的方法来实现这一点,但我可以似乎在Akka或Play中找到了一个明确的例子!将枚举器与OutputStream集成的文档。我知道Java库的阻塞IO是设计更好的解决方案的限制因素,但也许有一种更简洁的方法来做到这一点。有什么想法?

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?

跟进

假设我使用 Enumerator.outputStream ,将实际报告生成移动到另一个actor(可能在另一台机器上)的安全方法是什么?例如,我猜测发送OutputStream是不安全的(并且只能在本地工作)。

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).

推荐答案

Play 2.1 has以下方法: Enumerator.outputStream(a:(OutputStream)⇒Unit):枚举器[Array [Byte]] 这可能完全符合您的要求。

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

它的实现是专门使用其他Play 2.1类,所以如果你使用的是Play 2.0,你需要做一些额外的挖掘来实现同样的目的。

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.

这篇关于Play / Akka与Java OutputStreams集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆