0

我在 java (1.6) 中运行 akka remoting (2.0.2) 的示例示例,但没有从远程获取结果。看来我的本地节点无法连接到远程笔记。

LocalNodeApplication 代码在本地系统上,RemoteNodeApplication 代码在远程系统上。

请参考代码:

本地节点 -

public class LocalNodeApplication extends Controller {

public static Result process(String msg) throws Exception {

    ActorSystem _system = ActorSystem.create("LocalNodeApp", ConfigFactory.load().getConfig("LocalSys"));
    ActorRef localActor = _system.actorOf(new Props(LocalActor.class));

    localActor.tell("Hello");

    Thread.sleep(5000);
    _system.shutdown();

    return ok("success");
}

}

本地演员代码:

public class LocalActor extends UntypedActor {
LoggingAdapter log = Logging.getLogger(getContext().system(), this);
Timeout timeout = new Timeout(Duration.parse("5 seconds"));

ActorRef remoteActor;

@Override
public void preStart() {
    //Get a reference to the remote actor

    remoteActor = getContext().actorFor("akka://RemoteNodeApp@192.168.0.85:9002/user/remoteActor");
}

@Override
public void onReceive(Object message) throws Exception {
    Future<Object> future = Patterns.ask(remoteActor, message.toString(), timeout);

    String result = (String) Await.result(future, timeout.duration());

    log.info("Message received from Server -> {}", result);

    /*remoteActor.tell(message.toString());*/
}

}

本地系统配置:

LocalSys {
akka {
    actor {
        provider = "akka.remote.RemoteActorRefProvider"
        deployment {
            /remoteActor {
                remote = "akka://RemoteNodeApp@192.168.0.85:9002"
            }
        }
    }
}

}


远程节点代码 -

远程节点应用

public class RemoteNodeApplication implements Bootable {

final ActorSystem system = ActorSystem.create("RemoteNodeApp", ConfigFactory.load().getConfig("RemoteSys"));

public void shutdown() {
    system.shutdown();
}

public void startup() {     
    system.actorOf(new Props(RemoteActor.class), "remoteActor");
}

}

远程演员:

public class RemoteActor extends UntypedActor {
@Override
public void onReceive(Object message) throws Exception {
    if (message instanceof String) {
        // Get reference to the message sender and reply back
        getSender().tell(message + " got something");
    }
}

}

远程系统配置 -

RemoteSys {
akka {
    actor {
        provider = "akka.remote.RemoteActorRefProvider"
    }
    remote {
        untrusted-mode = on
        transport = "akka.remote.netty.NettyRemoteTransport"
        netty {
            hostname = "192.168.0.85"
            port = 9002
        }
    }
}

}


我开始在本地节点上播放 "run 9000" 并在远程开始播放 "run 9002" 。

我得到以下提到的异常:

[INFO] [09/17/2012 17:42:52.206] [play-akka.actor.actions-dispatcher-1] [ActorSystem(LocalNodeApp)] REMOTE: RemoteServerStarted@akka://LocalNodeApp@127.0.0.1:2552
[INFO] [09/17/2012 17:42:52.406] [LocalNodeApp-akka.actor.default-dispatcher-4] [ActorSystem(LocalNodeApp)] REMOTE: RemoteClientStarted@akka://RemoteNodeApp@192.168.0.85:9002
[INFO] [09/17/2012 17:42:52.438] [LocalNodeApp-akka.actor.default-dispatcher-1] [ActorSystem(LocalNodeApp)] REMOTE: RemoteClientShutdown@akka://RemoteNodeApp@192.168.0.85:9002
[ERROR] [09/17/2012 17:42:57.281] [LocalNodeApp-akka.actor.default-dispatcher-4] [akka://LocalNodeApp/user/$a] Timed out
    akka.pattern.AskTimeoutException: Timed out
        at akka.dispatch.DefaultPromise.result(Future.scala:875)
        at akka.dispatch.Await$.result(Future.scala:74)
        at akka.dispatch.Await.result(Future.scala)
        at com.localNodeApp.LocalActor.onReceive(LocalActor.java:30)
        at akka.actor.UntypedActor$$anonfun$receive$1.apply(UntypedActor.scala:154)
        at akka.actor.UntypedActor$$anonfun$receive$1.apply(UntypedActor.scala:153)
        at akka.actor.Actor$class.apply(Actor.scala:318)
        at akka.actor.UntypedActor.apply(UntypedActor.scala:93)
        at akka.actor.ActorCell.invoke(ActorCell.scala:626)
        at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:197)
        at akka.dispatch.Mailbox.run(Mailbox.scala:179)
        at akka.dispatch.ForkJoinExecutorConfigurator$MailboxExecutionTask.exec(AbstractDispatcher.scala:516)
        at akka.jsr166y.ForkJoinTask.doExec(ForkJoinTask.java:259)
        at akka.jsr166y.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:975)
        at akka.jsr166y.ForkJoinPool.runWorker(ForkJoinPool.java:1479)
        at akka.jsr166y.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:104)
Caused by: akka.pattern.AskTimeoutException: Timed out
        at akka.pattern.PromiseActorRef$$anonfun$1.apply$mcV$sp(AskSupport.scala:274)
        at akka.actor.DefaultScheduler$$anon$6$$anon$7.run(Scheduler.scala:183)
        at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:94)
        at akka.jsr166y.ForkJoinTask$AdaptedRunnableAction.exec(ForkJoinTask.java:1381)
        ... 4 more
 [INFO] [09/17/2012 17:42:57.318] [LocalNodeApp-akka.actor.default-dispatcher-4] [ActorSystem(LocalNodeApp)] REMOTE: RemoteServerShutdown@akka://LocalNodeApp@127.0.0.1:2552

当我在本地系统上为本地节点和远程节点运行相同的示例时(对于将 IP 地址更改为本地系统),在控制台上得到以下提到的行:

[INFO] [09/17/2012 17:42:52.206] [play-akka.actor.actions-dispatcher-1] [ActorSystem(LocalNodeApp)] REMOTE: RemoteServerStarted@akka://LocalNodeApp@127.0.0.1:2552
[INFO] [09/17/2012 17:42:52.406] [LocalNodeApp-akka.actor.default-dispatcher-4] [ActorSystem(LocalNodeApp)] REMOTE: RemoteClientStarted@akka://RemoteNodeApp@192.168.0.85:9002
[INFO] [09/17/2012 17:42:52.438] [LocalNodeApp-akka.actor.default-dispatcher-1] [ActorSystem(LocalNodeApp)] REMOTE: RemoteClientShutdown@akka://RemoteNodeApp@192.168.0.85:9002
[INFO] [09/17/2012 17:42:57.318] [LocalNodeApp-akka.actor.default-dispatcher-4] [ActorSystem(LocalNodeApp)] REMOTE: RemoteServerShutdown@akka://LocalNodeApp@127.0.0.1:2552

请指导我。我是否按照正确的步骤进行 akka 远程处理?

4

1 回答 1

0

远程应用程序需要作为 jar 部署在 akka 微内核中。远程应用程序启动后,您可以运行本地应用程序以连接到远程应用程序。由write2munish回答

于 2012-09-19T15:54:11.367 回答