Question

I am trying to use Akka future with play framework to connect to a remote akka system . After running the system the akka future gives me a warning that one argument is left.

the code are below :

this is the [lay controller code: p

ackage controllers;

import com.typesafe.config.ConfigFactory;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import play.*;
import play.mvc.*;

import views.html.*;

public class Application extends Controller {



  public static Result index() throws InterruptedException { 

      System.out.println(" Local Node Called0");


      ActorSystem csystem = ActorSystem.create("Application", ConfigFactory.load().getConfig("LocalNode"));
      ActorRef localNode = csystem.actorOf(new Props(LocalNode.class));

        System.out.println(" Local Node Called1");
        localNode.tell("Hello");
        System.out.println(" Local Node Called2");

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

this is the play framework local actor node

package controllers;

import akka.actor.; import akka.dispatch.Await; import akka.dispatch.Future; import akka.event.Logging; import akka.event.LoggingAdapter; import akka.util.Duration; import akka.util.Timeout; import akka.pattern.;

public class LocalNode extends UntypedActor {

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

    ActorRef masterActor;

    public void preStart()
    {
        /* Get reference to Master Node*/
         masterActor = 

    getContext().actorFor("akka://MasterNode@127.0.0.1:2552/user/masterActor");
    }

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

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

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

            log.info("Messagefrom Server", result.toString());  
    }
   }

this is the remote akka system master nide

package Rubine_Cluster;

import com.typesafe.config.ConfigFactory;

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

/**
 * Hello world!
 *
 */
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();
          }
}  

this is the akka remote system MasterActor

package Rubine_Cluster;

import akka.actor.*;


public class MasterActor 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");       
    }
  }  
}

this is the message from the play framewrok

[INFO] [09/30/2012 16:47:25.669] [Application-akka.actor.default-dispatcher-1] [
akka://Application/user/$a] Messagefrom Server WARNING arguments left: 1

help is needed cos I am suppose to meet the assignment deadline

thanks to all

Was it helpful?

Solution

Try to give a name to your actor in Play:

ActorRef localNode = csystem.actorOf(new Props(LocalNode.class), "localNode");

And also note that if you need to access the Akka system from Play, it is better to use:

ActorSystem csystem = Akka.system();

instead of:

ActorSystem csystem = ActorSystem.create("Application", ConfigFactory.load().getConfig("LocalNode"));

OTHER TIPS

The "arguments left: 1" warning is due to the following line:

log.info("Messagefrom Server", result.toString())

You're passing unused params to the logger. It should be this instead:

log.info("Messagefrom Server {}", result.toString())
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top