{"id":193,"date":"2019-05-14T20:26:06","date_gmt":"2019-05-14T20:26:06","guid":{"rendered":"https:\/\/www.oreineke.dev\/?p=193"},"modified":"2026-03-07T10:38:10","modified_gmt":"2026-03-07T10:38:10","slug":"akka-typed-with-java","status":"publish","type":"post","link":"https:\/\/www.oreineke.dev\/?p=193","title":{"rendered":"Akka Typed (with Java)"},"content":{"rendered":"\n<p>This post is a followup to &#8220;<a href=\"https:\/\/www.oreineke.dev\/?p=1\">Akka Typed (with Scala)<\/a>&#8221; to demonstrate how to use Akka Typed with Java.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Akka?<\/h2>\n\n\n\n<p><a href=\"https:\/\/akka.io\">Akka <\/a>is a framework for writing message driven, scalable, error resilient applications, while making working with concurrency easy. This is achieved by modeling applications around the concept of &#8220;actors&#8221;. <\/p>\n\n\n\n<p>Actors are entities that communicate with each other through messages. When an actor receives a message, it can react by <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>changing its internal state, <\/li>\n\n\n\n<li>creating new actors, and <\/li>\n\n\n\n<li>sending out messages to actors.<\/li>\n<\/ul>\n\n\n\n<p>Multiple actors typically run in parallel, but the business logic within each actor is always executed single threaded, thereby avoiding complications that normally arise from concurrency.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Akka Typed<\/h2>\n\n\n\n<p>However, until the advent of <a href=\"https:\/\/doc.akka.io\/docs\/akka\/current\/typed\/index.html\">Akka Typed<\/a>, messages passed between actors have been untyped: You have been able to send any kind of message to any actor without the compiler being able to assert if the receiving actor is able to handle the message. This prevented detecting errors during compilation and made reasoning about actor driven applications difficult at times.<\/p>\n\n\n\n<p>With Akka Typed, you can now specify which message types an actor is able to handle and the compiler will complain about mismatching message types sent to an actor. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">An Example<\/h2>\n\n\n\n<p>The following is an example that shows the new Akka typed API in action (you can view and download the complete source code <a href=\"https:\/\/git.oreineke.dev\/olafr\/akka-typed-scala-java\">here<\/a>, including both the Scala and Java code):<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage dev.oreineke;\n\nimport akka.actor.typed.ActorSystem;\nimport akka.actor.typed.Behavior;\nimport akka.actor.typed.javadsl.Behaviors;\n\npublic class Main {\n    interface Message {}\n    static class Greet implements Message {\n        String name;\n\n        Greet(String name) {\n            this.name = name;\n        }\n    }\n    static class Stop implements Message {}\n\n    public static void main(String&#x5B;] args) {\n        Behavior&lt;Message&gt; readyToGreet = Behaviors.receive(Message.class)\n                .onMessage(Greet.class, (context, message) -&gt; {\n                    context.getLog().info(&quot;Hello &quot; + message.name + &quot;!!!&quot;);\n                    return Behaviors.same();\n                })\n                .onMessage((Stop.class), (context, message) -&gt; {\n                    context.getLog().info(&quot;Goodbye!&quot;);\n                    return Behaviors.stopped();\n                })\n                .build();\n\n        ActorSystem&lt;Message&gt; greeter = ActorSystem.create(readyToGreet, &quot;Greeter&quot;);\n\n        greeter.tell(new Greet(&quot;Peter&quot;));\n        greeter.tell(new Greet(&quot;Paul&quot;));\n        greeter.tell(new Greet(&quot;Marry&quot;));\n        greeter.tell(new Stop());\n    }\n}\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Explanation<\/h2>\n\n\n\n<p>After defining the type of message (<code>Message<\/code>) the actor is willing to accept,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n    interface Message {}\n    static class Greet implements Message {\n        String name;\n\n        Greet(String name) {\n            this.name = name;\n        }\n    }\n    static class Stop implements Message {}\n<\/pre><\/div>\n\n\n<p>a <code>Behavior&lt;Message&gt;<\/code> named <code>readyToGreet<\/code> is defined:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n        Behavior&lt;Message&gt; readyToGreet = Behaviors.receive(Message.class)\n                .onMessage(Greet.class, (context, message) -&gt; {\n                    context.getLog().info(&quot;Hello &quot; + message.name + &quot;!!!&quot;);\n                    return Behaviors.same();\n                })\n                .onMessage((Stop.class), (context, message) -&gt; {\n                    context.getLog().info(&quot;Goodbye!&quot;);\n                    return Behaviors.stopped();\n                })\n                .build();\n<\/pre><\/div>\n\n\n<p>Note that, to define the actor behavior <code>readyToGreet<\/code>, a typed factory method is used:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nBehaviors.receive(Message.class)\n    .onMessage(Class&lt;Message&gt; mclass, ...)\n    .onMessage(Class&lt;Message&gt; mclass, ...) \n<\/pre><\/div>\n\n\n<p><\/p>\n\n\n\n<p>Each <code>onMessage(...)<\/code> method expects, as second argument, a function, mapping the current context and the received message (of type <code>Message<\/code>) to a new <code>Behavior<\/code>. <\/p>\n\n\n\n<p>Here, for the returned <code>Behavior<\/code>s, instead of defining them manually, predefined factory methods are used to generate special return values: <code>Behaviors.same<\/code> to cause the current behavior to stay active, and <code>Behavior.stop<\/code> to cause the actor to die.<\/p>\n\n\n\n<p>Finally, <code>ActorSystem.create()<\/code> is called to create an actor with the <code>Behavior<\/code> <code>readyToGreet<\/code> (along with a name for easy actor identification):<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n        ActorSystem&lt;Message&gt; greeter = ActorSystem.create(readyToGreet, &quot;Greeter&quot;);\n<\/pre><\/div>\n\n\n<p>At this point, the actor <code>greeter<\/code> is ready to receive massages of type <code>Message<\/code>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n        greeter.tell(new Greet(&quot;Peter&quot;));\n        greeter.tell(new Greet(&quot;Paul&quot;));\n        greeter.tell(new Greet(&quot;Marry&quot;));\n        greeter.tell(new Stop());\n<\/pre><\/div>\n\n\n<p>If we would have tried to send a message of type <code>String<\/code> to the actor, the compiler would have noticed the error:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: scala; title: ; notranslate\" title=\"\">\n  greeter.tell(&quot;Eat this&quot;); \/\/ this will fail during compilation\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>This post is a followup to &#8220;Akka Typed (with Scala)&#8221; to demonstrate how to use Akka Typed with Java. What is Akka? Akka is a framework for writing message driven, scalable, error resilient applications, while making working with concurrency easy. This is achieved by modeling applications around the concept of &#8220;actors&#8221;. Actors are entities that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","osh_disable_topbar_sticky":"default","osh_disable_header_sticky":"default","osh_sticky_header_style":"default","osh_sticky_header_effect":"","osh_custom_sticky_logo":0,"osh_custom_retina_sticky_logo":0,"osh_custom_sticky_logo_height":0,"osh_background_color":"","osh_links_color":"","osh_links_hover_color":"","osh_links_active_color":"","osh_links_bg_color":"","osh_links_hover_bg_color":"","osh_links_active_bg_color":"","osh_menu_social_links_color":"","osh_menu_social_hover_links_color":"","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[4,9],"tags":[3,2],"class_list":["post-193","post","type-post","status-publish","format-standard","hentry","category-akka","category-java","tag-akka","tag-scala","entry"],"categories_names":["Akka","Java"],"post_class":[["post-193","post","type-post","status-publish","format-standard","hentry","category-akka","category-java","tag-akka","tag-scala","entry"]],"author_avatar":["<img alt='' src='https:\/\/secure.gravatar.com\/avatar\/a57dd6e6879c288f9d7c2e6b98faed481a067e5e155cc409952ac7e08d656a28?s=96&#038;d=mm&#038;r=g' srcset='https:\/\/secure.gravatar.com\/avatar\/a57dd6e6879c288f9d7c2e6b98faed481a067e5e155cc409952ac7e08d656a28?s=192&#038;d=mm&#038;r=g 2x' class='avatar avatar-96 photo' height='96' width='96' loading='lazy' decoding='async'\/>"],"featured_video":null,"_links":{"self":[{"href":"https:\/\/www.oreineke.dev\/index.php?rest_route=\/wp\/v2\/posts\/193","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.oreineke.dev\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oreineke.dev\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oreineke.dev\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oreineke.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=193"}],"version-history":[{"count":15,"href":"https:\/\/www.oreineke.dev\/index.php?rest_route=\/wp\/v2\/posts\/193\/revisions"}],"predecessor-version":[{"id":301,"href":"https:\/\/www.oreineke.dev\/index.php?rest_route=\/wp\/v2\/posts\/193\/revisions\/301"}],"wp:attachment":[{"href":"https:\/\/www.oreineke.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oreineke.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oreineke.dev\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}