远程akka系统端口冲突

时间:2012-10-08 11:05:53

标签: java playframework-2.0 akka eclipse-juno

我正在使用java开发一个play framework 2系统。该play框架使用与play框架集成的akka​​系统来连接远程akka系统。远程akka系统由主节点和工作节点组成。两个系统都与eclipse juno IDE在同一台计算机上  我为主节点配置了两个端口2552,为工作节点配置了端口2553.播放2框架上的akka​​节点由系统自身选择。播放框架中的akka​​系统有望通过使用akka配置进行远程查找,将杂物传播到远程主节点。主节点实习生也将消息传递给远程工作人员,以便通过远程查找进行处理。主节点和工作节点在以下格式中包含了application.conf文件:

src/main/resources/application.conf

然而,在启动时,主节点和工作节点都决定使用端口号2552进行通信。我在下面提供代码片段:

这是播放framewrok application.config文件的代码。

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

这是play localNode的配置

package controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import play.libs.F.Callback;
import play.mvc.WebSocket;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.serialization.Serialization;
import akka.serialization.SerializationExtension;

import com.typesafe.config.ConfigFactory;
import  Com.RubineEngine.GesturePoints.*;

public class LocalNode  {

    ActorSystem csystem;
    ActorRef localActor ;


    public LocalNode() { 

       //We create the actor container and a child upon initialization 
     csystem = ActorSystem.create("LocalNode", ConfigFactory.load().getConfig("localNode"));
     localActor = csystem.actorOf(new Props(LocalActor.class),"localActor");
    }


     public void connectMaster (final String classname)
     {  
         localActor.tell(classname);

     }

     public  void connectMaster ()
     {  

     }


     public void connectMaster (final WebSocket.In<JsonNode> in, final WebSocket.Out<JsonNode> out )
     {        
             in.onMessage(new Callback<JsonNode>() {
             public void invoke(JsonNode event) throws JsonParseException, JsonMappingException, IOException {                 

            ObjectMapper mapper = new ObjectMapper();               

            @SuppressWarnings("unchecked")
            Map<String,ArrayList<Object>> jsonMap = mapper.readValue(event, Map.class); 
            GesturePoints gp = new GesturePoints();

            gp.setPoints(jsonMap);
            localActor.tell(gp);            
             }
          });     } 
}           

这是play框架中akka actor的代码

package controllers;


import Com.RubineEngine.GesturePoints.*;

import akka.actor.ActorRef;
import akka.actor.UntypedActor;
import akka.event.Logging;
import akka.event.LoggingAdapter;

public class LocalActor extends UntypedActor {

     /**
     * 
     */

    ActorRef masterActor; // = getContext().actorFor("akka://MasterNode@127.0.0.1:2552/user/masterActor");
     LoggingAdapter log = Logging.getLogger(getContext().system(), this);

    @Override
    public void onReceive(Object arg) throws Exception {
        System.out.println(" Local Actor 1");
          if(arg instanceof GesturePoints)
         {  System.out.println(" local Actor 2");
              masterActor.tell(" Welcome home " , getSelf());   
              System.out.println(" Local Actor 3");}    
         else 
         {unhandled(arg);}
    }


    public void preStart()
    {
      masterActor = getContext().actorFor("akka://MasterNode@127.0.0.1:2553/user/masterActor");
    }
}

这是主节点application.conf

