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

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

This example Akka source code file (DangerousJavaActor.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, callable, circuitbreaker, concurrent, dangerousjavaactor, duration, event, exception, future, loggingadapter, my, override, string, this, time

The DangerousJavaActor.java Akka example source code

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

//#imports1

import akka.actor.UntypedActor;
import scala.concurrent.Future;
import akka.event.LoggingAdapter;
import scala.concurrent.duration.Duration;
import akka.pattern.CircuitBreaker;
import akka.event.Logging;

import static akka.pattern.Patterns.pipe;
import static akka.dispatch.Futures.future;

import java.util.concurrent.Callable;

//#imports1

//#circuit-breaker-initialization
public class DangerousJavaActor extends UntypedActor {

  private final CircuitBreaker breaker;
  private final LoggingAdapter log = Logging.getLogger(getContext().system(), this);

  public DangerousJavaActor() {
    this.breaker = new CircuitBreaker(
      getContext().dispatcher(), getContext().system().scheduler(),
      5, Duration.create(10, "s"), Duration.create(1, "m"))
      .onOpen(new Runnable() {
        public void run() {
          notifyMeOnOpen();
        }
      });
  }

  public void notifyMeOnOpen() {
    log.warning("My CircuitBreaker is now open, and will not close for one minute");
  }
//#circuit-breaker-initialization

  //#circuit-breaker-usage
  public String dangerousCall() {
    return "This really isn't that dangerous of a call after all";
  }

  @Override
  public void onReceive(Object message) {
    if (message instanceof String) {
      String m = (String) message;
      if ("is my middle name".equals(m)) {
        pipe(breaker.callWithCircuitBreaker(
          new Callable<Future<String>>() {
            public Future<String> call() throws Exception {
              return future(
                      new Callable<String>() {
                          public String call() {
                            return dangerousCall();
                          }
                        }, getContext().dispatcher());
            }
          }), getContext().dispatcher()).to(getSender());
      }
      if ("block for me".equals(m)) {
        getSender().tell(breaker
          .callWithSyncCircuitBreaker(
            new Callable<String>() {
              @Override
              public String call() throws Exception {
                return dangerousCall();
              }
            }), getSelf());
      }
    }
  }
//#circuit-breaker-usage

}

Other Akka source code examples

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

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 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.