gpt4 book ai didi

playframework - playframework 2.4.6 中redis 上保存ActorRef 时Direct self-reference 导致循环错误如何解决

转载 作者:可可西里 更新时间:2023-11-01 11:35:01 28 4
gpt4 key购买 nike

我正在为移动聊天后端使用 playframework 2.4.6。如果我使用内存而不是数据库,一切都很好。我正在尝试将状态保存在 redis 数据库中。代码如下..

这是接受套接字请求的 Controller 方法..

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);
}
}

由于列表类型 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]

有什么解决办法吗?

谢谢..

最佳答案

我解决了我自己的问题。

你必须用akka的方式序列化你的actor。看这个link

关于playframework - playframework 2.4.6 中redis 上保存ActorRef 时Direct self-reference 导致循环错误如何解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35975839/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com