What is the difference between Typed and UnTyped Actors in Akka? When to use what?

北城以北 提交于 2019-12-03 01:12:30

UntypedActor is simply the name for Actor but as the Java API.

I'm very surprised that the documentation doesn't suffice:

Java:

Scala:

The difference is that TypedActors have a static interface, and the invocations of the methods on that interface is transformed into message sends. UntypedActors can receive any message.

Hope that helps.

Cheers, √

Actors (Untyped)

For actors (Scala) to receive messages, they must mixin the Consumer trait. For example, the following actor class (Consumer1) implements the endpointUri method, which is declared in the Consumer trait, in order to receive messages from the file:data/input/actor Camel endpoint. Untyped actors (Java) need to extend the abstract UntypedConsumerActor class and implement the getEndpointUri() and onReceive(Object) methods.

Actors (Typed)

Typed actors can also receive messages from Camel endpoints. In contrast to (untyped) actors, which only implement a single receive or onReceive method, a typed actor may define several (message processing) methods, each of which can receive messages from a different Camel endpoint. For a typed actor method to be exposed as Camel endpoint it must be annotated with the @consume annotation. For example, the following typed consumer actor defines two methods, foo and bar.

Reference

Untyped actors respond to messages sent, while typed actors respond to method calls(the parameter values are the messages).

Typed Actor Model is used in order to define strict contracts for actors that can respond to only the predefined set of messages. In this case, every message need not be encapsulated as one object; typed actors allow us to define separate methods that accept multiple inputs as defined by the contract. In Java parlance, typed actors provide the Java interface in the object-oriented world.[1]

[1] Akka Essentials

Akka untyped actors "do not compose" and "are not usefully typed".

This defeats the very reason we use Scala: it's functional programming power. See http://noelwelsh.com/programming/2013/03/04/why-i-dont-like-akka-actors/ for in depth on this mindset.

Also, see attempts to improve this in Scala to match the Haskell way.

http://letitcrash.com/post/40198993865/makkros-first-step-towards-typed-channels http://letitcrash.com/post/45188487245/the-second-step-akka-typed-channels

Seems to still be experimental in Akka or even dropped - not clear.

This idea of typed channels makes a lot of sense because similar issues occur when one tries to build type checked data (analytics) pipelines.

Akka Typed seems to be a great evolution to a safer programming model: https://opencredo.com/akka-typed/

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!