使用文件上传Actor进行喷涂路由

时间:2015-04-04 19:57:17

标签: akka spray

我有一个使用Spray Routing的现有Spray应用程序,我最近添加了一个文件流上传Actor based on the example

我能解释的是如何将我现有的根HttpService actor与不扩展HttpService的文件上传Actor结合起来。

我现有的root服务actor看起来像这样:

class RootService extends Actor with HttpService with Routes with ActorLogging {
  def receive = runRoute {
      routes
  }
}

我的文件上传了这样的Actor:

class FileUploadService extends Actor with Logging {
  def receive = {
    case part @ HttpRequest(POST, Uri.Path("/upload"), headers, entity: HttpEntity.NonEmpty, protocol) => {
      val parts = part.asPartStream()
      val client = sender
      val handler = context.actorOf(Props(new FileUploadHandler(client, parts.head.asInstanceOf[ChunkedRequestStart])))
      parts.tail.foreach(handler !)
    }
    case start @ ChunkedRequestStart(HttpRequest(POST, Uri.Path("/upload"), _, _, _)) => {
      val client = sender
      val handler = context.actorOf(Props(new FileUploadHandler(client, start)))
      sender ! RegisterChunkHandler(handler)
    }
  }
}

我尝试修改我的root服务actor,如下所示:

class RootService extends Actor with HttpService with Routes with ActorLogging {
  val fileUploadActor = context.actorOf(Props[FileUploadService], "fileUploadActor")
  def receive = runRoute {
    pathPrefix("upload") {
        fileUploadActor ! _.request
    } ~ {
      route
    }
  }
}

但这不能正常工作。最终,响应永远不会回到客户端(即使我指定ask vs tell)。

有关工作实施的任何建议吗?

谢谢!

1 个答案:

答案 0 :(得分:3)

在RootService中,将RequestContext发送给上传actor:

def receive = runRoute {
 pathPrefix("upload") {
   ctx => fileUploadActor ! ctx
 }
}

然后在文件上传服务中,您可以像RootService一样使用runRoute,并在该服务中完成请求。如果您有对RequestContext的引用,则无需回复RootService。