Created Ping Pong Application in Akka using Java. I cannot make it to work.

28 views
Skip to first unread message

Sabyasachi Mohanty

unread,
Jun 14, 2018, 8:38:45 AM6/14/18
to Akka User List
Hello Guys..I am new to akka. Was trying out a remote ping pong application using java. I cannot make it to work. Can anyone tell me what am doing wrong. 

I have created 2 projects AkkaPing and AkkaPing, Below is the code snippets. 


public class AkkaPing {


        @SuppressWarnings("unused")

       public static void main(String[] args) {

               final ActorSystem pingSystem = ActorSystem.create("PingApplication",

                               ConfigFactory.load().getConfig("PingConfig"));

               final ActorSelection pongSelection = pingSystem

                               .actorSelection("akka.tcp://PongApp...@127.0.0.1:5152/user/PongActor");

               System.out.println("*************Sending the first Ping***********");

               pongSelection.tell(new Message("sunny"), ActorRef.noSender());

                final class PingActor extends UntypedAbstractActor {

                       public PingActor() {

                               super();

                       }

                        @Override

                       public void onReceive(Object message) {

                               if (message instanceof Message) {

                                       Message recMsg = (Message) message;

                                       System.out.println("Received Message: " + recMsg.toString());

                                       pongSelection.tell(recMsg, getSelf());

                               } else {

                                       System.out.println("UnHandled Message Received");

                                       unhandled(message);

                               }

                       }

               }

       }

}


application.conf for AkkaPing project.

PingConfig {

akka {

 actor {

    serializers {

     java = "akka.serialization.JavaSerializer"

     proto = "akka.remote.serialization.ProtobufSerializer"

     myown = "docs.serialization.MyOwnSerializer"

   }


    serialization-bindings {

     "java.lang.String" = java

     "docs.serialization.Customer" = java

     "com.google.protobuf.Message" = proto

     "docs.serialization.MyOwnSerializable" = myown

     "java.lang.Boolean" = myown

   }

   provider = remote

 }

 remote {

   akka.remote.trusted-selection-paths = ["/user/PongActor"]

   enabled-transports = ["akka.remote.netty.tcp"]

   netty.tcp {

     hostname = "127.0.0.1"

     port=5153

  }

}

}

}



 

public
class AkkaPong {

public static void main(String[] args) {

final ActorSystem pongSystem = ActorSystem.create("PongApplication",

ConfigFactory.load().getConfig("PongConfig"));

final ActorSelection pingSelection = pongSystem

.actorSelection("akka.tcp://PingApp...@127.0.0.1:5153/user/PingActor");



final class PongActor extends UntypedAbstractActor {

public PongActor() {

super();

}

@Override

public void onReceive(Object message) {


if (message instanceof Message) {

Message recMsg = (Message) message;

System.out.println("Received Message: " + recMsg.toString());

pingSelection.tell(recMsg, getSelf());

} else {

System.out.println("UnHandled Message Received");

unhandled(message);

}

}

}

}

}



application.conf for AkkaPong project

PongConfig {
akka {
  actor {
    provider = remote
  }
  remote {
    akka.remote.trusted-selection-paths = ["/user/PingActor"]
    enabled-transports = ["akka.remote.netty.tcp"]
    netty.tcp {
      hostname = "127.0.0.1"
      port=5152
}
 }
}
}









 


Matthew Howard

unread,
Jun 18, 2018, 1:28:05 PM6/18/18
to Akka User List
You've defined the Ping and Pong classes but nothing is instantiating them yet - each application needs to call the "constructor" for each actor it needs. You don't actually call the constructor yourself, but instead tell akka to do this by invoking the actorOf method:

pingSystem.actorOf(PingActor.props(), "PingActor");

pongSystem.actorOf(PongActor.props(), "PongActor");

So my guess is that your messages are going to the dead letter queue because no actor exists to handle them.  


On Thursday, June 14, 2018 at 8:38:45 AM UTC-4, Sabyasachi Mohanty wrote:
Hello Guys..I am new to akka. Was trying out a remote ping pong application using java. I cannot make it to work. Can anyone tell me what am doing wrong. 

I have created 2 projects AkkaPing and AkkaPing, Below is the code snippets. 


public class AkkaPing {


        @SuppressWarnings("unused")

       public static void main(String[] args) {

               final ActorSystem pingSystem = ActorSystem.create("PingApplication",

                               ConfigFactory.load().getConfig("PingConfig"));

               final ActorSelection pongSelection = pingSystem

                               .actorSelection("akka.tcp://PongAppl...@127.0.0.1:5152/user/PongActor");


application.conf for AkkaPing project.

.actorSelection("akka.tcp://PingAppl...@127.0.0.1:5153/user/PingActor");

Reply all
Reply to author
Forward
0 new messages