首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用卡西巴插入演员的MongoDB?

如何用卡西巴插入演员的MongoDB?
EN

Stack Overflow用户
提问于 2015-01-06 20:00:13
回答 1查看 729关注 0票数 1

假设:

  • MongoDB正在本地主机上运行:27017
  • 该项目以https://github.com/sap1ens/akka-microservice为模型。如果下面没有足够的信息来帮助,请参考它。如果太混乱,我可以在必要时从其他文件中添加代码。

基于的RegistrationsService.scala问题

  • 为什么它试图连接到MongoDB并在启动时插入文档,然后才将任何PostRegistrationMessage发送给RegistrationsService参与者?
  • 为什么失败了?
  • 如何将registrationJsValue转换为MongoDBObject并将其插入到集合中?

相关信息在build.sbt

代码语言:javascript
复制
scalaVersion := "2.10.4"

val akkaVersion = "2.3.8"

val sprayVersion = "1.3.1"

// Main dependencies
libraryDependencies ++= Seq(
    "com.typesafe.akka" %% "akka-actor" % akkaVersion,
    "com.typesafe.akka" %% "akka-slf4j" % akkaVersion,
    "io.spray" % "spray-can" % sprayVersion,
    "io.spray" % "spray-routing" % sprayVersion,
    "io.spray" % "spray-client" % sprayVersion,
    "io.spray" %% "spray-json" % sprayVersion,
    "com.typesafe" % "config" % "1.2.1",
    "ch.qos.logback" % "logback-classic" % "1.1.2",
    "org.mongodb" %% "casbah" % "2.7.4"
)

RegistrationService.scala (类似于git中的ExampleService.scala )

代码语言:javascript
复制
package service

import akka.actor.{Props, ActorLogging, Actor}
import spray.json._
import com.mongodb.util.JSON
import com.mongodb.casbah.Imports._
import model.Registration
import model.RegistrationProtocol._

object RegistrationsService {
  case class PostRegistrationMessage(registration: Registration)

  def props(property: String) = Props(classOf[RegistrationsService], property)
}

class RegistrationsService(property: String) extends Actor with ActorLogging {
  import RegistrationsService._

  def receive = {
    case PostRegistrationMessage(registration) => {

      val registrationJsValue = registration.toJson
      val dbObject = JSON.parse(registrationJsValue.toString()).asInstanceOf[DBObject]

      val mongoClientURI = MongoClientURI("mongodb://localhost:27017/")
      val mongoClient = MongoClient(mongoClientURI)
      val someDB = mongoClient("somedb")
      val registrationsColl = someDB("registratoins")
      log.info(s"Got access to registratoins collection")

      registrationsColl.insert(MongoDBObject("hello" -> "world"))
      log.info(s"Inserted a doc to registratoins collection")

      mongoClient.close()
      log.info(s"Closed client connection to mongo")

      sender() ! registrationJsValue
    }
  }
}

sbt运行

