| career | drupal | java | mac | mysql | perl | scala | uml | unix  

Akka/Scala example source code file (

This example Akka source code file ( 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; 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, biginteger, callable, cluster, concurrent, dispatch, factorial, factorialbackend, factorialresult, future, integer, mapper, override, untypedactor

The Akka example source code

package sample.cluster.factorial;

import java.math.BigInteger;
import java.util.concurrent.Callable;
import scala.concurrent.Future;
import akka.dispatch.Mapper;
import static akka.dispatch.Futures.future;
import static akka.pattern.Patterns.pipe;

public class FactorialBackend extends UntypedActor {

  public void onReceive(Object message) {
    if (message instanceof Integer) {
      final Integer n = (Integer) message;
      Future<BigInteger> f = future(new Callable<BigInteger>() {
        public BigInteger call() {
          return factorial(n);
      }, getContext().dispatcher());

      Future<FactorialResult> result =
          new Mapper<BigInteger, FactorialResult>() {
            public FactorialResult apply(BigInteger factorial) {
              return new FactorialResult(n, factorial);
          }, getContext().dispatcher());

      pipe(result, getContext().dispatcher()).to(getSender());

    } else {

  BigInteger factorial(int n) {
    BigInteger acc = BigInteger.ONE;
    for (int i = 1; i <= n; ++i) {
      acc = acc.multiply(BigInteger.valueOf(i));
    return acc;

Other Akka source code examples

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

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

#1 New Release!

FP Best Seller


new blog posts


Copyright 1998-2021 Alvin Alexander,
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.