Scala Reactive Programming

Author: Rambabu Posa  

Publisher: Packt Publishing‎

Publication year: 2018

E-ISBN: 9781787282872

P-ISBN(Paperback): 89543100405220

Subject: TP312 程序语言、算法语言

Language: ENG

Access to resources Favorite

Disclaimer: Any content in publications that violate the sovereignty, the constitution or regulations of the PRC is not accepted or approved by CNPIEC.

Scala Reactive Programming

Chapter

Preface

Chapter 1: Getting Started with Reactive and Functional Programming

Introduction to Reactive

What is Reactive?

What is Reactive programming?

What is a data stream or stream?

RP versus Reactive systems versus Reactive architecture

Event-Driven versus Message-Driven

Benefits of Reactive systems with RP

Functional programming

What is functional programming?

Principles of functional programming

Benefits of functional programming

Functional Reactive programming

Types of RP

Why FP is the best fit for RP

Reactive Manifesto

Need of Reactive Manifesto

Principles of Reactive systems

Message-Driven

Elasticity

Resilience

Responsiveness

Why Reactive Streams specification?

Why is Play Framework the best for Reactive systems?

Reactive systems versus traditional systems

The Java 9 Flow API

Flow API – Publisher

Flow API – Subscriber

Flow API – Subscription

Flow API – Processor

Flow API – Flow

Implementations of Reactive Streams

Lightbend's Reactive Platform

Pivotal's Reactor project

Microsoft's Reactive Extensions (RX)

Netflix's RxJava

Eclipse's Vert.x

Ratpack

How are Reactive Streams born?

Marble diagrams

What is a Marble diagram?

Data transformation

Benefits of Marble diagrams

Rules of Marble diagrams

Important FRP operators

FRP – the map() function Marble diagram

FRP – the flatMap() function Marble diagram

FRP – the merge() function Marble diagram

FRP – the filter() function Marble diagram

FRP – the reduce() function Marble diagram

FRP – the concat() and sorted() functions Marble diagram

Observer pattern versus Reactive pattern

Summary

Chapter 2: Functional Scala

Introduction to Scala

The Scala ecosystem

Understanding the Scala Application

Scala REPL

Principles of Scala FP

FP Design Patterns

Scala FP features in action

Immutability

Scala functions

Scala pure functions

Pattern matching

Scala combinators

For-comprehensions

Scala implicits

Implicit parameters

Implicit conversions

Scala anonymous functions

Everything is an expression

Referential transparency

Functions are first-class citizens

Partial functions

Function currying

Higher-Order Functions

Scala tail-recursion

Types of recursions

Benefits of linear recursion

A linear-recursion example

Benefits of tail-recursion

A tail-recursion example

Scala Type class

Benefits of Type classes

Scala Collections in action

Scala List

Scala List Cons operator

Right associative rule

Scala Map

Scala Range

Scala Functional Design Patterns

Scala map() function

Scala flatMap() function

Advantages of flatMap

Scala Monads in action

Scala Option

Scala Either

Scala Case class and object

Benefits of Scala Case class

Scala Traits in action

Trait as an interface

Traits linearization 

Linearization rules

Summary

Chapter 3: Asynchronous Programming with Scala

Introduction to Scala AP

What is asynchronous?

Differences between asynchronous and synchronous

Benefits of asynchronous programming

Differences between Concurrency and Parallelism

How Scala supports AP

The Scala Future API

Building blocks of the Scala Future API

Benefits of the Scala Future API

The Scala Future

What is a Future in Scala?

The Scala Future API

What is a computation unit?

Future Trait definition

Future Companion object

The complete Scala Future API

Scala Future examples

The Scala Promise

What is a Scala Promise?

The Scala Promise API

Scala Promise examples

Scala ExecutionContext

What is ExecutionContext?

The relationship between Scala Future components

Differences between a Future and a Promise

Scala Future API callbacks

Scala Future API combinators

Scala Future.sequence()

The Scala Async Library

Scala Async API

The Scala Future API in Play Framework

The Scala Future API in the Akka Toolkit

A Scala Future versus a Java Future

Summary

Chapter 4: Building Reactive Applications with Akka

Introduction to Akka

What is Akka?

Applications on the JVM (Java Virtual Machine)

Features of Akka

Benefits of Akka (or why do we need Akka?)

Building blocks of the Akka Toolkit

Akka Extensions (or modules)

Akka Clients

Actor Model in-depth

What is the Actor Model?

Principles of the Actor Model (or properties of Actor in the Actor Model)

Issues with the Shared-State Concurrency model

Benefits of the Actor Model

Components of the Actor Model (or building blocks of the Actor Model)

What are the benefits of immutable messages in the Actor Model?

How Akka implements the Actor Model

Other Actor Model implementations

Akka ActorSystem

What is the ActorSystem?

Roles and responsibilities of ActorSystem

How to create an Akka ActorSystem?

How to shut down an Akka ActorSystem?

Components of Akka's ActorSystem

What is materialization and Akka's implementation?

