Running the application

You can run the Hello World application from the command line or an IDE. The final topic in this guide describes how to run it from IntelliJ IDEA. But, before we run the application again, let’s take a quick look at the build files.

The build files

As you can see below the build files for Maven (pom.xml) and Gradle (build.gradle) used in this sample project are very simple. Please refer to the documentation of the build tool of your choice for more information.

Maven
<project>
    <modelVersion>4.0.0</modelVersion>

    <groupId>hello-akka-java</groupId>
    <artifactId>app</artifactId>
    <version>1.0</version>

    <dependencies>
        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-actor_2.12</artifactId>
            <version>2.5.3</version>
        </dependency>
        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-testkit_2.12</artifactId>
            <version>2.5.3</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <configuration>
                    <executable>java</executable>
                    <arguments>
                        <argument>-classpath</argument>
                        <classpath />
                        <argument>com.lightbend.akka.sample.AkkaQuickstart</argument>
                    </arguments>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
Gradle
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'application'


repositories {
    mavenCentral()
    mavenLocal()
}

dependencies {
  compile 'com.typesafe.akka:akka-actor_2.12:2.5.3'
  testCompile 'com.typesafe.akka:akka-testkit_2.12:2.5.3'
  testCompile 'junit:junit:4.12'
}

mainClassName = "com.lightbend.akka.sample.AkkaQuickstart"

run {
  standardInput = System.in
}

Running the project

Just as you did earlier, run the application from a console:

Maven
$ mvn compile exec:exec
Gradle
$ gradle run

The output should look something like this (scroll all the way to the right to see the Actor output):

Maven
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building app 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:exec (default-cli) @ app ---
>>> Press ENTER to exit <<<
[INFO] [05/11/2017 14:07:20.790] [helloakka-akka.actor.default-dispatcher-2] [akka://helloakka/user/printerActor] Hello, Java
[INFO] [05/11/2017 14:07:20.791] [helloakka-akka.actor.default-dispatcher-2] [akka://helloakka/user/printerActor] Good day, Play
[INFO] [05/11/2017 14:07:20.791] [helloakka-akka.actor.default-dispatcher-2] [akka://helloakka/user/printerActor] Howdy, Akka
[INFO] [05/11/2017 14:07:20.791] [helloakka-akka.actor.default-dispatcher-2] [akka://helloakka/user/printerActor] Howdy, Lightbend
Gradle
:compileJava UP-TO-DATE
:processResources NO-SOURCE
:classes UP-TO-DATE
:run
>>> Press ENTER to exit <<<
[INFO] [05/11/2017 14:08:22.884] [helloakka-akka.actor.default-dispatcher-2] [akka://helloakka/user/printerActor] Howdy, Akka
[INFO] [05/11/2017 14:08:22.884] [helloakka-akka.actor.default-dispatcher-2] [akka://helloakka/user/printerActor] Good day, Play
[INFO] [05/11/2017 14:08:22.884] [helloakka-akka.actor.default-dispatcher-2] [akka://helloakka/user/printerActor] Hello, Java
[INFO] [05/11/2017 14:08:22.884] [helloakka-akka.actor.default-dispatcher-2] [akka://helloakka/user/printerActor] Howdy, Lightbend
<=========----> 75% EXECUTING
> :run

Remember that we set our Printer Actor to use the Logger from Akka? This is why there is a lot of extra information when we log things. The log output contains information like when and from what actor it was logged. Let’s focus on the output from the Printer Actor for a while:

... Howdy, Akka
... Hello, Java
... Good day, Play
... Howdy, Lightbend

This is the result of our code that sends messages to the Greeter Actor:

howdyGreeter.tell(new WhoToGreet("Akka"), ActorRef.noSender());
howdyGreeter.tell(new Greet(), ActorRef.noSender());

howdyGreeter.tell(new WhoToGreet("Lightbend"), ActorRef.noSender());
howdyGreeter.tell(new Greet(), ActorRef.noSender());

helloGreeter.tell(new WhoToGreet("Java"), ActorRef.noSender());
helloGreeter.tell(new Greet(), ActorRef.noSender());

goodDayGreeter.tell(new WhoToGreet("Play"), ActorRef.noSender());
goodDayGreeter.tell(new Greet(), ActorRef.noSender());

To run the tests, enter the test task instead:

Maven
$ mvn test
Gradle
$ gradle test

Try running the code a couple of more times and make sure to notice the order of the logging. Did you notice that it can change from one run to another. What’s happening here? The asynchronous behavior becomes evident. This might be a new mental model for you. But, once you gain experience with it everything will become clear; just like for Neo in the Matrix.

Next steps

If you use IntelliJ, try integrating the sample project with IntelliJ IDEA.

To continue learning more about Akka and Actor Systems, look at the Getting Started Guide next. Happy hakking!