2016-03-13 3 views
0

Я использую playframework 2.4.6 для мобильного чата. Все нормально, если я использую память вместо db. Я пытаюсь сохранить состояния в redis db. Код ниже ..Как решить Прямая самооценка, приводящая к экономии времени цикла ActorRef on redis in playframework 2.4.6

Это метод управления принимает запросы сокетов ..

public WebSocket<String> chat(String roomId,String nick){ 
    return WebSocket.withActor(new Function<ActorRef, Props>(){ 
     @Override 
     public Props apply(ActorRef actorRef) throws Throwable { 
      return WebSocketActor.props(actorRef,new ChatUser(roomId, nick)); 
     } 
    }); 
} 

Мой WebSocket Актер ниже

public class WebSocketActor extends UntypedActor{ 
... 
public WebSocketActor(ActorRef actorRef,ChatUser chatUser) { 
      mChatUser = chatUser; 
      mActorRef = actorRef; 
      //redis room fetch 
      mRoom = Room.fetchFromRedis(mChatUser.roomId); 
      if(mRoom==null) 
       mRoom = new Room(mChatUser.roomId); 
      mRoom.addSocket(mActorRef); 
     } 
class Room{ 
    private String mRoomId; 
    //TODO: this list causes error while saving to redis 
    private List<ActorRef> mActorRefList; 
    public Room() { 
     //default no arg constructor 
    } 
    public Room(String roomId) { 
     this.mRoomId = roomId; 
     this.mActorRefList = new ArrayList<>(); 
    } 

    public void addSocket(ActorRef actorRef){ 
     this.mActorRefList.add(actorRef); 
     saveToRedis(); 
    } 

    public static Room fetchFromRedis(String roomId){ 
     Jedis jedis = new Jedis("localhost"); 
     String roomJson = jedis.get(roomId); 
     if(roomJson!=null&&!roomJson.equals("")) 
      return Json.fromJson(Json.parse(roomJson),Room.class); 
     return null; 
    } 

    private void saveToRedis(){ 
     Jedis jedis = new Jedis("localhost"); 
     JsonNode jsonNode = Json.toJson(this); 
     String roomStr = Json.stringify(jsonNode); 
     jedis.set(mRoomId, roomStr); 
    } 
} 

я получаю сообщение об ошибке при сохранении моего объекта номера для Redis из-за Перечислите тип параметра mActorRefList.

Стек след ниже ..

[error] - akka.actor.OneForOneStrategy - exception during creation 
akka.actor.ActorInitializationException: exception during creation 
    at akka.actor.ActorInitializationException$.apply(Actor.scala:166) ~[akka-actor_2.11-2.3.13.jar:na] 
    at akka.actor.ActorCell.create(ActorCell.scala:596) ~[akka-actor_2.11-2.3.13.jar:na] 
    at akka.actor.ActorCell.invokeAll$1(ActorCell.scala:456) ~[akka-actor_2.11-2.3.13.jar:na] 
    at akka.actor.ActorCell.systemInvoke(ActorCell.scala:478) ~[akka-actor_2.11-2.3.13.jar:na] 
    at akka.dispatch.Mailbox.processAllSystemMessages(Mailbox.scala:263) ~[akka-actor_2.11-2.3.13.jar:na] 
    at akka.dispatch.Mailbox.run(Mailbox.scala:219) ~[akka-actor_2.11-2.3.13.jar:na] 
    at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:397) [akka-actor_2.11-2.3.13.jar:na] 
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) [scala-library-2.11.6.jar:na] 
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) [scala-library-2.11.6.jar:na] 
    at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) [scala-library-2.11.6.jar:na] 
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) [scala-library-2.11.6.jar:na] 
Caused by: java.lang.reflect.InvocationTargetException: null 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_66] 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_66] 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_66] 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422) ~[na:1.8.0_66] 
    at akka.util.Reflect$.instantiate(Reflect.scala:66) ~[akka-actor_2.11-2.3.13.jar:na] 
    at akka.actor.ArgsReflectConstructor.produce(Props.scala:355) ~[akka-actor_2.11-2.3.13.jar:na] 
    at akka.actor.Props.newActor(Props.scala:255) ~[akka-actor_2.11-2.3.13.jar:na] 
    at akka.actor.ActorCell.newActor(ActorCell.scala:552) ~[akka-actor_2.11-2.3.13.jar:na] 
    at akka.actor.ActorCell.create(ActorCell.scala:578) ~[akka-actor_2.11-2.3.13.jar:na] 
    ... 9 common frames omitted 
Caused by: java.lang.RuntimeException: java.lang.IllegalArgumentException: Direct self-reference leading to cycle (through reference chain: actors.Room["mActorRefList"]->java.util.ArrayList[0]->akka.actor.LocalActorRef["parent"]->akka.actor.RepointableActorRef["parent"]->akka.actor.LocalActorRef["parent"]->akka.actor.$anon$1["parent"]) 
    at play.libs.Json.toJson(Json.java:78) ~[play-json_2.11-2.4.6.jar:2.4.6] 
    at actors.Room.saveToRedis(WebSocketActor.java:85) ~[classes/:na] 
    at actors.Room.addSocket(WebSocketActor.java:72) ~[classes/:na] 
    at actors.WebSocketActor.<init>(WebSocketActor.java:31) ~[classes/:na] 
    ... 18 common frames omitted 
Caused by: java.lang.IllegalArgumentException: Direct self-reference leading to cycle (through reference chain: actors.Room["mActorRefList"]->java.util.ArrayList[0]->akka.actor.LocalActorRef["parent"]->akka.actor.RepointableActorRef["parent"]->akka.actor.LocalActorRef["parent"]->akka.actor.$anon$1["parent"]) 
    at com.fasterxml.jackson.databind.ObjectMapper.valueToTree(ObjectMapper.java:2374) ~[jackson-databind-2.5.4.jar:2.5.4] 
    at play.libs.Json.toJson(Json.java:76) ~[play-json_2.11-2.4.6.jar:2.4.6] 

Любое решение?

Thx ..

+0

Вы видели фактическое сообщение об ошибке, в котором говорится, что у вас есть циклическая зависимость? – rethab

ответ

0

Я решил свою проблему.

Вы должны сериализовать своих актеров акк-способом. Посмотрите на это link

 Смежные вопросы

  • Нет связанных вопросов^_^