以这种方式使用akka与套接字不好的做法?

时间:2016-05-27 14:16:01

标签: scala akka

以下是我如何使用PLay配置ReactiveMongo!接受WebSocket连接并通过Actor将数据返回给客户端:

@Singleton
class Cont @Inject() (implicit system: ActorSystem, materializer: Materializer, val reactiveMongoApi: ReactiveMongoApi)
    extends Controller with MongoController with ReactiveMongoComponents {

  val collection: reactivemongo.api.collections.bson.BSONCollection = reactiveMongoApi.connection.db("db1").collection("col1");

  def socket = WebSocket.accept[String, String] { request =>
    ActorFlow.actorRef[String, String](out => ActorObj.props(out))
  }

  object ActorObj {

    def props(out: ActorRef) = Props(new ActorClass(out))
  }

  class ActorClass(out: ActorRef) extends Actor {
    def receive = {
      case msg: String => {
        val queryDoc = BSONDocument("id" -> "1")
        val enumerator: Enumerator[BSONDocument] =
          collection.find(queryDoc).options(QueryOpts().tailable.awaitData).cursor[BSONDocument].enumerate()

        val processDocuments: Iteratee[BSONDocument, Unit] =
          Iteratee.foreach { doc =>

            out ! (String.valueOf(doc.getAs[Int]("auditCount").get))

          }
        enumerator.run(processDocuments)

      }

    }
  }
}

由于我只是为每个客户端套接字连接创建一个actor并使用同一个actor通过套接字向客户端发送数据,这样使用Akka是一种不好的做法吗?我认为这种不良做法的原因是akka演员应该是短暂的,但是只要插座开启,这个实现中的演员就会活着,所以可能是几小时,几天......

0 个答案:

没有答案