1

我使用这样的代码:

用户角色首先在代码中的某处创建,如下所示:

TypedActor.get(actorSystem).typedActorOf(new TypedProps<UserActor>(IUserActor.class, new Creator<UserActor>() {
        @Override
        public UserActor create() throws Exception {
            return new UserActor(userId);
        }
    }), userId);

之后,我试图获取它的一个实例(就像 UntypedActors 的 actorFor 一样):

ActorRef userActorRef = actorSystem.actorFor("akka://actors/user/" + userId));
UserActor userActor = TypedActor.get(actorSystem).typedActorOf(new TypedProps<UserActor>(UserActor.class), userActorRef);

// exception is here:
userActor.gotRequest(msg);

但是一旦我在 userActor 上调用一个方法,我就会得到

java.lang.ClassCastException: $Proxy5 cannot be cast to com.lutshe.akka.actors.UserActor
    at com.lutshe.akka.AkkaMessagesHandler.process(AkkaMessagesHandler.java:47)
    at com.lutshe.tcp.server.TestMessageHandler.messageReceived(TestMessageHandler.java:31)
    at com.lutshe.tcp.server.TestMessageHandler.messageReceived(TestMessageHandler.java:14)
    at io.netty.channel.ChannelInboundMessageHandlerAdapter.inboundBufferUpdated(ChannelInboundMessageHandlerAdapter.java:69)
 ...

我究竟做错了什么?谢谢

4

1 回答 1

1

Akka 返回接口的代理,而不是实现,你应该这样做:

 IUserActor userActor = TypedActor.get(actorSystem).typedActorOf(new TypedProps<IUserActor>(UserActor.class), userActorRef);
于 2013-02-12T09:44:28.307 回答