{"id":140,"date":"2019-06-26T07:11:59","date_gmt":"2019-06-26T07:11:59","guid":{"rendered":"https:\/\/blog.adonlon.org\/?p=140"},"modified":"2019-12-14T12:53:30","modified_gmt":"2019-12-14T12:53:30","slug":"akka-typed-part-1-actors-and-actorsystems","status":"publish","type":"post","link":"https:\/\/blog.adonlon.org\/index.php\/2019\/06\/26\/akka-typed-part-1-actors-and-actorsystems\/","title":{"rendered":"Akka-Typed: Part 1 &#8211; Actors and ActorSystems"},"content":{"rendered":"\n<p>Akka-Typed has been described as production ready with the release of Akka 2.5.22 so it&#8217;s worth exploring what the move to Typed will mean for the interaction patterns we&#8217;re used to. There are many new ways of working with Actors in Typed. Here we&#8217;ll focus on the major differences around how ActorSystem and an Actor are represented in Typed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Key Differences<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">All New APIs<\/h3>\n\n\n\n<p>The first thing to remember is that <em>everything<\/em> is different in Akka-Typed, with all new dependencies and packages. This covers the range from core Actor APIs through to supervision, remoting, clustering, persistence and more. Many of the familiar classes are now generic, some are gone altogether and some new ones have put in an appearance. Inter-operability with a non-typed actor system is possible via <code><code><code>akka.actor.ActorSystem<\/code><\/code><\/code>but, as with Java&#8217;s Collections, the major benefits will come with migrating 100% to Typed, and thus <code><code><code>akka.actor.typed.ActorSystem<\/code><\/code><\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">No More Actors<\/h3>\n\n\n\n<p>Perhaps the most obvious difference is that there&#8217;s no more <code>Actor<\/code> in Akka-Typed. In fairness, the distinction between <code>Actor<\/code>and <code>ActorRef<\/code>was always confusing so having a clearer distinction between the building blocks of an Actor and a reference <strong>to<\/strong> and Actor can only be a good thing.<\/p>\n\n\n\n<p>The key new type then is a <code>akka.actor.typed.Behavior[T]<\/code>. A Behavior is basically a receiver for a message, of type T. On receipt of a message a behavior performs whatever action needs to happen and returns a <em>new<\/em> Behavior which the Actor switches to (aka &#8216;become&#8217;s in old money). A Behavior can be either a total or a partial function over T, with glue classes existing to help define new behaviors (<code>Behaviors.receive<\/code>, <code>Behaviors.receiveMsg<\/code>, <code>Behaviors.receivePartial<\/code>, &#8230;) and to return next states (<code>Behaviors.same<\/code>, <code>Behaviors.unhandled<\/code>, &#8230;). The new API is quite functional but also covers allows for an object-oriented approach via <code>AbstractBehavior[T]<\/code>. By explicitly returning a new behavior the model more cleanly supports the implementation of a state machine pattern, as well as the use of immutable patterns for managing mutable state.<\/p>\n\n\n\n<p>In the Typed world, an Actor is created (aka &#8216;spawn&#8217;ed) from an <code>ActorSystem<\/code>by specifying a <code>Behavior<\/code>and possibly also giving it a name. The Actor itself is never really visible to either the caller, who interacts with an <code>ActorRef<\/code>, or the implementer who only deals with <code>Behavior<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ActorSystem Is An Actor<\/h3>\n\n\n\n<p>The new <code>akka.actor.typed.ActorSystem[T]<\/code>is itself an Actor. This can be a bit of a head-melter at first but at its heart the idea is that the ActorSystem can be the first actor you interact with from *outside* the Actor system and that interactions proceed from there via message passing to other Actors which are contained within that single system. It&#8217;s still possible to locate an Actor within the system and send a message to it but the easier pattern of interaction is to go via the <code>ActorSystem<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Props is Gone<\/h3>\n\n\n\n<p>There are no more <code>props<\/code> methods to create, but they&#8217;ve been replaced by a different kind of factory &#8211; a behavior factory which returns a Behavior.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Interactions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Sending A Message<\/h3>\n\n\n\n<p>Once you have an <code>akka.actor.typed.ActorRef[T]<\/code>it&#8217;s quite easy to send a message to it. Let&#8217;s say I have a Play Controller class, which has a reference to a <code>SomeActor[Command]<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">final class SomeController(myActorRef: ActorRef[SomeActor.Command], cc: ControllerComponents)(implicit timeout: Timeout, scheduler: Scheduler, ec: ExecutionContext) extends AbstractController(cc) {\n  override def someEntryPoint = <em>Action(parse.json) <\/em>{ request =&gt;\n    myActorRef ! SomeActor.<em>FirstCommand<\/em>(request)\n    Response.ACCEPTED\n  }\n}\n\nfinal class SomeActor extends AbstractBehavior[SomeActor.Command] {\n  override def onMessage(msg: SomeActor.Command): Behavior[SomeActor.Command] = {\n    msg match {\n      case authorizationRequest: SomeActor.AuthorizeCardPayment =&gt;\n        \/\/ Do Stuff Here\n        Behaviors.<em>same\n    <\/em>}\n  }\n}\n\nobject AuthorizationRequest {\n<em>  <\/em>def behavior: Behavior[Command] = Behaviors.<em>setup<\/em>(new AuthorizationRequest)\n\n  sealed trait Command\n  case class FirstCommand(requestData: JsValue) extends Command\n}<\/pre>\n\n\n\n<p>What we have here then is a sealed trait (<code>Command<\/code>) which acts as the protocol description for the actor. The <code>behavior<\/code> method is used to initialise the actor based on a factory which is able to construct a new Behavior within a given context. The Controller receives a HTTP request, deserialises it as a JSON entity, and fires it into the actor.<\/p>\n\n\n\n<p>See part 2 on how to receive a response and the Ask pattern. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Akka-Typed has been described as production ready with the release of Akka 2.5.22 so it&#8217;s worth exploring what the move to Typed will mean for the interaction patterns we&#8217;re used to. There are many new ways of working with Actors in Typed. Here we&#8217;ll focus on the major differences around how ActorSystem and an Actor [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[6],"class_list":["post-140","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-akka"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Akka-Typed: Part 1 - Actors and ActorSystems - Pragmatic Software Engineering<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.adonlon.org\/index.php\/2019\/06\/26\/akka-typed-part-1-actors-and-actorsystems\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Akka-Typed: Part 1 - Actors and ActorSystems - Pragmatic Software Engineering\" \/>\n<meta property=\"og:description\" content=\"Akka-Typed has been described as production ready with the release of Akka 2.5.22 so it&#8217;s worth exploring what the move to Typed will mean for the interaction patterns we&#8217;re used to. There are many new ways of working with Actors in Typed. Here we&#8217;ll focus on the major differences around how ActorSystem and an Actor [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.adonlon.org\/index.php\/2019\/06\/26\/akka-typed-part-1-actors-and-actorsystems\/\" \/>\n<meta property=\"og:site_name\" content=\"Pragmatic Software Engineering\" \/>\n<meta property=\"article:published_time\" content=\"2019-06-26T07:11:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-12-14T12:53:30+00:00\" \/>\n<meta name=\"author\" content=\"alastair\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"alastair\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/blog.adonlon.org\/index.php\/2019\/06\/26\/akka-typed-part-1-actors-and-actorsystems\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/blog.adonlon.org\/index.php\/2019\/06\/26\/akka-typed-part-1-actors-and-actorsystems\/\"},\"author\":{\"name\":\"alastair\",\"@id\":\"https:\/\/blog.adonlon.org\/#\/schema\/person\/4c5a1b2463425db9814bf8540f8954c1\"},\"headline\":\"Akka-Typed: Part 1 &#8211; Actors and ActorSystems\",\"datePublished\":\"2019-06-26T07:11:59+00:00\",\"dateModified\":\"2019-12-14T12:53:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/blog.adonlon.org\/index.php\/2019\/06\/26\/akka-typed-part-1-actors-and-actorsystems\/\"},\"wordCount\":591,\"publisher\":{\"@id\":\"https:\/\/blog.adonlon.org\/#\/schema\/person\/4c5a1b2463425db9814bf8540f8954c1\"},\"keywords\":[\"akka\"],\"articleSection\":[\"Uncategorized\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blog.adonlon.org\/index.php\/2019\/06\/26\/akka-typed-part-1-actors-and-actorsystems\/\",\"url\":\"https:\/\/blog.adonlon.org\/index.php\/2019\/06\/26\/akka-typed-part-1-actors-and-actorsystems\/\",\"name\":\"Akka-Typed: Part 1 - Actors and ActorSystems - Pragmatic Software Engineering\",\"isPartOf\":{\"@id\":\"https:\/\/blog.adonlon.org\/#website\"},\"datePublished\":\"2019-06-26T07:11:59+00:00\",\"dateModified\":\"2019-12-14T12:53:30+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/blog.adonlon.org\/index.php\/2019\/06\/26\/akka-typed-part-1-actors-and-actorsystems\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blog.adonlon.org\/index.php\/2019\/06\/26\/akka-typed-part-1-actors-and-actorsystems\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blog.adonlon.org\/index.php\/2019\/06\/26\/akka-typed-part-1-actors-and-actorsystems\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/blog.adonlon.org\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Akka-Typed: Part 1 &#8211; Actors and ActorSystems\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blog.adonlon.org\/#website\",\"url\":\"https:\/\/blog.adonlon.org\/\",\"name\":\"Pragmatic Software Engineering\",\"description\":\"Random thoughts and insights on software engineering\",\"publisher\":{\"@id\":\"https:\/\/blog.adonlon.org\/#\/schema\/person\/4c5a1b2463425db9814bf8540f8954c1\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blog.adonlon.org\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/blog.adonlon.org\/#\/schema\/person\/4c5a1b2463425db9814bf8540f8954c1\",\"name\":\"alastair\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/blog.adonlon.org\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cf9abf1220f449fccd0f6795d12b05cb?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cf9abf1220f449fccd0f6795d12b05cb?s=96&d=mm&r=g\",\"caption\":\"alastair\"},\"logo\":{\"@id\":\"https:\/\/blog.adonlon.org\/#\/schema\/person\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Akka-Typed: Part 1 - Actors and ActorSystems - Pragmatic Software Engineering","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.adonlon.org\/index.php\/2019\/06\/26\/akka-typed-part-1-actors-and-actorsystems\/","og_locale":"en_GB","og_type":"article","og_title":"Akka-Typed: Part 1 - Actors and ActorSystems - Pragmatic Software Engineering","og_description":"Akka-Typed has been described as production ready with the release of Akka 2.5.22 so it&#8217;s worth exploring what the move to Typed will mean for the interaction patterns we&#8217;re used to. There are many new ways of working with Actors in Typed. Here we&#8217;ll focus on the major differences around how ActorSystem and an Actor [&hellip;]","og_url":"https:\/\/blog.adonlon.org\/index.php\/2019\/06\/26\/akka-typed-part-1-actors-and-actorsystems\/","og_site_name":"Pragmatic Software Engineering","article_published_time":"2019-06-26T07:11:59+00:00","article_modified_time":"2019-12-14T12:53:30+00:00","author":"alastair","twitter_card":"summary_large_image","twitter_misc":{"Written by":"alastair","Estimated reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.adonlon.org\/index.php\/2019\/06\/26\/akka-typed-part-1-actors-and-actorsystems\/#article","isPartOf":{"@id":"https:\/\/blog.adonlon.org\/index.php\/2019\/06\/26\/akka-typed-part-1-actors-and-actorsystems\/"},"author":{"name":"alastair","@id":"https:\/\/blog.adonlon.org\/#\/schema\/person\/4c5a1b2463425db9814bf8540f8954c1"},"headline":"Akka-Typed: Part 1 &#8211; Actors and ActorSystems","datePublished":"2019-06-26T07:11:59+00:00","dateModified":"2019-12-14T12:53:30+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.adonlon.org\/index.php\/2019\/06\/26\/akka-typed-part-1-actors-and-actorsystems\/"},"wordCount":591,"publisher":{"@id":"https:\/\/blog.adonlon.org\/#\/schema\/person\/4c5a1b2463425db9814bf8540f8954c1"},"keywords":["akka"],"articleSection":["Uncategorized"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/blog.adonlon.org\/index.php\/2019\/06\/26\/akka-typed-part-1-actors-and-actorsystems\/","url":"https:\/\/blog.adonlon.org\/index.php\/2019\/06\/26\/akka-typed-part-1-actors-and-actorsystems\/","name":"Akka-Typed: Part 1 - Actors and ActorSystems - Pragmatic Software Engineering","isPartOf":{"@id":"https:\/\/blog.adonlon.org\/#website"},"datePublished":"2019-06-26T07:11:59+00:00","dateModified":"2019-12-14T12:53:30+00:00","breadcrumb":{"@id":"https:\/\/blog.adonlon.org\/index.php\/2019\/06\/26\/akka-typed-part-1-actors-and-actorsystems\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.adonlon.org\/index.php\/2019\/06\/26\/akka-typed-part-1-actors-and-actorsystems\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.adonlon.org\/index.php\/2019\/06\/26\/akka-typed-part-1-actors-and-actorsystems\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.adonlon.org\/"},{"@type":"ListItem","position":2,"name":"Akka-Typed: Part 1 &#8211; Actors and ActorSystems"}]},{"@type":"WebSite","@id":"https:\/\/blog.adonlon.org\/#website","url":"https:\/\/blog.adonlon.org\/","name":"Pragmatic Software Engineering","description":"Random thoughts and insights on software engineering","publisher":{"@id":"https:\/\/blog.adonlon.org\/#\/schema\/person\/4c5a1b2463425db9814bf8540f8954c1"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.adonlon.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":["Person","Organization"],"@id":"https:\/\/blog.adonlon.org\/#\/schema\/person\/4c5a1b2463425db9814bf8540f8954c1","name":"alastair","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/blog.adonlon.org\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cf9abf1220f449fccd0f6795d12b05cb?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cf9abf1220f449fccd0f6795d12b05cb?s=96&d=mm&r=g","caption":"alastair"},"logo":{"@id":"https:\/\/blog.adonlon.org\/#\/schema\/person\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/blog.adonlon.org\/index.php\/wp-json\/wp\/v2\/posts\/140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.adonlon.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.adonlon.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.adonlon.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.adonlon.org\/index.php\/wp-json\/wp\/v2\/comments?post=140"}],"version-history":[{"count":0,"href":"https:\/\/blog.adonlon.org\/index.php\/wp-json\/wp\/v2\/posts\/140\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.adonlon.org\/index.php\/wp-json\/wp\/v2\/media?parent=140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.adonlon.org\/index.php\/wp-json\/wp\/v2\/categories?post=140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.adonlon.org\/index.php\/wp-json\/wp\/v2\/tags?post=140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}