我正在使用Tumblr的新Colossus框架(http://tumblr.github.io/colossus/)构建应用程序。关于它的文档仍然有限(而且我对Akka来说还是很新的事实并没有帮助),所以我想知道是否有人能说出我的方法是否正确。
应用程序很简单,由两个关键组件组成:
我做了一个简单的例子来证明我的并发模型能够正常工作(它确实如此),我在下面发布了这个模型。但是,我想确保没有更惯用的方法来做到这一点。
import colossus.IOSystem
import colossus.protocols.http.Http
import colossus.protocols.http.HttpMethod.Get
import colossus.protocols.http.UrlParsing._
import colossus.service.{Callback, Service}
import colossus.task.Task
object QueueProcessor {
implicit val io = IOSystem() // Create separate IOSystem for worker
Task { ctx =>
while(true) {
// Below code is for testing purposes only. This is where the Redis loop will live, and will use a blocking call to get the next available task
Thread.sleep(5000)
println("task iteration")
}
}
def ping = println("starting") // Method to launch this processor
}
object Main extends App {
implicit val io = IOSystem() // Primary IOSystem for the web service
QueueProcessor.ping // Launch worker
Service.serve[Http]("app", 8080) { ctx =>
ctx.handle { conn =>
conn.become {
case req@Get on Root => Callback.successful(req.ok("Here"))
// The methods to add tasks to the queue will live here
}
}
}
}
我测试了上面的模型并且它有效。当服务愉快地接受请求时,后台循环继续运行。但是,我认为可能有更好的方法与工作人员(在文档中找不到任何内容),或者Akka Streams?
答案 0 :(得分:0)
我得到的东西对我来说似乎是半惯用的。然而,新答案&反馈仍然受到欢迎!
class Processor extends Actor {
import scala.concurrent.ExecutionContext.Implicits.global
override def receive = {
case "start" => self ! "next"
case "next" => {
Future {
blocking {
// Blocking call here to wait on Redis (BRPOP/BLPOP)
self ! "next"
}
}
}
}
}
object Main extends App {
implicit val io = IOSystem()
val processor = io.actorSystem.actorOf(Props[Processor])
processor ! "start"
Service.serve[Http]("app", 8080) { ctx =>
ctx.handle { conn =>
conn.become {
// Queue here
case req@Get on Root => Callback.successful(req.ok("Here\n"))
}
}
}
}