Chris goes through a deeper dive on actors and streams. We review additional examples and variations of implementing these patterns.
- [Instructor] In this video, we will build a full application using Akka and Spring. We will follow the exact same process as last chapter to generate a basic Spring Boot project, using Spring Initializr. We will then integrate the Akka library. Finally, we will build our application, using Reactive Spring and Akka components. We will create three Actors, a Poller Actor, which maintains a timer and periodically sends a message to the PriceRequestor Actor.
The PriceRequestor Actor will make a REST API call and will send a message to the Printer Actor. And finally the Printer Actor will parse the JSON response and print out a message to the console. All of the Spring configuration and components in the Akka project are re-used from the RxJava project. Therefore we will not be going over them again. We will start with a preconfigured Spring project and build from there.
Let's jump into IntelliJ and first off let's create a new package called actors. Let's right click on the Root package, select New and Package, then type in actors. Okay now let's create a new class called Poller in the actors package.
This Poller Actor will need access to timers, so we will extend the AbstractActorWithTimers class. Okay, mouse over the name, click on the red lightbulb, and select Implement Methods. And let's implement the createRecieve method. Alright, let's create three fields, a TICK_KEY, a requestorActor ActorRef and a string called cryptoName.
Let's import the ActorRef class using the keyboard shortcut Alt and Enter. Now let's use the keyboard shortcut and generate a Constructor. Okay, let's be sure to select the requestorActor field as well as the cryptoName field. Now let's create a Props method which leverages that Constructor.
Okay let's call the Props.create method passing in the Poller class and also a lambda with a reference to a new instance of Poller. Now let's import the Props class. Let's make sure our parameters are in the correct order.
Now let's create two classes to represent our two messages to be used, the first class will be called FirstTick. FirstTick will represent the first tick of the timer. The next class will be called Tick, Tick will represent every subsequent tick after FirstTick, let's head back to our constructor. Let's call the getTimers method which is inherited from the AbstractActorWithTimers class.
Let's startSingleTimer, passing in the TICK_KEY, passing in a instance of the FirstTick message, and the Duration, 3000 milliseconds. Now let's build the createRecieve method, let's call recieveBuilder and then let's call match.
We are gonna match on the FirstTick message class and then also pass in a handler. When a Poller receives a FirstTick class, it will start a new periodic timer, passing in TICK_KEY, a new Tick message, and a Duration of three seconds.
Let's format for readability, then let's also match on the Tick message. For now, each Tick will just print out a message to the console. Then let's build our receive builder.
And then let's return the output of that build command. Okay, let's jump in to our CmdLineUI. And then we will want to create an ActorSystem called "helloakka".
Now let's declare a ActorRef pointing to an instance of the Poller Actor, let's call the actorOf method of the Actor system, then let's invoke the props method on Poller, passing in a string, and a null reference currently. Eventually we will want to pass in a reference to a PriceRequestor Actor, but we have not declared that class yet.
Then let's name that Actor. Let's refactor for readability, and there we have it we have created our first Actor. In the next video we will create our PriceRequestor Actor.
- Reactive programming concepts and techniques
- Functional programming concepts and techniques
- Overview of popular reactive frameworks
- Building an RxJava application
- Building an Akka application
- Unit testing with Akka and RxJava