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

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

This example Akka source code file (ViewExample.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

abstractpersistentactor, abstractpersistentview, actor, actorref, actorsystem, akka, boxedunit, concurrent, duration, exampleview, override, partialfunction, persistence, receivebuilder, string, time

The ViewExample.java Akka example source code

/**
 * Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
 */

package sample.persistence;

import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.japi.pf.ReceiveBuilder;
import akka.persistence.*;
import scala.PartialFunction;
import scala.concurrent.duration.Duration;
import scala.runtime.BoxedUnit;

import java.util.concurrent.TimeUnit;

public class ViewExample {
  public static class ExamplePersistentActor extends AbstractPersistentActor {
    private int count = 1;

    @Override
    public String persistenceId() { return "sample-id-4"; }

    @Override
    public PartialFunction<Object, BoxedUnit> receiveCommand() {
      return ReceiveBuilder.
        match(String.class, s -> {
          System.out.println(String.format("persistentActor received %s (nr = %d)", s, count));
          persist(s + count, evt -> {
            count += 1;
          });
        }).
        build();
    }

    @Override
    public PartialFunction<Object, BoxedUnit> receiveRecover() {
      return ReceiveBuilder.
        match(String.class, s -> count += 1).
        build();
    }
  }

  public static class ExampleView extends AbstractPersistentView {

    private int numReplicated = 0;

    @Override public String persistenceId() { return "sample-id-4"; }
    @Override public String viewId() { return "sample-view-id-4"; }

    public ExampleView() {
      receive(ReceiveBuilder.
        match(Object.class, m -> isPersistent(), msg -> {
          numReplicated += 1;
          System.out.println(String.format("view received %s (num replicated = %d)",
            msg,
            numReplicated));
        }).
        match(SnapshotOffer.class, so -> {
          numReplicated = (Integer) so.snapshot();
          System.out.println(String.format("view received snapshot offer %s (metadata = %s)",
            numReplicated,
            so.metadata()));
        }).
        match(String.class, s -> s.equals("snap"), s -> saveSnapshot(numReplicated)).build()
      );
    }
  }

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

    system.scheduler()
      .schedule(Duration.Zero(),
        Duration.create(2, TimeUnit.SECONDS),
        persistentActor,
        "scheduled",
        system.dispatcher(),
        null);
    system.scheduler()
      .schedule(Duration.Zero(), Duration.create(5, TimeUnit.SECONDS), view, "snap", system.dispatcher(), null);
  }
}

Other Akka source code examples

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