में असमर्थ करने के लिए एक संदेश भेजने के लिए एक अक्का रिमोट अभिनेता

0

सवाल

मैं नया हूँ करने के लिए अक्का remoting और मैं कोशिश कर रहा हूँ बस के लिए एक संदेश भेजने के लिए दूरदराज के एक अभिनेता और एक प्रतिक्रिया प्राप्त बदले में. मैं 2 अभिनेता सिस्टम पर स्थानीय होस्ट - अलग बंदरगाहों: MasterSystem और WorkerSystem. मैं बनाया है एक अभिनेता में WorkerSystem की कोशिश की और एक संदेश भेजने के लिए अपने दूरदराज के पते. लेकिन मैं रखने के लिए हो रही पर एक 'मृत पत्र का सामना करना पड़ा' संदेश! की सराहना करते हैं किसी भी मदद की है । धन्यवाद!

MainMaster.java

package pi_swarm_approx;

import akka.actor.ActorRef;
import akka.actor.ActorSelection;
import akka.actor.ActorSystem;
import akka.actor.UntypedActor;
import com.typesafe.config.ConfigFactory;

public class MainMaster extends UntypedActor{
    ActorSystem system;
    ActorRef actor;
    ActorSelection remoteActor;
    
    public MainMaster() {
        system = ActorSystem.create("MasterSystem", ConfigFactory.load("master"));
        System.out.println("MasterSystem created");
        MainWorker mw = new MainWorker();
        System.out.println("MainWorker obj created");
        remoteActor = mw.system.actorSelection("akka://WorkerSystem@localhost:2552/user/workerActor");
        
        System.out.println("Remote actor created");    
        remoteActor.tell("hello", getSelf());
        System.out.println("Message sent to remote actor");
    }
    
    public void onReceive(Object msg) {
        if (msg != null) {
            System.out.println("Got it back");
        }
         else {
            unhandled(msg);
            getContext().stop(getSelf());
         }
    }
}

MainWorker.java

package pi_swarm_approx;

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

public class MainWorker {
    ActorSystem system;
    ActorRef actor;
    public MainWorker() {
        this.system = ActorSystem.create("WorkerSystem", ConfigFactory.load("worker"));
        actor = system.actorOf(Props.create(Worker.class), "workerActor");
    }
}

Worker.java

package pi_swarm_approx;

import akka.actor.UntypedActor;

public class Worker extends UntypedActor {  
    public void onReceive(Object msg) {
        System.out.println("Worker actor got message");
        if (msg != null) {
            getSender().tell("Request processed", getSelf());
        }
         else {
            unhandled(msg);
         }
        getContext().stop(getSelf());
    }
}

मास्टर.conf

akka {
  actor {
    provider = "cluster"
  }
  remote {
  transport = ["akka.remote.netty.tcp"]
    netty.tcp {
      hostname = "localhost"
      port = 2551
    }
  }
  
  clustering {
    cluster.name = "MasterSystem"
    role = "master"
  }
 }

कार्यकर्ता.conf

