Akka Typed Actors

Akka Typed Actors

 

In short:

Akka aims to be the platform for the next-generation, event-driven, scalable, and fault-tolerant architectures on the JVM. One of the core features of Akka is an implementation of the Actor model. It alleviates the developer from having to deal with explicit locking and thread management. Using the Actor model raises the abstraction level and provides a better platform for building correct concurrent and scalable applications.

In Details:

Akka authors believe that writing correct concurrent, fault-tolerant and scalable applications is too hard. Most of the time it's because we are using the wrong tools and the wrong level of abstraction.

Akka hopes to change that. Using the Actor Model it raises the abstraction level and provides a better platform to build correct, concurrent, and scalable applications.

For fault-tolerance it adopts the "Let it crash" model which the telecom industry has used with great success to build applications that self-heal and systems that never stop.

Actors provide the abstraction for transparent distribution and the basis for truly scalable and fault-tolerant applications.

 

Akka is Open Source and available under the Apache 2 License.

 

Adoption:

Since Akka implies a different programming model than the standard n-tier OOP paradigm, it is more challenging in adaptation. Akka provides an adaptation bridge to simplify the embracing of Actors into your code without dramatically changing it.

Typed actors are nice for bridging between actor systems (the “inside”) and non-actor code (the “outside”), because they allow you to write normal OO-looking code on the outside. Think of them like doors: their practicality lies in interfacing between private sphere and the public.

 

Typed Actor

Turning method invocations into asynchronous dispatch instead of synchronous that has been the default way since Smalltalk came out.

 

Typed Actors consist of:

·        Public interface – used to communicate between different actors

·        Implementation – used to do actual business logic

 

Typed actors are nice for bridging between actor systems (the “inside”) and non-actor code (the “outside”), because they allow you to write normal OO-looking code on the outside.

Typed Actors are implemented using JDK Proxies

 

The advantage of Typed Actors is that with TypedActors you have a static contract, and don't need to define your own messages

Example

Creating Typed Actors

To create a Typed Actor you need to have one or more interfaces, and one implementation.

 

Public interface


public interface Squarer {
  void squareDontCare(int i); //fire-forget
  Future<Integer> square(int i); //non-blocking send-request-reply
  Option<Integer> squareNowPlease(int i);//blocking send-request-reply
  int squareNow(int i); //blocking send-request-reply
}

Implementation

class SquarerImpl implements Squarer {

private String name;

 

         public SquarerImpl() {

                   this.name = "default";}

          public SquarerImpl(String name) {

                                        this.name = name; }

          public void squareDontCare(int i) {

                                                                   int sq = i * i; }

                         public Future<Integer> square(int i) {

                                                                  return Futures.successful(i * i);}

                        public Option<Integer> squareNowPlease(int i) {

                                                                  return Option.some(i * i);}

                         public int squareNow(int i) {

                            return i * i;}

 

Instance creation

Squarer otherSquarer = TypedActor.get(system).typedActorOf(

new TypedProps<SquarerImpl>(Squarer.class, new Creator<SquarerImpl>() {

public SquarerImpl create() { return new SquarerImpl("foo"); }

}),"name");

Method dispatching

                void will be dispatched with fire-and-forget 

 

scala.concurrent.Future<?> will use send-request-reply semantics, exactly like ActorRef.ask

 

scala.Option<?> or akka.japi.Option<?> will use send-request-reply semantics, but will block to wait for an answer, and return None if no answer was produced within the timeout.

 

Any other type of value will use send-request-reply semantics, but will block to wait for an answer, throwingjava.util.concurrent.TimeoutException if there was a timeout or rethrow any exception that was thrown during this call.

Messages and immutability

One-way message send

mySquarer.squareDontCare(10);

The method will be executed on another thread; asynchronously.

 

Request-reply message send

1.  Option<Integer> oSquare = mySquarer.squareNowPlease(10); //Option[Int]

This will block for timeout if needed; It will return None if a timeout occurs.

1.  int iSquare = mySquarer.squareNow(10); //Int

This will block for timeout if needed,  throw java.util.concurrent.TimeoutException if a timeout occurs.

Request-reply-with-future message send

1.  Future<Integer> fSquare = mySquarer.square(10); //A Future[Int]

This call is asynchronous, and the Future returned can be used for asynchronous composition

 

Composable Futures:

Operations on a future or a collection of futures can be chained together without blocking (as seen in the fuse day)

 

Stopping Typed Actors

1.  TypedActor.get(system).stop(mySquarer);

This asynchronously stops the Typed Actor associated with the specified proxy ASAP.

1.  TypedActor.get(system).poisonPill(otherSquarer);

This asynchronously stops the Typed Actor associated with the specified proxy after it's done with all calls that were made prior to this call.

 

Typed Actor Hierarchies

Squarer childSquarer =

TypedActor.get(TypedActor.context()).

typedActorOf(

new TypedProps<SquarerImpl>(Squarer.class, SquarerImpl.class)

});

 

Receive arbitrary messages

 

Extend akka.actor.TypedActor.ReceiveronReceive(…) - method.

This allows you to react to DeathWatch Terminated-messages and other types of lifecycle/system messages.

Lifecycle callbacks

By having your Typed Actor implementation class implement any and all of the following:

TypedActor.PreStart

TypedActor.PostStop

TypedActor.PreRestart

TypedActor.PostRestart

You can hook into the lifecycle of your Typed Actor.

Proxying -  a kind of RPC

You can use the typedActorOf (takes a TypedProps and an ActorRef) to proxy the given ActorRef as a TypedActor.

This is usable if you want to communicate remotely with TypedActors on other machines, just look them up with actorFor and pass the ActorRef to typedActorOf.

 

 

Thank you for your interest!

We will contact you as soon as possible.

Send us a message

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com