如何解决直接自引用导致循环错误将ActorRef保存在playframework 2.4.6中的redis上
Posted
技术标签:
【中文标题】如何解决直接自引用导致循环错误将ActorRef保存在playframework 2.4.6中的redis上【英文标题】:How to solve Direct self-reference leading to cycle error saving ActorRef on redis in playframework 2.4.6 【发布时间】:2016-03-13 21:02:27 【问题描述】:我正在将 playframework 2.4.6 用于移动聊天后端。如果我使用内存而不是数据库,一切都很好。我正在尝试将状态保存在 redis db 中。代码如下..
这是接受套接字请求的控制器方法..
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 Actor 在下面
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);
由于 List 类型 mActorRefList 参数,我在将房间对象保存到 redis 时出错。
堆栈跟踪如下..
[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]
有什么办法吗?
谢谢..
【问题讨论】:
你看到实际的错误信息了吗,说你有循环依赖? 【参考方案1】:我解决了自己的问题。
您必须通过 akka 方式序列化您的演员。看看这个link
【讨论】:
以上是关于如何解决直接自引用导致循环错误将ActorRef保存在playframework 2.4.6中的redis上的主要内容,如果未能解决你的问题,请参考以下文章
EF实体类,设置导航属性,导致序列化时出现"循环引用"错误,及序列化时间格式解决方案