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

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

This example Akka source code file (CalculatorActor.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, akka, calculatoractor, override, remote, untypedactor

The CalculatorActor.java Akka example source code

package sample.remote.calculator;

import akka.actor.UntypedActor;

public class CalculatorActor extends UntypedActor {
  @Override
  public void onReceive(Object message) {

    if (message instanceof Op.Add) {
      Op.Add add = (Op.Add) message;
      System.out.println("Calculating " + add.getN1() + " + " + add.getN2());
      Op.AddResult result = new Op.AddResult(add.getN1(), add.getN2(),
          add.getN1() + add.getN2());
      getSender().tell(result, getSelf());

    } else if (message instanceof Op.Subtract) {
      Op.Subtract subtract = (Op.Subtract) message;
      System.out.println("Calculating " + subtract.getN1() + " - "
          + subtract.getN2());
      Op.SubtractResult result = new Op.SubtractResult(subtract.getN1(),
          subtract.getN2(), subtract.getN1() - subtract.getN2());
      getSender().tell(result, getSelf());

    } else if (message instanceof Op.Multiply) {
      Op.Multiply multiply = (Op.Multiply) message;
      System.out.println("Calculating " + multiply.getN1() + " * "
          + multiply.getN2());
      Op.MultiplicationResult result = new Op.MultiplicationResult(
          multiply.getN1(), multiply.getN2(), multiply.getN1()
              * multiply.getN2());
      getSender().tell(result, getSelf());

    } else if (message instanceof Op.Divide) {
      Op.Divide divide = (Op.Divide) message;
      System.out.println("Calculating " + divide.getN1() + " / "
          + divide.getN2());
      Op.DivisionResult result = new Op.DivisionResult(divide.getN1(),
          divide.getN2(), divide.getN1() / divide.getN2());
      getSender().tell(result, getSelf());

    } else {
      unhandled(message);
    }
  }
}

Other Akka source code examples

Here is a short list of links related to this Akka CalculatorActor.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.