【问题标题】:File writing iteratee does not receive EOF for WS.get文件写入迭代器没有收到 WS.get 的 EOF
【发布时间】:2014-03-26 22:44:09
【问题描述】:

我已经创建了一个简单的迭代器来使用 WS 下载文件,如 this link 中所述。

考虑以下 sn-p:

import java.nio.ByteBuffer
import java.nio.channels.FileChannel
import org.specs2.mutable.Specification
import org.specs2.time.NoTimeConversions
import play.api.libs.Files.TemporaryFile
import play.api.libs.iteratee.{Done, Input, Cont, Iteratee}
import play.api.libs.ws.WS
import scala.concurrent.Await
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global

class DummySpec extends Specification with NoTimeConversions {
  def fileChannelIteratee(channel: FileChannel): Iteratee[Array[Byte], Unit] = Cont {
    case Input.EOF =>
      println("Input.EOF")
      channel.close()
      Done(Unit, Input.EOF)
    case Input.El(bytes) =>
      println("Input.El")
      val buf = ByteBuffer.wrap(bytes)
      channel.write(buf)
      fileChannelIteratee(channel)
    case Input.Empty =>
      println("Input.Empty")
      fileChannelIteratee(channel)
  }

  "fileChannelIteratee" should {
    "work" in {
      val file = TemporaryFile.apply("test").file
      val channel = FileChannel.open(file.toPath)

      val future = WS.url("http://www.example.com").get(_ => fileChannelIteratee(channel)).map(_.run)

      Await.result(future, 10.seconds)

      file.length !== 0
    }
  }
}

在 WS.get 之后调用 .map(_.run) 似乎在这里没有任何效果,因为 iteratee 似乎没有收到 Input.EOF。它使我无法关闭频道。这是我得到的输出:

Input.El
[info] DummySpec
[info]
[info] fileChannelIteratee should
[info] x work (941 ms)
[error]    '0' is equal to '0' (DummySpec.scala:37)
[info]
[info]
[info] Total for specification DummySpec
[info] Finished in 948 ms
[info] 1 example, 1 failure, 0 error

我做错了什么?

我正在使用 Play Framework 2.2.2。

提前致谢。

【问题讨论】:

    标签: scala playframework playframework-2.2 iterate


    【解决方案1】:

    我以错误的方式打开 FileChannel。当没有给出参数时,它似乎默认为根据this link 读取模式。

    channel.write 抛出的异常被map 操作吞噬,因为整个操作的返回类型为Future[Future[Unit]]。即使在这种情况下内部失败,外部Future 也处于成功状态。应该改用flatMap

    【讨论】:

      猜你喜欢
      • 2011-11-29
      • 1970-01-01
      • 2021-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多