akka {
  actor {
    provider = "cluster"
    deployment {
        /workerActor {
          remote = "akka.tcp://WorkerSystem@localhost:2552"
        }
  }
}

आउटपुट

In main
MasterSystem created
MainWorker obj created
Remote actor created
Message sent to remote actor
[INFO] [11/22/2021 16:01:34.531] [WorkerSystem-akka.actor.default-dispatcher-5] [akka://WorkerSystem/deadLetters] Message [java.lang.String] from Actor[akka://Main/user/app#402333018] to Actor[akka://WorkerSystem/deadLetters] was not delivered. [1] dead letters encountered. If this is not an expected behavior, then [Actor[akka://WorkerSystem/deadLetters]] may have terminated unexpectedly, This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
<=========----> 75% EXECUTING [18s]
akka akka-remoting java
2021-11-22 10:59:44
1

सबसे अच्छा जवाब

1

वहाँ रहे हैं कई समस्याओं के साथ कोड है कि आप को तैनात किया है । मैं पोस्ट कर रहा हूँ कम से कम एक काम कर कोड.

सबसे पहले, आप का उपयोग कर रहे हैं एक पदावनत संस्करण के akka.actor.UntypedActor. इस पदावनत किया गया था में 2.4.0. यदि आप उपयोग कर रहे हैं maven परिवर्तन की निर्भरता के अनुसार । कोड संकलित किया गया था और चलाने के लिए पर java 11.

का निर्माण.एसबीटी

libraryDependencies ++=
  Seq(
    "com.typesafe.akka" %% "akka-actor" % "2.6.17",
    "com.typesafe.akka" %% "akka-remote" % "2.6.17",
  )

के लिए provider मैं का इस्तेमाल किया है remote के बजाय cluster. आप उपयोग कर सकते हैं clusterहै , लेकिन सुनिश्चित करें कि आप जोड़ने के लिए आवश्यक निर्भरता. विन्यास किया जा सकता है आगे सरलीकृत हटाने के द्वारा repetitions, लेकिन आप ऐसा कर सकते हैं के रूप में आप का पता लगाने.

मास्टर.conf

akka {
  actor {
    provider = "remote"
  }
  remote {
    artery {
      enabled = on
      transport = tcp
      canonical {
        hostname = "127.0.0.1"
        port = 2552
      }
    }
  }
}

कार्यकर्ता.conf

akka {
  actor {
    provider = "remote"
  }
  remote {
    artery {
      enabled = on
      transport = tcp
      canonical {
        hostname = "127.0.0.1"
        port = 2551
      }
    }
  }
}

MainMaster.java

import akka.actor.AbstractActor;

public class MainMaster extends AbstractActor {
    @Override
    public Receive createReceive() {
        return receiveBuilder()
            .match(
                    String.class,
                    System.out::println)
            .matchAny(o -> System.out.println("received unknown message"))
            .build();
    }
}

Worker.java

public class Worker extends AbstractActor {

    @Override
    public Receive createReceive() {
        return receiveBuilder()
            .match(
                    String.class,
                    msg -> {
                        System.out.println(msg);
                        getSender().tell("Request processed", getSelf());
                    })
            .matchAny(o -> System.out.println("received unknown message"))
            .build();
    }
}

MainWorker.java

public class MainWorker {

    public static void main(String[] args) {
        ActorSystem system = ActorSystem.create("WorkerSystem", ConfigFactory.load("worker.conf"));
        ActorRef actor = system.actorOf(Props.create(Worker.class), "workerActor");
        System.out.println("worker started");
    }
}

Main.java

public class Main {
    public static void main(String[] args) {
        System.out.println("In main");
        ActorSystem system = ActorSystem.create("MasterSystem", ConfigFactory.load("master.conf"));
        ActorRef master = system.actorOf(Props.create(MainMaster.class), "master");

        ActorSelection remoteActor = system.actorSelection("akka://[email protected]:2551/user/workerActor");
        remoteActor.tell("Hello Worker", master);
    }
}
2021-11-22 17:59:51

उत्तर के लिए धन्यवाद. एक सवाल है, हालांकि, जब WorkerSystem बनाया? मैं देख नहीं है एक MainWorker चीज हो रही है कहीं भी बनाया.
Deepika Vemuri

दूरस्थ जा रहा है सिस्टम पर, आप पर विचार करना चाहिए MainWorker और Main के रूप में अलग-अलग अनुप्रयोगों और चलाने के रूप में इस तरह के । इस विशेष मामले में, चलाने के लिए MainWorker पहली बार के द्वारा पीछा किया Main इतना है कि एक उदाहरण के Worker प्रारंभ किया है और उपलब्ध है के लिए खोज से Main.
Johny T Koshy

दुर्भाग्य से, मैं अभी भी हो रही है 'मृत पत्र का सामना करना पड़ा' संदेश. मैं बनाने की कोशिश की workerActor में Main.java और ActorSelection remoteActor = system1.actorSelection("akka://WorkerSystem/user/workerActor"); काम करता है । लेकिन, ActorSelection remoteActor = system.actorSelection("akka://[email protected]:2551/user/workerActor"); नहीं करता है.
Deepika Vemuri

अगर MainWorker नहीं है पहले से ही चल रहा है जब आप शुरू Main, आप मिल जाएगा कि संदेश. की जाँच करें अगर है कि मामला है ।
Johny T Koshy

इसके अलावा, इस ActorRef actor = system1.actorOf(Props.create(Worker.class), "workerActor"); System.out.println(actor.path().address().host()); रिटर्न में कोई नहीं है ।
Deepika Vemuri

आप कर सकते हैं दिखाने के लिए अद्यतन की गई फ़ाइल की संरचना और फ़ाइलों github में?
Johny T Koshy


worker.conf और master.conf के तहत रखा जाता है resources/configs. वे पहुँचा रहे हैं के लिए ActorSystem रचना का उपयोग कर ActorSystem.create("MasterSystem", ConfigFactory.load("master.conf"));. डाल दिया conf फाइलों में resources फ़ोल्डर, यह काम करना चाहिए
Johny T Koshy

अन्य भाषाओं में

यह पृष्ठ अन्य भाषाओं में है

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................