| 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

abstractpersistentactor, actor, actorsystem, akka, arraylist, boxedunit, examplestate, japi, override, partialfunction, persistence, receivebuilder, runtime, serializable, string

The Akka example source code

 * Copyright (C) 2009-2014 Typesafe Inc. <>

package sample.persistence;

import akka.persistence.AbstractPersistentActor;
import akka.persistence.SnapshotOffer;
import scala.PartialFunction;
import scala.runtime.BoxedUnit;

import java.util.ArrayList;

public class SnapshotExample {
  public static class ExampleState implements Serializable {
    private final ArrayList<String> received;

    public ExampleState() {
      this(new ArrayList<String>());

    public ExampleState(ArrayList<String> received) {
      this.received = received;

    public ExampleState copy() {
      return new ExampleState(new ArrayList<String>(received));

    public void update(String s) {

    public String toString() {
      return received.toString();

  public static class ExamplePersistentActor extends AbstractPersistentActor {
    private ExampleState state = new ExampleState();

    public PartialFunction<Object, BoxedUnit> receiveCommand() {
      return ReceiveBuilder.
        match(String.class, s -> s.equals("print"), s -> System.out.println("current state = " + state)).
        match(String.class, s -> s.equals("snap"), s ->
          // IMPORTANT: create a copy of snapshot
          // because ExampleState is mutable !!!
        match(String.class, s -> {
          persist(s, evt -> {

    public String persistenceId() { return "sample-id-3"; }

    public PartialFunction<Object, BoxedUnit> receiveRecover() {
      return ReceiveBuilder.
        match(String.class, evt -> state.update(evt)).
        match(SnapshotOffer.class, ss -> {
          System.out.println("offered state = " + ss);
          state = (ExampleState) ss.snapshot();

  public static void main(String... args) throws Exception {
    final ActorSystem system = ActorSystem.create("example");
    final ActorRef persistentActor = system.actorOf(Props.create(ExamplePersistentActor.class), "persistentActor-3-java");

    persistentActor.tell("a", null);
    persistentActor.tell("b", null);
    persistentActor.tell("snap", null);
    persistentActor.tell("c", null);
    persistentActor.tell("d", null);
    persistentActor.tell("print", null);


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.