使用Akka远程系统播放2.0.2

时间:2012-10-01 02:32:21

标签: java playframework playframework-2.0 akka future

我正在使用Akka 2和Play 2.0.2。 我想将Play Framewrok连接到远程Akka系统。 我做了远程配置和 演员的远程引用,但任何时候 我尝试将播放framewrok连接到Akka远程系统。 我收到错误,我在下面提供了代码片段。

播放应用程序中的

代码。

public static Result index() throws InterruptedException { 

      ActorSystem csystem = Akka.system();
      ActorRef localNode = csystem.actorOf(new Props(LocalNode.class),"localNode");
        localNode.tell("Hello");


        Thread.sleep(5000);
        csystem.shutdown();
        return ok(index.render("I am OK"));
  }  
}

这是Play Actor的代码

public class LocalNode extends UntypedActor {

    LoggingAdapter log = Logging.getLogger(getContext().system(), this);
    Timeout timeout = new Timeout(Duration.create(5 , "seconds"));

    static ActorRef masterActor;

    public void preStart()
    {

        /* Get reference to Master Node*/
         masterActor = getContext().
            actorFor("akka://MasterNode@127.0.0.1:9002/user/masterNode/masterActor");
    }

    @Override
    public void onReceive(Object message) throws Exception {
        System.out.println(" Future called  ");

        Future<Object> future = Patterns.ask(masterActor , message, timeout);

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

           log.info("Message from Server", result.toString());  
    }
}
来自Play应用程序配置文件的

代码

#confige the remote connection

localNode {
    akka {
        actor {
            provider = "akka.remote.RemoteActorRefProvider"
        }
        remote {
            transport = "akka.remote.netty.NettyRemoteTransport"
            netty {
                hostname = "127.0.0.1"
                port = 0
            }
        }
    }
}
下面的

是来自远程Akka主节点的代码

包Rubine_Cluster;

import com.typesafe.config.ConfigFactory;
public class MasterNode implements Bootable
{
     final ActorSystem system;

      public MasterNode() {
        system = ActorSystem.create("MasterNode", ConfigFactory.load()
            .getConfig("masterNode"));
        ActorRef masterActor = system.actorOf(new Props(MasterActor.class),"masterActor");
        System.out.println(" Master Node is called ");
      }  
      public void startup() {

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

下面是远程Akka子Actor

的代码
public class MasterActor extends UntypedActor {

    public MasterActor(){System.out.println(" the masteractor has been started ");}

    @Override
    public void onReceive(Object message) throws Exception {
        System.out.print(" this is from me to you  "+message.toString());

         if (message instanceof String) {
                // Get reference to the message sender and reply back
                getSender().tell(message + " got something");
                System.out.print(" this is from me to you  "+message.toString());
    }
  }  
}

下面是远程Akka系统配置文件的代码

masterNode {

akka {
  actor {
    provider = "akka.remote.RemoteActorRefProvider"
  }
  remote {
    transport = "akka.remote.netty.NettyRemoteTransport"
    netty {
      hostname = "127.0.0.1"
      port = 9002
    }
 }
}
}

下面我们有错误ocode:

[ERROR] [10/01/2012 03:03:21.646] [application-akka.actor.default-dispatcher-2]
[akka://application/user/localNode] sending to terminated ref breaks promises
akka.pattern.AskTimeoutException: sending to terminated ref breaks promises
        at akka.pattern.AskSupport$class.ask(AskSupport.scala:76)
        at akka.pattern.package$.ask(package.scala:43)
        at akka.pattern.Patterns$.ask(Patterns.scala:41)
        at akka.pattern.Patterns.ask(Patterns.scala)
        at controllers.LocalNode.onReceive(LocalNode.java:32)
        at akka.actor.UntypedActor$$anonfun$receive$1.apply(UntypedActor.scala:1
54)
        at akka.actor.UntypedActor$$anonfun$receive$1.apply(UntypedActor.scala:1
53)
        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)

我正在做一个提交项目,有人可以帮助解决这个问题。欢迎任何建议。

1 个答案:

答案 0 :(得分:0)

  1. 你不应该关闭csystem,因为它是Play的ActorSystem。
  2. 你有:“actorFor(”akka://MasterNode@127.0.0.1:9002 / user / masterNode / masterActor“);”但是你没有“masterActor”这样的父(“masterNode”)