1

I created a simple app with remote actor (example from here) :

object HelloRemote extends App  {
  val system = ActorSystem("HelloRemoteSystem")
  val remoteActor = system.actorOf(Props[RemoteActor], name = "RemoteActor")
  remoteActor ! "The RemoteActor is alive"
}

class RemoteActor extends Actor {
  def receive = {
    case msg: String =>
        println(s"RemoteActor received message '$msg'")
        sender ! "Hello from the RemoteActor"
  }
}

is it possible to send to it message from sbt shell ?

igx
  • 4,101
  • 11
  • 43
  • 88

2 Answers2

1

Only an actor reference is required to send messages to the Actor. e.g. You can do the same in the scala shell: Follow these:

import akka.actor._

Define your Actor in the shell.

class RemoteActor extends Actor {
        def receive = {
          case msg: String =>
              println(s"RemoteActor received message '$msg'")
              sender ! "Hello from the RemoteActor"
        }
        }

val system = ActorSystem("HelloRemoteSystem")
val remoteActor = system.actorOf(Props[RemoteActor], name = "RemoteActor")
remoteActor ! "The RemoteActor is alive"

Here remoteActor is the reference for the Actor instantiated. You can send messages from anywhere if 1. This is actor is alive and 2. Your are able to acuire an actor reference there.

Manish Mishra
  • 796
  • 6
  • 21
  • Thanks but when I try to ```import akka.actor._``` I am getting ```:25: error: not found: value akka import akka.actor._ ^``` or if I am trying from sbt shell : ```> import akka.actor._ [error] No valid parser available. [error] import akka.actor._``` – igx Feb 11 '17 at 19:46
  • I think you are not using the akka library with sbt console. Please refer to this SO link http://stackoverflow.com/questions/18812399/how-to-use-third-party-libraries-with-scala-repl for How to use libraries with sbt console – Manish Mishra Feb 12 '17 at 03:28
0

Only an Actor can send a message to another Actor.

Thomas Lehoux
  • 1,158
  • 9
  • 13
  • But is it possible to create a temp actor on sbt shell that will send messages to the remote actor – igx Feb 11 '17 at 17:48