的代码
masterNode {

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

这是主节点的代码

package Rubine_Cluster;

import java.util.Arrays;

import com.typesafe.config.ConfigFactory;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.kernel.Bootable;

/**
 * Hello world!
 *
 */
public class MasterNode implements Bootable
{
     final ActorSystem system;
     ActorRef masterActor;

      public MasterNode() {

        //Create a child actor of this actor upon initialization 
        system = ActorSystem.create("MasterNode", ConfigFactory.load()
            .getConfig("masterNode"));
         masterActor = system.actorOf(new Props(MasterActor.class),"masterActor");  

      }


      public void startup() {

      }

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

这是主akka演员的代码

package Rubine_Cluster;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Map;

import Com.RubineEngine.GesturePoints.*;
import akka.actor.*;
import akka.serialization.Serialization;
import akka.serialization.SerializationExtension;
import akka.serialization.Serializer;





public class MasterActor extends UntypedActor {

    /**
     * 
     */

    ActorRef worker1;



    @Override
    public void onReceive(Object message) throws Exception {
        System.out.println(" Master Actor 5");
        System.out.println(message);

         if(message instanceof GesturePoints)
         {  //GesturePoints gp = (GesturePoints) message;
              System.out.println(" Master Actor 1");             
         try {      worker1.tell(message, getSelf());

             System.out.println(" Master Actor 2");
                } catch (Exception e) {
                  getSender().tell(new akka.actor.Status.Failure(e), getSelf());
                  throw e;
                }

    }
         else{ unhandled(message);}
  }  

    public void preStart()
    {
      worker1 = getContext().actorFor("akka://WorkerNode@127.0.0.1:2552/user/workerActor");
    }

}

这是工作节点application.conf的代码

workerNode {

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

这是工人nide的代码

package com.theta.gesture;

import com.typesafe.config.ConfigFactory;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.kernel.Bootable;


public class WorkerNode implements Bootable{
   ActorSystem system;
   ActorRef worker;

   WorkerNode(){
       system = ActorSystem.create("WorkerNode", ConfigFactory.load()
                .getConfig("workerNode"));
            ActorRef workerActor = system.actorOf(new Props(WorkerActor.class),"workerActor");              
}


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

    }

    public void startup() {
    }       
}

这是工作项目中actor的代码

package com.theta.gesture;

import java.util.ArrayList;
import java.util.Map;

import Com.RubineEngine.GesturePoints.GesturePoints;
import akka.actor.*;

public class WorkerActor extends UntypedActor {

     private static double              DIST_SQ_THRESHOLD   = 3 * 3; /* threshold to eliminate mouse jitter */

    @Override
    public void onReceive(Object msg) throws Exception {

         if(msg instanceof GesturePoints) 
         { GesturePoints message = (GesturePoints) msg;
          initial_Theta(message);}  

         else {unhandled(msg);}
    }

    public void initial_Theta(GesturePoints p)
    {       System.out.println(" Worker Actor 1");          
        if(p.getPoints().get("X").size() < 3)  //The number of x coordinates as size
        {   return;}                     

        System.out.println(" Worker Actor 2");
             double magsq,dx,dy, recip;

             dx = (double) ((Integer)p.getPoints().get("x").get(2)  - (Integer)p.getPoints().get("x").get(0)) ;
             dy = ((Double)p.getPoints().get("y").get(2)) - ((Double)p.getPoints().get("y").get(0));

             magsq = dx * dx + dy * dy;  
             if(magsq  > DIST_SQ_THRESHOLD)
             {
                 recip = 1/Math.sqrt(magsq);
                double  initial_cos = dx * recip;

                System.out.println(" Worker Actor 3");
                double  initial_sin = dy * recip;

                 System.out.println("Feature cos  " + initial_cos);
                 System.out.println("Gesture sin  " + initial_sin);
             } 
         }  }

这是工作节点上的控制台信息

[INFO] [10/08/2012 12:12:44.486] [main] [ActorSystem(WorkerNode)] REMOTE: 
RemoteServerStarted@akka://WorkerNode@127.0.0.1:2552

这是主节点上的控制台信息

[INFO] [10/08/2012 12:13:34.633] [main] [ActorSystem(MasterNode)] REMOTE:  
RemoteServerStarted@akka://MasterNode@127.0.0.1:2552

之后,对这种情况的可能过程以及建议的解决方案的任何想法都非常了解

1 个答案:

答案 0 :(得分:3)

嵌套部分中的“remote.netty.port = X”应为“port = X”