代码语言:javascript
复制
14:01:10.655 [microservice-system-akka.actor.default-dispatcher-3] INFO  c.e.a.s.RegistrationsService - Got access to registratoins collection
14:01:11.383 [microservice-system-akka.actor.default-dispatcher-3] DEBUG spray.can.server.HttpListener - Binding to localhost/127.0.0.1:8878
14:01:11.475 [microservice-system-akka.actor.default-dispatcher-3] DEBUG akka.io.TcpListener - Successfully bound to /127.0.0.1:8878
14:01:11.480 [microservice-system-akka.actor.default-dispatcher-6] INFO  spray.can.server.HttpListener - Bound to localhost/127.0.0.1:8878
14:01:20.704 [microservice-system-akka.actor.default-dispatcher-5] ERROR akka.actor.OneForOneStrategy - Timed out after 10000 ms while waiting for a server that matches AnyServerSelector{}. Client view of cluster state is {type=Unknown, servers=[{address=localhost:27017, type=Unknown, state=Connecting, exception={java.lang.NullPointerException}}]
com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting for a server that matches AnyServerSelector{}. Client view of cluster state is {type=Unknown, servers=[{address=localhost:27017, type=Unknown, state=Connecting, exception={java.lang.NullPointerException}}]
    at com.mongodb.BaseCluster.getServer(BaseCluster.java:82) ~[mongo-java-driver-2.12.4.jar:na]
    at com.mongodb.DBTCPConnector.getServer(DBTCPConnector.java:654) ~[mongo-java-driver-2.12.4.jar:na]
    at com.mongodb.DBTCPConnector.access$300(DBTCPConnector.java:39) ~[mongo-java-driver-2.12.4.jar:na]
    at com.mongodb.DBTCPConnector$MyPort.getConnection(DBTCPConnector.java:503) ~[mongo-java-driver-2.12.4.jar:na]
    at com.mongodb.DBTCPConnector$MyPort.get(DBTCPConnector.java:451) ~[mongo-java-driver-2.12.4.jar:na]
    at com.mongodb.DBTCPConnector.getPrimaryPort(DBTCPConnector.java:409) ~[mongo-java-driver-2.12.4.jar:na]
    at com.mongodb.DBCollectionImpl.insert(DBCollectionImpl.java:182) ~[mongo-java-driver-2.12.4.jar:na]
    at com.mongodb.DBCollectionImpl.insert(DBCollectionImpl.java:165) ~[mongo-java-driver-2.12.4.jar:na]
    at com.mongodb.DBCollection.insert(DBCollection.java:93) ~[mongo-java-driver-2.12.4.jar:na]
    at com.mongodb.casbah.MongoCollectionBase$class.insert(MongoCollection.scala:621) ~[casbah-core_2.10-2.7.4.jar:2.7.4]
    at com.mongodb.casbah.MongoCollection.insert(MongoCollection.scala:1109) ~[casbah-core_2.10-2.7.4.jar:2.7.4]
    at service.RegistrationsService$$anonfun$receive$1.applyOrElse(RegistrationsService.scala:47) ~[classes/:na]
    at akka.actor.Actor$class.aroundReceive(Actor.scala:465) ~[akka-actor_2.10-2.3.8.jar:na]
    at service.RegistrationsService.aroundReceive(RegistrationsService.scala:20) ~[classes/:na]
    at akka.actor.ActorCell.receiveMessage(ActorCell.scala:516) [akka-actor_2.10-2.3.8.jar:na]
    at akka.actor.ActorCell.invoke(ActorCell.scala:487) [akka-actor_2.10-2.3.8.jar:na]
    at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:254) [akka-actor_2.10-2.3.8.jar:na]
    at akka.dispatch.Mailbox.run(Mailbox.scala:221) [akka-actor_2.10-2.3.8.jar:na]
    at akka.dispatch.Mailbox.exec(Mailbox.scala:231) [akka-actor_2.10-2.3.8.jar:na]
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) [scala-library.jar:na]
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) [scala-library.jar:na]
    at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) [scala-library.jar:na]
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) [scala-library.jar:na]
^C14:01:42.231 [microservice-system-akka.actor.default-dispatcher-11] INFO  akka.actor.LocalActorRef - Message [akka.actor.Terminated] from Actor[akka://microservice-system/user/$a#1754981697] to Actor[akka://microservice-system/user/IO-HTTP/listener-0#-602059531] was not delivered. [1] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
14:01:42.236 [microservice-system-akka.actor.default-dispatcher-11] DEBUG akka.event.EventStream - shutting down: StandardOutLogger started
EN

回答 1

Stack Overflow用户

发布于 2015-01-06 20:42:02

不知道你说的是什么意思。

为什么它试图连接到MongoDB并在启动时插入文档,然后才将任何PostRegistrationMessage发送给RegistrationsService参与者?

在示例代码(对于RegistrationsService)中,您已经显示了所建立的唯一连接是在Actor的接收方法中。除非您(或其他代码正在发送此消息),否则参与者将不会尝试连接到Mongo。

另外,您要在Actor的receive方法中创建一个Mongo连接吗?您应该避免在receive方法中进行任何繁重的操作。您可以在Actor之外创建一个Mongo连接,然后将它注入Actor的构造函数中,以便在接收方法中使用。

您是否编写了一个独立的程序/测试,使用casbah驱动程序连接到您的MongoDB?它应该独立于您的参与者代码,并将帮助您隔离驱动程序和连接代码中的任何问题(如果存在)。

这可能不是你的问题的答案,但我不想写在评论部分,因为篇幅和格式的限制。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27806271

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档