Akka Actors

What is an Actor?

Components of Akka Actor

Akka Actor – ActorRef

Akka Actor – Dispatcher

Benefits of the dispatcher pattern

Akka Actor – Mailbox(MessageQueue)

Akka Actor – Actor

Akka Actor – ActorPath

Actor versus thread

The lifecycle of an Akka Actor

Actor's preStart() lifecycle method

Actor's postStop() lifecycle method

Actor's preRestart() lifecycle method

Actor's postRestart() lifecycle method

Why Actors are lightweight?

Actor basic operations

Defining an Actor

Creating an Actor

Sending messages to an Actor

The tell (!) function

The ask (?) function

Replaying messages from an Actor

Actor to Actor communication

Actor to non-Actor communication

Stopping an Actor

Killing an Actor

Become/unbecoming an Actor

Case object Switch

Supervise an Actor

Akka Supervision

What is supervision?

What is the main goal of Akka's supervision?

Benefits of supervision

Why don't we write exceptions or failures handling in Actor itself?

Rules of Akka Supervision

Akka Supervision strategies

Akka's supervision hierarchy

Actor's Path

What is Akka's Let It Crash model?

Akka HelloWorld Actor example

Akka Actors communicate example

Akka Actor's lifecycle example

Akka parent-child Actors example

Actor's Path versus Reference

Differences between ActorRef and ActorPath

When we execute actorRef.tell (message), what happens internally?

MessageDispatcher

How to configure a dispatcher?

Akka Configuration

Akka SBT Templates

Akka Logging

Business problem

Problem discussion

Akka Actor's implementation

Summary

Chapter 5: Adding Reactiveness with RxScala

Introduction to RxScala

Reactive Extensions

Understanding Rx implementations

RxScala

Benefits of Reactive Extensions

Limitations of Reactive Extensions

Building blocks of RxScala

Understanding the Observable

Developing RxScala HelloWorld with an Observable

Observer

Extending RxScala HelloWorld with Observer

Subscriber

Extending RxScala HelloWorld with Subscriber

Subscription

Scheduler

RxScala Marble diagrams

RxScala's map() function

Summary

Chapter 6: Extending Applications with Play

Introduction to Play Framework

What is Play Framework?

Features of Play Framework

Benefits of Play Framework

Why Play Framework is so fast?

Clients of Play Framework

Building blocks of Play Framework

Play Routings

Client Request

Controller Action

Routes

Play Controllers

View Templates

Benefits of Twirl Templates

Model and Forms

Other components – services and repositories

Configuration

Play Framework View Template constructs

Twirl View Templates

Twirl Template Constructs

Architecture of Play Framework

The new features of Play Framework

Play Framework 2.5.x features

Play Framework 2.6.x features

Play Application project structure

Play/Scala HelloWorld example

What is Play Dependency Injection (DI)

Benefits of DI

Extend HelloWorld Example With DI

Extending HelloWorld example with Scala Futures

Play Form – Data Model

Play Framework Form API

Play Form – Binding Data

Play Form functions

Play Framework Form-based web application

Extending Play/Scala HelloWorld example with Akka Toolkit

Play Fileupload web application

Summary

Chapter 7: Working with Reactive Streams

Introduction to Akka Streams

What is a stream?

Goals of data streaming

What is Akka Streams?

Goals of the Akka Streams API

Features of the Akka Streams API

Benefits of the Akka Streams API

Why do we need Akka Streams and why not just Akka?

Other Reactive Streams implementations

Components of Akka Streams

The Akka Streams API

What is streaming data?

The Akka Streams Source component

The Akka Streams Sink component

The Akka Streams Flow component

RunnableGraph or Graph

Modules of Akka Streams

Akka Materialization

What is materialization?

Akka's Materializer

Akka's ActorMaterializer

Roles and responsibilities of Akka's ActorMaterializer

Akka's Actor versus Akka Stream's ActorMaterializer

Backpressure

The traditional backpressure approach

Push – Fast Producer / Slow Consumer

Pull – Slow Producer/Fast Consumer

The dynamic push/pull model

Akka Streams programming basics

Creating a Source

Creating a Sink

Connect a Source to a Sink

Creating a Flow

Connecting a Source to a Flow to a Sink

Why do we need ActorSystem and ActorMaterializer?

Akka Streams HelloWorld example

Extending the Akka Streams HelloWorld example with the Flow component

HelloWorld example description

Akka Streams GraphDSL

Goal of Akka Streams Graph DSL

Building blocks of Akka Streams Graph DSL

Fan-In functions

Fan-Out functions

Edge operator

An Akka Streams GraphDSL example

Akka Persistence

Akka Persistence features

The ES (Event Sourcing) model

What is an Event?

An Event Stream

Principles of an Event Sourcing (ES) model

Event sourcing

The CQRS pattern

Benefits of CQRS

How Akka Persistence implements CQRS/ES

How to develop Akka Persistence Actors

Step1 – Use the PersistenceActor trait

Step2 – Implement PersistenceActor's receiveRecover

