alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Akka/Scala example source code file (Main.java)

This example Akka source code file (Main.java) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Akka and Scala source code examples by using tags.

All credit for the original source code belongs to akka.io; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Akka tags/keywords

actor, actorref, actorsystem, add, akka, concurrent, const, divide, duration, exception, expression, finiteduration, main, multiply, time, util

The Main.java Akka example source code

package supervision;

import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.ActorSystem;
import akka.util.Timeout;
import scala.concurrent.Await;
import scala.concurrent.duration.Duration;
import scala.concurrent.duration.FiniteDuration;
import java.util.concurrent.TimeUnit;

import static supervision.Expression.*;
import static akka.pattern.Patterns.ask;
import static akka.japi.Util.classTag;

public class Main {

  public static void main(String[] args) throws Exception {
    ActorSystem system = ActorSystem.create("calculator-system");
    ActorRef calculatorService =
      system.actorOf(Props.create(ArithmeticService.class), "arithmetic-service");

    // (3 + 5) / (2 * (1 + 1))
    Expression task = new Divide(
      new Add(new Const(3), new Const(5)),
      new Multiply(
        new Const(2),
        new Add(new Const(1), new Const(1))
      )
    );

    FiniteDuration duration = Duration.create(1, TimeUnit.SECONDS);
    Integer result = Await.result(ask(calculatorService, task, new Timeout(duration)).mapTo(classTag(Integer.class)), duration);
    System.out.println("Got result: " + result);

    system.shutdown();
    system.awaitTermination();
  }
}

Other Akka source code examples

Here is a short list of links related to this Akka Main.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.