重新加载后在WebSocket Iteratee中播放Morphia MappingException

时间:2015-03-21 04:57:50

标签: playframework websocket playframework-2.0 akka morphia

我使用Play 2.3和Akka在Web套接字上设置简单的发布订阅协议。作为此协议的一部分,在客户端订阅之后,服务器从数据库发送初始状态。

我当前的代码最初工作,但在开发过程中,套接字Iteratee中的Morphia查询在开发重新加载后停止工作。常规请求,甚至是完全相同查询的请求,都能继续正常工作。

我正在使用分布式pub子调解器Akka插件。这是代表Web套接字侦听器的actor的相关代码:

object Subscriber {
  def props(channel: Concurrent.Channel[JsValue]): Props = Props(new Subscriber(channel))
}

class Subscriber(channel: Concurrent.Channel[JsValue]) extends Actor {
  def receive = {
    case StatusUpdate(...) =>
      channel.push(...)
  }
}

对于主视图控制器:

object SocketController extends Controller {
  val mediator = DistributedPubSubExtension.get(Akka.system).mediator

  def index = WebSocket.using[JsValue] { implicit request =>
    val (out, channel) = Concurrent.broadcast[JsValue]

    val ws = Akka.system.actorOf(Subscriber.props(channel))

    val in = Iteratee.foreach[JsValue] { msg =>
      (msg \ "type").as[String] match {
        case "Subscribe" =>
          val target = (msg \ "value").as[String]

          // Query database with Morphia
          val current = MyObj.findByName(target)

          // Notify of current state
          ws ! StatusUpdate(target, current)

          // Subscribe for further updates
          mediator ! DistributedPubSubMediator.Subscribe(target, ws)
      }
    }

    (in, out)
  }
}

问题出现在Iterattee的查询语句中。重新加载后,Morphia投掷 org.mongodb.morphia.mapping.MappingException: Could not map models.MyObj with ID: 5507a3653004b8a9e8f3d3b2。这里有完整的痕迹:

org.mongodb.morphia.mapping.MappingException: Could not map models.MyObj with ID: 5507a3653004b8a9e8f3d3b2
    at org.mongodb.morphia.mapping.Mapper.fromDb(Mapper.java:594)
    at org.mongodb.morphia.mapping.Mapper.fromDBObject(Mapper.java:299)
    at org.mongodb.morphia.query.MorphiaIterator.convertItem(MorphiaIterator.java:79)
    at org.mongodb.morphia.query.MorphiaIterator.processItem(MorphiaIterator.java:65)
    at org.mongodb.morphia.query.MorphiaIterator.next(MorphiaIterator.java:60)
    at org.mongodb.morphia.query.QueryImpl.get(QueryImpl.java:402)
    at models.MyObj$.findByName(MyObj.scala:99)
    at controllers.MyObj$$anonfun$MyObj$1$$anonfun$4.apply(MyObj.scala:99)
    at controllers.MyObj$$anonfun$MyObj$1$$anonfun$4.apply(MyObj.scala:93)
    at play.api.libs.iteratee.Iteratee$$anonfun$foreach$1.apply(Iteratee.scala:201)
    at play.api.libs.iteratee.Iteratee$$anonfun$foreach$1.apply(Iteratee.scala:201)
    at play.api.libs.iteratee.Iteratee$$anonfun$fold$1$$anonfun$apply$1.apply(Iteratee.scala:41)
    at play.api.libs.iteratee.internal$.eagerFuture(package.scala:30)
    at play.api.libs.iteratee.Iteratee$$anonfun$fold$1.apply(Iteratee.scala:41)
    at play.api.libs.iteratee.Iteratee$$anonfun$fold$1.apply(Iteratee.scala:41)
    at play.api.libs.iteratee.Iteratee$$anonfun$1.apply(Iteratee.scala:60)
    at play.api.libs.iteratee.Iteratee$$anonfun$1.apply(Iteratee.scala:60)
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24)
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24)
    at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:41)
    at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:393)
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
    at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
Caused by: org.mongodb.morphia.mapping.MappingException: Error setting value from converter (ObjectIdConverter) for models.MyObj.id to 5507a3653004b8a9e8f3d3b2
    at org.mongodb.morphia.converters.DefaultConverters.fromDBObject(DefaultConverters.java:137)
    at org.mongodb.morphia.mapping.ValueMapper.fromDBObject(ValueMapper.java:27)
    at org.mongodb.morphia.mapping.Mapper.readMappedField(Mapper.java:608)
    at org.mongodb.morphia.mapping.Mapper.fromDb(Mapper.java:589)
    ... 24 more
Caused by: java.lang.IllegalArgumentException: Can not set org.bson.types.ObjectId field models.MyObj.id to models.MyObj
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:75)
    at java.lang.reflect.Field.set(Field.java:758)
    at org.mongodb.morphia.mapping.MappedField.setFieldValue(MappedField.java:508)
    at org.mongodb.morphia.converters.DefaultConverters.fromDBObject(DefaultConverters.java:135)
    ... 27 more

此异常仅发生在Web套接字处理程序中,常规请求可以使查询正常。完全重新启动开发服务器可以解决问题,直到下次重新加载。

这是控制台从启动和重启时的日志:http://pastebin.com/xsstw4ki

对这里可能发生的事情有任何想法?我发现此错误的其他参考文件似乎是unrelatedold

更新 - 2015年7月4日

也在Play 2.4.2上发生。

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码段来解决此问题。通过添加以下代码(Scala代码)来修改您的morphia创建

if (Play.isDev)
    morphia.getMapper.getOptions.setObjectFactory(new PlayCreator)

PlayCreator类的定义是(Java代码)

public class PlayCreator extends DefaultCreator {

@Override
protected ClassLoader getClassLoaderForClass() {
    return Play.application().classloader();
}
}

请添加相关的导入,您的热重载应该效果很好:)