├── project └── build.properties ├── src ├── test │ └── java │ │ └── sample │ │ ├── TestCountingService.java │ │ └── SpringTest.java └── main │ └── java │ └── sample │ ├── CountingService.java │ ├── SpringActorProducer.java │ ├── AppConfiguration.java │ ├── CountingActor.java │ ├── SpringExtension.java │ └── Main.java ├── README.md ├── activator.properties ├── .gitignore ├── LICENSE ├── COPYING └── tutorial └── index.html /project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version=0.13.8 2 | -------------------------------------------------------------------------------- /src/test/java/sample/TestCountingService.java: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Typesafe Activator template for Akka and Spring in Java. 2 | -------------------------------------------------------------------------------- /activator.properties: -------------------------------------------------------------------------------- 1 | name=akka-java-spring 2 | title=Akka Java Spring 3 | description=Illustrates how to Inject dependencies into Akka actors using Spring in Java 4 | tags=Basics,akka,java,spring,starter 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *# 2 | *.iml 3 | *.ipr 4 | *.iws 5 | *.pyc 6 | *.tm.epoch 7 | *.vim 8 | *-shim.sbt 9 | .idea/ 10 | /project/plugins/project 11 | project/boot 12 | target/ 13 | /logs 14 | .cache 15 | .classpath 16 | .project 17 | .settings -------------------------------------------------------------------------------- /src/main/java/sample/CountingService.java: -------------------------------------------------------------------------------- 1 | package sample; 2 | 3 | import javax.inject.Named; 4 | 5 | /** 6 | * A simple service that can increment a number. 7 | */ 8 | @Named("CountingService") 9 | public class CountingService { 10 | /** 11 | * Increment the given number by one. 12 | */ 13 | public int increment(int count) { 14 | return count + 1; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Activator Template by Typesafe 2 | 3 | Licensed under Public Domain (CC0) 4 | 5 | To the extent possible under law, the person who associated CC0 with 6 | this Activator Tempate has waived all copyright and related or neighboring 7 | rights to this Activator Template. 8 | 9 | You should have received a copy of the CC0 legalcode along with this 10 | work. If not, see . 11 | -------------------------------------------------------------------------------- /src/main/java/sample/SpringActorProducer.java: -------------------------------------------------------------------------------- 1 | package sample; 2 | 3 | import akka.actor.Actor; 4 | import akka.actor.IndirectActorProducer; 5 | import org.springframework.context.ApplicationContext; 6 | 7 | /** 8 | * An actor producer that lets Spring create the Actor instances. 9 | */ 10 | public class SpringActorProducer implements IndirectActorProducer { 11 | final ApplicationContext applicationContext; 12 | final String actorBeanName; 13 | 14 | public SpringActorProducer(ApplicationContext applicationContext, 15 | String actorBeanName) { 16 | this.applicationContext = applicationContext; 17 | this.actorBeanName = actorBeanName; 18 | } 19 | 20 | @Override 21 | public Actor produce() { 22 | return (Actor) applicationContext.getBean(actorBeanName); 23 | } 24 | 25 | @Override 26 | public Class extends Actor> actorClass() { 27 | return (Class extends Actor>) applicationContext.getType(actorBeanName); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/sample/AppConfiguration.java: -------------------------------------------------------------------------------- 1 | package sample; 2 | 3 | import akka.actor.ActorSystem; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.context.ApplicationContext; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.context.annotation.Configuration; 8 | import static sample.SpringExtension.SpringExtProvider; 9 | 10 | /** 11 | * The application configuration. 12 | */ 13 | @Configuration 14 | class AppConfiguration { 15 | 16 | // the application context is needed to initialize the Akka Spring Extension 17 | @Autowired 18 | private ApplicationContext applicationContext; 19 | 20 | /** 21 | * Actor system singleton for this application. 22 | */ 23 | @Bean 24 | public ActorSystem actorSystem() { 25 | ActorSystem system = ActorSystem.create("AkkaJavaSpring"); 26 | // initialize the application context in the Akka Spring Extension 27 | SpringExtProvider.get(system).initialize(applicationContext); 28 | return system; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/sample/CountingActor.java: -------------------------------------------------------------------------------- 1 | package sample; 2 | 3 | import akka.actor.UntypedActor; 4 | import javax.inject.Inject; 5 | import javax.inject.Named; 6 | import org.springframework.context.annotation.Scope; 7 | 8 | /** 9 | * An actor that can count using an injected CountingService. 10 | * 11 | * @note The scope here is prototype since we want to create a new actor 12 | * instance for use of this bean. 13 | */ 14 | @Named("CountingActor") 15 | @Scope("prototype") 16 | class CountingActor extends UntypedActor { 17 | 18 | public static class Count {} 19 | public static class Get {} 20 | 21 | // the service that will be automatically injected 22 | final CountingService countingService; 23 | 24 | @Inject 25 | public CountingActor(@Named("CountingService") CountingService countingService) { 26 | this.countingService = countingService; 27 | } 28 | 29 | private int count = 0; 30 | 31 | @Override 32 | public void onReceive(Object message) throws Exception { 33 | if (message instanceof Count) { 34 | count = countingService.increment(count); 35 | } else if (message instanceof Get) { 36 | getSender().tell(count, getSelf()); 37 | } else { 38 | unhandled(message); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/sample/SpringExtension.java: -------------------------------------------------------------------------------- 1 | package sample; 2 | 3 | import akka.actor.AbstractExtensionId; 4 | import akka.actor.ExtendedActorSystem; 5 | import akka.actor.Extension; 6 | import akka.actor.Props; 7 | import org.springframework.context.ApplicationContext; 8 | 9 | /** 10 | * An Akka Extension to provide access to Spring managed Actor Beans. 11 | */ 12 | public class SpringExtension extends 13 | AbstractExtensionId { 14 | 15 | /** 16 | * The identifier used to access the SpringExtension. 17 | */ 18 | public static SpringExtension SpringExtProvider = new SpringExtension(); 19 | 20 | /** 21 | * Is used by Akka to instantiate the Extension identified by this 22 | * ExtensionId, internal use only. 23 | */ 24 | @Override 25 | public SpringExt createExtension(ExtendedActorSystem system) { 26 | return new SpringExt(); 27 | } 28 | 29 | /** 30 | * The Extension implementation. 31 | */ 32 | public static class SpringExt implements Extension { 33 | private volatile ApplicationContext applicationContext; 34 | 35 | /** 36 | * Used to initialize the Spring application context for the extension. 37 | * @param applicationContext 38 | */ 39 | public void initialize(ApplicationContext applicationContext) { 40 | this.applicationContext = applicationContext; 41 | } 42 | 43 | /** 44 | * Create a Props for the specified actorBeanName using the 45 | * SpringActorProducer class. 46 | * 47 | * @param actorBeanName The name of the actor bean to create Props for 48 | * @return a Props that will create the named actor bean using Spring 49 | */ 50 | public Props props(String actorBeanName) { 51 | return Props.create(SpringActorProducer.class, 52 | applicationContext, actorBeanName); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/test/java/sample/SpringTest.java: -------------------------------------------------------------------------------- 1 | package sample; 2 | 3 | import akka.actor.ActorRef; 4 | import akka.actor.ActorSystem; 5 | import static akka.pattern.Patterns.ask; 6 | import akka.util.Timeout; 7 | import java.util.concurrent.TimeUnit; 8 | import static org.junit.Assert.assertEquals; 9 | import org.junit.Test; 10 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 11 | import sample.CountingActor.Count; 12 | import sample.CountingActor.Get; 13 | import static sample.SpringExtension.SpringExtProvider; 14 | import scala.concurrent.Await; 15 | import scala.concurrent.Future; 16 | import scala.concurrent.duration.FiniteDuration; 17 | 18 | public class SpringTest { 19 | @Test 20 | public void testSpring() throws Exception { 21 | // create a spring context and scan the classes 22 | AnnotationConfigApplicationContext ctx = 23 | new AnnotationConfigApplicationContext(); 24 | ctx.scan("sample"); 25 | ctx.refresh(); 26 | 27 | // get hold of the actor system 28 | ActorSystem system = ctx.getBean(ActorSystem.class); 29 | // use the Spring Extension to create props for a named actor bean 30 | ActorRef counter = system.actorOf( 31 | SpringExtProvider.get(system).props("CountingActor"), "counter"); 32 | 33 | // tell it to count three times 34 | counter.tell(new Count(), null); 35 | counter.tell(new Count(), null); 36 | counter.tell(new Count(), null); 37 | 38 | // check that it has counted correctly 39 | FiniteDuration duration = FiniteDuration.create(3, TimeUnit.SECONDS); 40 | Future result = ask(counter, new Get(), 41 | Timeout.durationToTimeout(duration)); 42 | assertEquals(3, Await.result(result, duration)); 43 | 44 | // shut down the actor system 45 | system.shutdown(); 46 | system.awaitTermination(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/sample/Main.java: -------------------------------------------------------------------------------- 1 | package sample; 2 | 3 | import akka.actor.ActorRef; 4 | import akka.actor.ActorSystem; 5 | import static akka.pattern.Patterns.ask; 6 | import akka.util.Timeout; 7 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 8 | import sample.CountingActor.Count; 9 | import sample.CountingActor.Get; 10 | import static sample.SpringExtension.SpringExtProvider; 11 | import scala.concurrent.Await; 12 | import scala.concurrent.Future; 13 | import scala.concurrent.duration.FiniteDuration; 14 | import java.util.concurrent.TimeUnit; 15 | 16 | /** 17 | * A main class to start up the application. 18 | */ 19 | public class Main { 20 | public static void main(String[] args) throws Exception { 21 | // create a spring context and scan the classes 22 | AnnotationConfigApplicationContext ctx = 23 | new AnnotationConfigApplicationContext(); 24 | ctx.scan("sample"); 25 | ctx.refresh(); 26 | 27 | // get hold of the actor system 28 | ActorSystem system = ctx.getBean(ActorSystem.class); 29 | // use the Spring Extension to create props for a named actor bean 30 | ActorRef counter = system.actorOf( 31 | SpringExtProvider.get(system).props("CountingActor"), "counter"); 32 | 33 | // tell it to count three times 34 | counter.tell(new Count(), null); 35 | counter.tell(new Count(), null); 36 | counter.tell(new Count(), null); 37 | 38 | // print the result 39 | FiniteDuration duration = FiniteDuration.create(3, TimeUnit.SECONDS); 40 | Future result = ask(counter, new Get(), 41 | Timeout.durationToTimeout(duration)); 42 | try { 43 | System.out.println("Got back " + Await.result(result, duration)); 44 | } catch (Exception e) { 45 | System.err.println("Failed getting result: " + e.getMessage()); 46 | throw e; 47 | } finally { 48 | system.shutdown(); 49 | system.awaitTermination(); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | Creative Commons Legal Code 2 | 3 | CC0 1.0 Universal 4 | 5 | CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE 6 | LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN 7 | ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS 8 | INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES 9 | REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS 10 | PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM 11 | THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED 12 | HEREUNDER. 13 | 14 | Statement of Purpose 15 | 16 | The laws of most jurisdictions throughout the world automatically confer 17 | exclusive Copyright and Related Rights (defined below) upon the creator 18 | and subsequent owner(s) (each and all, an "owner") of an original work of 19 | authorship and/or a database (each, a "Work"). 20 | 21 | Certain owners wish to permanently relinquish those rights to a Work for 22 | the purpose of contributing to a commons of creative, cultural and 23 | scientific works ("Commons") that the public can reliably and without fear 24 | of later claims of infringement build upon, modify, incorporate in other 25 | works, reuse and redistribute as freely as possible in any form whatsoever 26 | and for any purposes, including without limitation commercial purposes. 27 | These owners may contribute to the Commons to promote the ideal of a free 28 | culture and the further production of creative, cultural and scientific 29 | works, or to gain reputation or greater distribution for their Work in 30 | part through the use and efforts of others. 31 | 32 | For these and/or other purposes and motivations, and without any 33 | expectation of additional consideration or compensation, the person 34 | associating CC0 with a Work (the "Affirmer"), to the extent that he or she 35 | is an owner of Copyright and Related Rights in the Work, voluntarily 36 | elects to apply CC0 to the Work and publicly distribute the Work under its 37 | terms, with knowledge of his or her Copyright and Related Rights in the 38 | Work and the meaning and intended legal effect of CC0 on those rights. 39 | 40 | 1. Copyright and Related Rights. A Work made available under CC0 may be 41 | protected by copyright and related or neighboring rights ("Copyright and 42 | Related Rights"). Copyright and Related Rights include, but are not 43 | limited to, the following: 44 | 45 | i. the right to reproduce, adapt, distribute, perform, display, 46 | communicate, and translate a Work; 47 | ii. moral rights retained by the original author(s) and/or performer(s); 48 | iii. publicity and privacy rights pertaining to a person's image or 49 | likeness depicted in a Work; 50 | iv. rights protecting against unfair competition in regards to a Work, 51 | subject to the limitations in paragraph 4(a), below; 52 | v. rights protecting the extraction, dissemination, use and reuse of data 53 | in a Work; 54 | vi. database rights (such as those arising under Directive 96/9/EC of the 55 | European Parliament and of the Council of 11 March 1996 on the legal 56 | protection of databases, and under any national implementation 57 | thereof, including any amended or successor version of such 58 | directive); and 59 | vii. other similar, equivalent or corresponding rights throughout the 60 | world based on applicable law or treaty, and any national 61 | implementations thereof. 62 | 63 | 2. Waiver. To the greatest extent permitted by, but not in contravention 64 | of, applicable law, Affirmer hereby overtly, fully, permanently, 65 | irrevocably and unconditionally waives, abandons, and surrenders all of 66 | Affirmer's Copyright and Related Rights and associated claims and causes 67 | of action, whether now known or unknown (including existing as well as 68 | future claims and causes of action), in the Work (i) in all territories 69 | worldwide, (ii) for the maximum duration provided by applicable law or 70 | treaty (including future time extensions), (iii) in any current or future 71 | medium and for any number of copies, and (iv) for any purpose whatsoever, 72 | including without limitation commercial, advertising or promotional 73 | purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each 74 | member of the public at large and to the detriment of Affirmer's heirs and 75 | successors, fully intending that such Waiver shall not be subject to 76 | revocation, rescission, cancellation, termination, or any other legal or 77 | equitable action to disrupt the quiet enjoyment of the Work by the public 78 | as contemplated by Affirmer's express Statement of Purpose. 79 | 80 | 3. Public License Fallback. Should any part of the Waiver for any reason 81 | be judged legally invalid or ineffective under applicable law, then the 82 | Waiver shall be preserved to the maximum extent permitted taking into 83 | account Affirmer's express Statement of Purpose. In addition, to the 84 | extent the Waiver is so judged Affirmer hereby grants to each affected 85 | person a royalty-free, non transferable, non sublicensable, non exclusive, 86 | irrevocable and unconditional license to exercise Affirmer's Copyright and 87 | Related Rights in the Work (i) in all territories worldwide, (ii) for the 88 | maximum duration provided by applicable law or treaty (including future 89 | time extensions), (iii) in any current or future medium and for any number 90 | of copies, and (iv) for any purpose whatsoever, including without 91 | limitation commercial, advertising or promotional purposes (the 92 | "License"). The License shall be deemed effective as of the date CC0 was 93 | applied by Affirmer to the Work. Should any part of the License for any 94 | reason be judged legally invalid or ineffective under applicable law, such 95 | partial invalidity or ineffectiveness shall not invalidate the remainder 96 | of the License, and in such case Affirmer hereby affirms that he or she 97 | will not (i) exercise any of his or her remaining Copyright and Related 98 | Rights in the Work or (ii) assert any associated claims and causes of 99 | action with respect to the Work, in either case contrary to Affirmer's 100 | express Statement of Purpose. 101 | 102 | 4. Limitations and Disclaimers. 103 | 104 | a. No trademark or patent rights held by Affirmer are waived, abandoned, 105 | surrendered, licensed or otherwise affected by this document. 106 | b. Affirmer offers the Work as-is and makes no representations or 107 | warranties of any kind concerning the Work, express, implied, 108 | statutory or otherwise, including without limitation warranties of 109 | title, merchantability, fitness for a particular purpose, non 110 | infringement, or the absence of latent or other defects, accuracy, or 111 | the present or absence of errors, whether or not discoverable, all to 112 | the greatest extent permissible under applicable law. 113 | c. Affirmer disclaims responsibility for clearing rights of other persons 114 | that may apply to the Work or any use thereof, including without 115 | limitation any person's Copyright and Related Rights in the Work. 116 | Further, Affirmer disclaims responsibility for obtaining any necessary 117 | consents, permissions or other rights required for any use of the 118 | Work. 119 | d. Affirmer understands and acknowledges that Creative Commons is not a 120 | party to this document and has no duty or obligation with respect to 121 | this CC0 or use of the Work. 122 | -------------------------------------------------------------------------------- /tutorial/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Akka Java Spring - Activator Template 4 | 5 | 6 | 7 | Introduction 8 | 9 | This tutorial aims to show you how to inject resources into Akka actors 10 | using Spring. You will be guided through the different parts used to 11 | integrate Akka with Spring and also create a test wrapper for a service 12 | and rewire the service used during testing without any code changes needed 13 | to the actor or the application classes. 14 | 15 | 16 | Application Overview 17 | 18 | The application basically consist of a single actor defined in the 19 | 20 | CountingActor.java 21 | file. This actor uses a service defined in the CountingService.java file to increment a 23 | value. The CountingService is wired into the Actor via Spring. 24 | 25 | 26 | To drive the actor, there is a application defined in the 27 | 28 | Main.java file in the main method, that does the 29 | following: 30 | 31 | Initializes Spring 32 | Creates an actor system 33 | Creates a CountingActor 34 | Sends a couple of messages to the actor so that it uses the 35 | CountingService 36 | 37 | 38 | 39 | 40 | Running the Application 41 | 42 | The result of running the Main application is shown in the 43 | Run tab. On the left-hand side we can 44 | see the console output, which is "Got back 3" for the 45 | Main.java 46 | application. 47 | 48 | 49 | Running the Test 50 | 51 | There is also a Junit test for the CountingActor located in 52 | the 53 | SpringTest.java file. It is structured in the same way as 54 | the Main application. 55 | 56 | 57 | The result of running the SpringTest test is shown in the 58 | Test tab. On the left-hand side we can 59 | see the console output, which is "passed sample.SpringTest.testSpring". 60 | 61 | 62 | 63 | Exploring the Code: The Akka Extension 64 | 65 | To be able to use the Spring application context to let Spring create and 66 | wire up our actors, we need to store it away in a place that is easily 67 | accessible from within the actor system. This is an ideal use case for what 68 | is called an 69 | Akka Extension. An extension can be thought of as a per actor system 70 | singleton and the one in this tutorial is defined in the 72 | SpringExtension.java file. 73 | 74 | 75 | The extension consist of two parts. The SpringExtension class 76 | that defines the methods used by Akka to create the extension for an actor 77 | system, and the SpringExt class that defines the methods and 78 | fields available on the extension. 79 | 80 | 81 | On this extension, the SpringExt class, there are only two 82 | methods, initialize(ApplicationContext applicationContext) 83 | that is used during startup to initialize the extension with the right 84 | Spring application context, and props(String actorBeanName) 85 | that constructs a 86 | Props from an actor bean name, that is used to create an actor. 87 | 88 | 89 | The Props creation uses the SpringActorProducer 90 | that we will talk about next. 91 | 92 | 93 | 94 | Exploring the Code: The Spring Actor Producer 95 | 96 | To let Spring create the actor from a bean name we need a way to allow the 97 | Akka 98 | Props to delegate actor creation to Spring. This is done in the 100 | SpringActorProducer.java file. 101 | 102 | 103 | The SpringActorProducer implements the 105 | IndirectActorProducer interface which is a way to let the 106 | actor be 107 | created via a factory method. This interface has two methods that need 108 | to be implemented, actorClass that will return the type of the 109 | actor that will be created, and produce, that needs to create 110 | a new actor instance every time that it is called. 111 | 112 | 113 | 114 | Exploring the Code: The Spring Application Configuration 115 | 116 | To tie everything together and let the Akka actor system know about the 117 | Spring application context, we have a Spring configuration defined in the 118 | 119 | AppConfiguration.java file. 120 | 121 | 122 | The AppConfiguration has a single method actorSystem 123 | that is responsible for creating the ActorSystem singleton in this 124 | Spring application. The code creates the actor system and then initializes 125 | the SpringExtension with the Spring application context needed 126 | by the SpringActorProducer to create actor instances from bean 127 | names. 128 | 129 | 130 | 131 | Creating a Test Counting Service 132 | 133 | Since we want to be able to test our actor thoroughly, let's create a test 134 | counting service. In our tutorial it will just wrap the real counting 135 | service and do some extra book keeping, but it could just as easily be a 136 | complete mock of the service. 137 | 138 | 139 | Open the empty 140 | TestCountingService.java file, and add the code below. 141 | Remember to save the file afterwards. 142 | package sample; 143 | 144 | import javax.inject.Named; 145 | import java.util.concurrent.atomic.AtomicInteger; 146 | 147 | /** 148 | * A test counting service that wraps the normal counting service 149 | * and keeps track of the number of times that it has been called. 150 | */ 151 | @Named("TestCountingService") 152 | public class TestCountingService extends CountingService { 153 | 154 | private AtomicInteger called = new AtomicInteger(0); 155 | 156 | @Override 157 | public int increment(int count) { 158 | called.incrementAndGet(); 159 | return super.increment(count); 160 | } 161 | 162 | /** 163 | * How many times we have called this service. 164 | */ 165 | public int getNumberOfCalls() { 166 | return called.get(); 167 | } 168 | } 169 | 170 | 171 | 172 | A Closer Look at the Test Counting Service 173 | 174 | Lets take a closer look at the newly created test counting service in the 175 | file 176 | TestCountingService.java 177 | 178 | 179 | The class TestCountingService inherits from 180 | CountingService and overrides the method increment on 181 | line 16. In that method it keeps track of how many times it has been 182 | called, by using an internal counter named called and then it 183 | delegates to the real counting service by calling super.increment(count); 184 | 185 | 186 | It also has a method named getNumberOfCalls on line 24 that 187 | returns the value of the internal counter called that can be 188 | used during testing to verify how the service is being used by the actor. 189 | 190 | 191 | One important thing to note is the annotation on line 10 192 | @Named("TestCountingService"). This will allow us to wire together 193 | the TestCountingService with the CountingActor in 194 | the test during the next step. 195 | 196 | 197 | Wire up the Test Counting Service 198 | 199 | Now that you have created a test counting service it is time to start using 200 | it in the test. 201 | 202 | 203 | Open the 204 | SpringTest.java file. And between line 24 and 25, right after 205 | ctx.scan("sample");, add the two lines below. Remember 206 | to save the file afterwards. 207 | // wire up the test service instead of the normal service 208 | ctx.registerAlias("TestCountingService", "CountingService"); 209 | 210 | 211 | This alias will make sure that when we look for a bean named 212 | CountingService we will instead get the one named 213 | TestCountingService. Check that the tests are still green on the 214 | Test tab. 215 | 216 | 217 | We would also like to check that the actor actually called the service the 218 | right number of times by using the getNumberOfCalls method. 219 | Add the code below to the 220 | SpringTest.java file. on line 46, just above the comment 221 | // shut down the actor system. Remember to save the file 222 | afterwards. 223 | // check that it called the TestCountingService the right number of times 224 | TestCountingService testService = ctx.getBean(TestCountingService.class); 225 | assertEquals(3, testService.getNumberOfCalls()); 226 | 227 | 228 | 229 | Check that the tests are still green on the 230 | Test tab. 231 | 232 | 233 | 234 | Conclusions 235 | 236 | You now have a working sample that uses spring to inject a service or a 237 | test service into an actor. Feel free to experiment and change the actor 238 | 239 | CountingActor.java, service 240 | 241 | CountingService.java, test service 242 | 243 | TestCountingService.java and test 244 | 245 | SpringTest.java. 246 | 247 | 248 | The Akka extension 249 | SpringExtension.java, the actor producer 251 | SpringActorProducer.java and the application configuration 252 | 253 | AppConfiguration.java can be reused as the basis for your own 254 | Spring application. 255 | 256 | 257 | 258 | --------------------------------------------------------------------------------
This tutorial aims to show you how to inject resources into Akka actors 10 | using Spring. You will be guided through the different parts used to 11 | integrate Akka with Spring and also create a test wrapper for a service 12 | and rewire the service used during testing without any code changes needed 13 | to the actor or the application classes. 14 |
The application basically consist of a single actor defined in the 19 | 20 | CountingActor.java 21 | file. This actor uses a service defined in the CountingService.java file to increment a 23 | value. The CountingService is wired into the Actor via Spring. 24 |
CountingService
To drive the actor, there is a application defined in the 27 | 28 | Main.java file in the main method, that does the 29 | following: 30 |
main
CountingActor
The result of running the Main application is shown in the 43 | Run tab. On the left-hand side we can 44 | see the console output, which is "Got back 3" for the 45 | Main.java 46 | application. 47 |
Main
There is also a Junit test for the CountingActor located in 52 | the 53 | SpringTest.java file. It is structured in the same way as 54 | the Main application. 55 |
The result of running the SpringTest test is shown in the 58 | Test tab. On the left-hand side we can 59 | see the console output, which is "passed sample.SpringTest.testSpring". 60 |
SpringTest
To be able to use the Spring application context to let Spring create and 66 | wire up our actors, we need to store it away in a place that is easily 67 | accessible from within the actor system. This is an ideal use case for what 68 | is called an 69 | Akka Extension. An extension can be thought of as a per actor system 70 | singleton and the one in this tutorial is defined in the 72 | SpringExtension.java file. 73 |
The extension consist of two parts. The SpringExtension class 76 | that defines the methods used by Akka to create the extension for an actor 77 | system, and the SpringExt class that defines the methods and 78 | fields available on the extension. 79 |
SpringExtension
SpringExt
On this extension, the SpringExt class, there are only two 82 | methods, initialize(ApplicationContext applicationContext) 83 | that is used during startup to initialize the extension with the right 84 | Spring application context, and props(String actorBeanName) 85 | that constructs a 86 | Props from an actor bean name, that is used to create an actor. 87 |
initialize(ApplicationContext applicationContext)
props(String actorBeanName)
The Props creation uses the SpringActorProducer 90 | that we will talk about next. 91 |
Props
SpringActorProducer
To let Spring create the actor from a bean name we need a way to allow the 97 | Akka 98 | Props to delegate actor creation to Spring. This is done in the 100 | SpringActorProducer.java file. 101 |
The SpringActorProducer implements the 105 | IndirectActorProducer interface which is a way to let the 106 | actor be 107 | created via a factory method. This interface has two methods that need 108 | to be implemented, actorClass that will return the type of the 109 | actor that will be created, and produce, that needs to create 110 | a new actor instance every time that it is called. 111 |
IndirectActorProducer
actorClass
produce
To tie everything together and let the Akka actor system know about the 117 | Spring application context, we have a Spring configuration defined in the 118 | 119 | AppConfiguration.java file. 120 |
The AppConfiguration has a single method actorSystem 123 | that is responsible for creating the ActorSystem singleton in this 124 | Spring application. The code creates the actor system and then initializes 125 | the SpringExtension with the Spring application context needed 126 | by the SpringActorProducer to create actor instances from bean 127 | names. 128 |
AppConfiguration
actorSystem 123 |
Since we want to be able to test our actor thoroughly, let's create a test 134 | counting service. In our tutorial it will just wrap the real counting 135 | service and do some extra book keeping, but it could just as easily be a 136 | complete mock of the service. 137 |
Open the empty 140 | TestCountingService.java file, and add the code below. 141 | Remember to save the file afterwards. 142 |
package sample; 143 | 144 | import javax.inject.Named; 145 | import java.util.concurrent.atomic.AtomicInteger; 146 | 147 | /** 148 | * A test counting service that wraps the normal counting service 149 | * and keeps track of the number of times that it has been called. 150 | */ 151 | @Named("TestCountingService") 152 | public class TestCountingService extends CountingService { 153 | 154 | private AtomicInteger called = new AtomicInteger(0); 155 | 156 | @Override 157 | public int increment(int count) { 158 | called.incrementAndGet(); 159 | return super.increment(count); 160 | } 161 | 162 | /** 163 | * How many times we have called this service. 164 | */ 165 | public int getNumberOfCalls() { 166 | return called.get(); 167 | } 168 | }
Lets take a closer look at the newly created test counting service in the 175 | file 176 | TestCountingService.java 177 |
The class TestCountingService inherits from 180 | CountingService and overrides the method increment on 181 | line 16. In that method it keeps track of how many times it has been 182 | called, by using an internal counter named called and then it 183 | delegates to the real counting service by calling super.increment(count); 184 |
TestCountingService
180 | CountingService
increment
called
super.increment(count);
It also has a method named getNumberOfCalls on line 24 that 187 | returns the value of the internal counter called that can be 188 | used during testing to verify how the service is being used by the actor. 189 |
getNumberOfCalls
One important thing to note is the annotation on line 10 192 | @Named("TestCountingService"). This will allow us to wire together 193 | the TestCountingService with the CountingActor in 194 | the test during the next step.
192 | @Named("TestCountingService")
Now that you have created a test counting service it is time to start using 200 | it in the test. 201 |
Open the 204 | SpringTest.java file. And between line 24 and 25, right after 205 | ctx.scan("sample");, add the two lines below. Remember 206 | to save the file afterwards. 207 |
ctx.scan("sample");
// wire up the test service instead of the normal service 208 | ctx.registerAlias("TestCountingService", "CountingService");
This alias will make sure that when we look for a bean named 212 | CountingService we will instead get the one named 213 | TestCountingService. Check that the tests are still green on the 214 | Test tab. 215 |
212 | CountingService
213 | TestCountingService
We would also like to check that the actor actually called the service the 218 | right number of times by using the getNumberOfCalls method. 219 | Add the code below to the 220 | SpringTest.java file. on line 46, just above the comment 221 | // shut down the actor system. Remember to save the file 222 | afterwards. 223 |
221 | // shut down the actor system
// check that it called the TestCountingService the right number of times 224 | TestCountingService testService = ctx.getBean(TestCountingService.class); 225 | assertEquals(3, testService.getNumberOfCalls()); 226 |
Check that the tests are still green on the 230 | Test tab. 231 |
You now have a working sample that uses spring to inject a service or a 237 | test service into an actor. Feel free to experiment and change the actor 238 | 239 | CountingActor.java, service 240 | 241 | CountingService.java, test service 242 | 243 | TestCountingService.java and test 244 | 245 | SpringTest.java. 246 |
The Akka extension 249 | SpringExtension.java, the actor producer 251 | SpringActorProducer.java and the application configuration 252 | 253 | AppConfiguration.java can be reused as the basis for your own 254 | Spring application.