如何在java中获取已创建的TypedActor的实例

时间:2013-02-11 22:34:55

标签: akka typedactor

我使用这样的代码:

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

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

之后,我正在尝试获取它的实例(就像actor for UntypedActors一样):

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)
 ...

我做错了什么?感谢

1 个答案:

答案 0 :(得分:1)

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

 IUserActor userActor = TypedActor.get(actorSystem).typedActorOf(new TypedProps<IUserActor>(UserActor.class), userActorRef);