Step3 – Implement PersistenceActor's receiveCommand

Step4 – Implement PersistenceActor's persistenceId

Step5 – Configure our journal details in application.conf

Akka Persistence MongoDB Weather example

Summary

Chapter 8: Integrating Akka Streams to Play Application

Akka Streams

Akka Streams revisited

Types of Akka Stream components

Akka Dynamic Streams

MergeHub

BroadcastHub

PartitionHub

Developing Akka Dynamic Streams

Integrating Akka Streams into Play

Designing a Reactive Chat System

Developing the Reactive Chat System

Test Reactive Chat Application

Akka Persistence Query

Building blocks of Akka Persistence App

Developing WF Reactive Akka Persistence App

Testing WF Reactive Akka Persistence App

Summary

Chapter 9: Reactive Microservices with Lagom

Introduction to Lagom Framework

What is Lagom Framework?

Features of Lagom Framework

Benefits of Lagom Framework

Drawbacks of monolith architecture

Benefits of microservice architecture

Principles of microservices

Lagom Reactive Platform architecture

Modules of Lagom Framework

Lagom Core modules

Lagom Scala Core modules

Lagom Persistence modules

Lagom Clustering modules

Lagom Kafka modules

Lagom logging module

Building blocks of Lagom Framework

Lagom Service Locator

Lagom Service Gateway

Lagom Service Descriptor

Lagom Framework's identifiers

Lagom ServiceCall in detail

Description

Internal components of Lagom Framework

Kafka with ZooKeeper

Cassandra

Lagom project structure

Lagom System API

Lagom System – implementation

Lagom System – frontend

Lagom Hello Reactive System

Getting Hello Service code

Testing the Hello Reactive System

Lagom Hello Reactive System microservices

Understanding the Hello Service API code

Understanding HelloService implementation code

Developing Lagom WF Reactive System

WF Reactive System architecture

WF Reactive System – Producer API

WF Reactive System – Producer implementation

WF Reactive System – Consumer API

WF Reactive System – Consumer implementation

WF Reactive System – frontend

Testing the WF Reactive System

Summary

Chapter 10: Testing Reactive Microservices

Introduction to TDD

What is TDD?

Code Coverage

Benefits of TDD

Unit testing frameworks

ScalaTest

ScalaTest Plus

Benefits of ScalaTest

Unit testing Scala applications

Unit testing Play Framework components

Test HelloWorld without using DI Controller

Test HelloWorld with DI Controller

Unit testing Akka Actors

Akka Toolkit's testing modules

Testing HelloWorld Actor

Unit testing HelloWorldActor (Version 2)

Executing HelloWorld Actor unit tests

Unit testing Akka Streams

Uniting test Akka Streams, with Actor's Testkit

Unit testing Akka Streams with Akka Stream's Testkit

Unit testing Lagom services

The Code Coverage tool

The SCoverage tool

SBT SCoverage plugin

Summary

Chapter 11: Managing Microservices in ConductR

Introduction to Lightbend's ConductR

What is ConductR?

Responsibilities of ConductR

Advantages of ConductR

Components of ConductR

ConductR APIs

The Bundle API

The Control API

Installing Lightbend ConductR

Prerequisites

Installing the ConductR CLI

ConductR Sandbox Visualizer

Understanding ConductR Sandbox information

Preparing WF Reactive System for ConductR

ConductRSBT plugin

Mix ConductRApplicationComponents trait

Deploying the WF Reactive System on ConductR

Starting ConductR Sandbox with multinodes

Starting ConductR Sandbox with a single node

Bundle WF Reactive System components

Observing the WF Reactive System bundles

ConductR Bundle descriptor

Loading WF Reactive System bundles

Load wf-producer-impl

Loading wf-consumer-impl

Loading wf-frontend

Running and testing WF Reactive System bundles

Useful ConductR commands

Summary

Chapter 12: Reactive Design Patterns and Best Practices

Understanding Design Patterns

Understanding Let-it-Crash

The Circuit Breaker pattern

Understanding the Circuit Breaker pattern

With or without Circuit Breaker

How a Circuit Breaker works

The Sharding Pattern

The Event Sourcing pattern

The Event Streaming Pattern

The Active-Passive Replication Pattern

The Resource Management Design Patterns

The Reactive Resource Loan pattern

The Reactive Resource Pool pattern

Message Flow Patterns

The Request-Response Design Pattern

Understanding the Reactive Design Pattern

Ask Reactive Design Pattern

The Throttling Pattern

The Pull pattern

Reactive System's best practices and tools

Tools

Useful best practices

Prefer tell over ask with Akka Actors

Don't sequentialize Futures

Summary

Appendix A: Scala Plugin for IntelliJ IDEA

Understanding Scala Plugin

How to set up Scala Plugin for IntelliJ IDE

Summary

Appendix B: Installing Robomongo

What is Robomongo?

Setting up Robomongo

How to use Robomongo?

Summary

Other Books You May Enjoy

Index

The users who browse this book also browse