<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Akka &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/akka/feed/" rel="self" type="application/rss+xml" />
	<link>https://myshittycode.com</link>
	<description>Embracing the Messiness in Search of Epic Solutions</description>
	<lastBuildDate>Fri, 06 Jan 2023 16:38:12 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://myshittycode.com/wp-content/uploads/2022/04/cropped-icon-32x32.png</url>
	<title>Akka &#8211; My Shitty Code</title>
	<link>https://myshittycode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">205304208</site>	<item>
		<title>Akka: Spring Integration</title>
		<link>https://myshittycode.com/2015/08/26/akka-spring-integration/</link>
					<comments>https://myshittycode.com/2015/08/26/akka-spring-integration/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Wed, 26 Aug 2015 18:07:53 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Akka]]></category>
		<category><![CDATA[Spring]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=829</guid>

					<description><![CDATA[<p>PROBLEM To create Spring-managed prototype-scoped actors. SOLUTION The key to this solution is to create a custom implementation of IndirectActorProducer. From the documentation:- &#8220;&#8230;This interface defines a class of actor creation strategies deviating from the usual default of just reflectively instantiating the Actor subclass. It can be used to allow a dependency injection framework to [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2015/08/26/akka-spring-integration/">Akka: Spring Integration</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>To create Spring-managed prototype-scoped actors.</p>



<h2 class="wp-block-heading">SOLUTION</h2>



<p>The key to this solution is to create a custom implementation of <code>IndirectActorProducer</code>. From <a href="http://doc.akka.io/japi/akka/2.3.12/akka/actor/IndirectActorProducer.html" target="_blank" rel="noopener">the documentation</a>:-</p>



<p><em>&#8220;&#8230;This interface defines a class of actor creation strategies deviating from the usual default of just reflectively instantiating the Actor subclass. It can be used to allow a dependency injection framework to determine the actual actor class and how it shall be instantiated&#8230;&#8221;<br></em></p>



<p>First, create an implementation of <code>IndirectActorProducer</code> that creates the actor using Spring&#8217;s <code>ApplicationContext</code>.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; title: ; notranslate">
public final class SpringActorProducer implements IndirectActorProducer {
    private final ApplicationContext applicationContext;
    private final Class&lt;? extends Actor&gt; actorClass;

    public SpringActorProducer(final ApplicationContext applicationContext,
                               final Class&lt;? extends Actor&gt; actorClass) {
        this.applicationContext = applicationContext;
        this.actorClass = actorClass;
    }

    @Override
    public Actor produce() {
        return applicationContext.getBean(actorClass);
    }

    @Override
    public Class&lt;? extends Actor&gt; actorClass() {
        return actorClass;
    }
}
</pre></div>


<p>To prevent tedious auto-wiring <code>ApplicationContext</code> in every actor class to be passed to <code>SpringActorProducer</code> above, create a Spring-managed service that will do just that.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; title: ; notranslate">
@Component
public final class SpringProps {
    private final ApplicationContext applicationContext;

    @Autowired
    public SpringProps(final ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    public Props create(final Class&lt;? extends Actor&gt; actorClass) {
        return Props.create(SpringActorProducer.class,
                            applicationContext,
                            actorClass);
    }
}
</pre></div>


<p>Now, instead of creating the actors like this&#8230;.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; title: ; notranslate">
ActorRef master = actorSystem.actorOf(
                      Props.create(Master.class), &quot;master&quot;);

ActorRef workerRouter = getContext().actorOf(
                            Props.create(Worker.class)
                                 .withRouter(new RoundRobinPool(5)),
                            &quot;workerRouter&quot;);
</pre></div>


<p>&#8230; we will use the auto-wired <code>SpringProps</code> to create the actors&#8230;</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; title: ; notranslate">
@Autowire
private SpringProps springProps;

...

ActorRef master = actorSystem.actorOf(
                      springProps.create(Master.class), &quot;master&quot;);

ActorRef workerRouter = getContext().actorOf(
                            springProps.create(Worker.class)
                                       .withRouter(new RoundRobinPool(5)),
                            &quot;workerRouter&quot;);
</pre></div>


<p>Make sure all actor classes are Spring-managed with prototype-scoped by annotating them with <code>@Component</code> and <code>@Scope("prototype")</code>.</p>



<p>To see the full example, visit https://github.com/choonchernlim/test-akka-spring</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2015/08/26/akka-spring-integration/">Akka: Spring Integration</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2015/08/26/akka-spring-integration/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">829</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Database Caching 52/67 queries in 0.037 seconds using Disk

Served from: myshittycode.com @ 2026-02-18 10:21:05 by W3 Total Cache
-->