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

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

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

agent, agentdoctest, concurrent, duration, exception, executioncontext, future, integer, mapper, test, time

The AgentDocTest.java Akka example source code

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

import static org.junit.Assert.*;

import org.junit.Test;

import scala.concurrent.Await;
import scala.concurrent.duration.Duration;

//#import-agent
    import scala.concurrent.ExecutionContext;
    import akka.agent.Agent;
    import akka.dispatch.ExecutionContexts;
//#import-agent

//#import-function
    import akka.dispatch.Mapper;
//#import-function

//#import-future
    import scala.concurrent.Future;
//#import-future

public class AgentDocTest {

  private static ExecutionContext ec = ExecutionContexts.global();

  @Test
  public void createAndRead() throws Exception {
    //#create
    ExecutionContext ec = ExecutionContexts.global();
    Agent<Integer> agent = Agent.create(5, ec);
    //#create

    //#read-get
    Integer result = agent.get();
    //#read-get

    //#read-future
    Future<Integer> future = agent.future();
    //#read-future

    assertEquals(result, new Integer(5));
    assertEquals(Await.result(future, Duration.create(5,"s")), new Integer(5));
  }

  @Test
  public void sendAndSendOffAndReadAwait() throws Exception {
    Agent<Integer> agent = Agent.create(5, ec);

    //#send
    // send a value, enqueues this change
    // of the value of the Agent
    agent.send(7);

    // send a Mapper, enqueues this change
    // to the value of the Agent
    agent.send(new Mapper<Integer, Integer>() {
      public Integer apply(Integer i) {
        return i * 2;
      }
    });
    //#send

      Mapper<Integer, Integer> longRunningOrBlockingFunction = new Mapper<Integer, Integer>() {
      public Integer apply(Integer i) {
        return i * 1;
      }
    };

    ExecutionContext theExecutionContextToExecuteItIn = ec;
    //#send-off
    // sendOff a function
    agent.sendOff(longRunningOrBlockingFunction,
                  theExecutionContextToExecuteItIn);
    //#send-off

    assertEquals(Await.result(agent.future(), Duration.create(5,"s")), new Integer(14));
  }

    @Test
    public void alterAndAlterOff() throws Exception {
    Agent<Integer> agent = Agent.create(5, ec);

    //#alter
    // alter a value
    Future<Integer> f1 = agent.alter(7);

    // alter a function (Mapper)
    Future<Integer> f2 = agent.alter(new Mapper<Integer, Integer>() {
        public Integer apply(Integer i) {
            return i * 2;
        }
    });
    //#alter

    Mapper<Integer, Integer> longRunningOrBlockingFunction = new Mapper<Integer, Integer>() {
        public Integer apply(Integer i) {
            return i * 1;
        }
    };

    ExecutionContext theExecutionContextToExecuteItIn = ec;
    //#alter-off
    // alterOff a function (Mapper)
    Future<Integer> f3 = agent.alterOff(longRunningOrBlockingFunction,
                                        theExecutionContextToExecuteItIn);
    //#alter-off

    assertEquals(Await.result(f3, Duration.create(5,"s")), new Integer(14));
    }
}

Other Akka source code examples

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