Alan Coromano
Alan Coromano

Reputation: 26008

How to run Akka

It seems like there is no need in a class with a main method in it to be able to run Akka How to run akka actors in IntelliJ IDEA. However, here is what I have:

object Application extends App {
  val system = ActorSystem()
  val supervisor = system.actorOf(Props[Supervisor])
  implicit val timeout = Timeout(100 seconds)

  import system.dispatcher

  system.scheduler.schedule(1 seconds, 600 seconds) {
    val future = supervisor ? Supervisor.Start
    val list = Await.result(future, timeout.duration).asInstanceOf[List[Int]]
    supervisor ! list
  }
}

I know I have to specify a main method called "akka.Main" in the configuration. But nonetheless, where should I move the current code from object Application ?

Upvotes: 2

Views: 3060

Answers (3)

bsmk
bsmk

Reputation: 1347

You can write something like

import _root_.akka.Main
object Application extends App {
  Main.main(Array("somepackage.Supervisor"))
}

and Supervisor actor should have overriden preStart function as @cmbaxter suggested.

Then run sbt console in intellij and write run.

Upvotes: 5

cmbaxter
cmbaxter

Reputation: 35443

I agree with @kdrakon that your code is fine the way it is, but if you wanted to leverage the akka.Main functionality, then a simple refactor like so will make things work:

package code

class ApplicationActor extends Actor {

  override def preStart = {
    val supervisor = context.actorOf(Props[Supervisor])
    implicit val timeout = Timeout(100 seconds)

    import context.dispatcher

    context.system.scheduler.schedule(1 seconds, 600 seconds) {
      val future = (supervisor ? Supervisor.Start).mapTo[List[Int]]
      val list = Await.result(future, timeout.duration)
      supervisor ! list
    }
  }

  def receive = {
    case _ => //Not sure what to do here
  }

}

In this case, the ApplicationActor is the arg you would pass to akka.Main and it would basically be the root supervisor to all other actors created in your hierarchy. The only fishy thing here is that being an Actor, it needs a receive implementation and I don't imagine any other actors will be sending messages here thus it doesn't really do anything. But the power to this approach is that when the ApplicationActor is stopped, the stop will also be cascaded down to all other actors that it started, simplifying a graceful shutdown. I suppose you could have the ApplicationActor handle a message to shutdown the actor system given some kind of input (maybe a ShutdownHookThread could initiate this) and give this actor some kind of a purpose after all. Anyway, as stated earlier, your current approach seems fine but this could also be an option if you so desire.

EDIT

So if you wanted to run this ApplicationActor via akka.Main, according to the instructions here, you would execute this from your command prompt:

java -classpath <all those JARs> akka.Main code.ApplicationActor

You will of course need to supply <all those JARS> with your dependencies including akka. At a minimum you will need scala-library and akka-actor in your classpath to make this run.

Upvotes: 4

kdrakon
kdrakon

Reputation: 172

If you refer to http://doc.akka.io/docs/akka/snapshot/scala/hello-world.html, you'll find that akka.Main expects your root/parent Actor. In your case, Supervisor. As for your already existing code, it can be copied directly into the actors code, possibly in some initialisation calls. For example, refer to the HelloWorld's preStart function.

However, in my opinion, your already existing code is just fine too. Akka.main is a nice helper, as is the microkernel binary. But creating your own main executable is a viable option too.

Upvotes: 1

Related Questions