The Hello World Program

This section contains a simple, working Hello World program. We won’t get into too much details - you can find deeper information in the subsequent sections. We will define a reactor that waits for one incoming event, prints a message to the standard output, and then terminates.

import io.reactors._

object HelloWorld {
  def main(args: Array[String]) {
    val welcomeReactor = Reactor[String] {
      self => onEvent { name =>
          println(s"Welcome, $name!")
    val system = ReactorSystem.default("test-system")
    val ch = system.spawn(welcomeReactor)
    ch ! "Alan"

The program above declares an anonymous reactor called welcomeReactor, which waits for a name to arrive on its main event stream, prints that name, and then seals its main channel, therefore terminating itself. The main program then creates a new reactor system, uses the reactor template to start a new running instance of the reactor, and sends an event to it.

Above, we note the following:

  • A reactor is defined using the Reactor[T] constructor.
  • A reactor reacts to incoming events as specified in the callback function passed to the onEvent method.
  • Calling main.seal() terminates the reactor.
  • A reactor with a specific definition is started with the spawn method, which returns the reactor’s default channel.
  • Events are sent to the reactor by calling the ! operator on its channels.

The subsequent sections will explain these features in depth.

Note: If you are running the above example in IntelliJ or another IDE that runs your Scala programs in a separate JVM process, you need to ensure that this new JVM process does not die when the main function ends. Reactors run on daemon threads by default, so they will not prevent the JVM from terminating. There are several ways to fix this, and the easiest is to add a Thread.sleep at the end of the main function. A more sophisticated approach is to start your welcomeReactor on a dedicated thread instead of a thread pool:

val ch = system.spawn(