O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Building Applications with Scala

Book Description

Write modern, scalable, and reactive applications with the power of Scala

About This Book

  • Delves into the intricacies of functional reactive programming with Scala
  • Explores frameworks like Akka, Play and Slick used to develop efficient applications
  • A step by step guide with plenty of examples showing practical implementation of essential concepts

Who This Book Is For

If you are a Java or JVM developer who wants to use Scala to build reactive functional applications for the JVM platform, then this book is for you. Prior knowledge of Java or functional programing would help. No Scala knowledge is required.

What You Will Learn

  • Use Akka to create a chat service for your app
  • Equip yourself with the techniques and tools to build reports and build database persistence with Scala and Slick
  • Develop a customer-facing Rest API that makes use of Scala and Spray
  • Make use of the Scala web development principles and scale up the architecture of your application
  • Get familiar with the core principles and concepts of Functional Programming
  • Use the Play framework to create models, controllers, and views
  • Develop reactive backing frameworks by writing code with RxScala
  • Discover what proper testing entails with Scala using behavior-driven development

In Detail

Scala is known for incorporating both object-oriented and functional programming into a concise and extremely powerful package. However, creating an app in Scala can get a little tricky because of the complexity the language has. This book will help you dive straight into app development by creating a real, reactive, and functional application. We will provide you with practical examples and instructions using a hands-on approach that will give you a firm grounding in reactive functional principles.

The book will take you through all the fundamentals of app development within Scala as you build an application piece by piece. We’ve made sure to incorporate everything you need from setting up to building reports and scaling architecture. This book also covers the most useful tools available in the Scala ecosystem, such as Slick, Play, and Akka, and a whole lot more. It will help you unlock the secrets of building your own up-to-date Scala application while maximizing performance and scalability.

Style and approach

This book takes a step-by-step approach to app development with Scala. It will place special emphasis on functional language. It will teach you the core benefits of Scala and the fundamentals of functional programming by developing a robust application.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Building Applications with Scala
    1. Building Applications with Scala
    2. Credits
    3. About the Author
    4. Acknowledgments
    5. About the Reviewer
    6. www.PacktPub.com
      1. Why subscribe?
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    8. 1. Introduction to FP, Reactive, and Scala
      1. Functional programming
      2. Principles of functional programming
        1. Immutability
        2. Disciplined state
        3. Pure functions and no side effects
        4. First-class functions and higher-order functions
        5. Type systems
        6. Referential transparency
      3. Installing Java 8 and Scala 2.11
      4. Read Eval Print and Loop - REPL
      5. Scala Hello World using the REPL
        1. Scala REPL Hello World program
        2. Scala object-oriented HelloWorld program
        3. Scala HelloWorld App in the Scala REPL
        4. Java HelloWorld application
      6. Scala language - the basics
      7. Scala variables - var and val
        1. Scala REPL var usage
        2. Scala val usage at the Scala REPL
      8. Creating immutable variables
        1. Scala variable type in the Scala REPL
        2. Scala variables with explicit typing at the Scala REPL
      9. Scala conditional and loops statements
        1. If statements in Scala REPL
        2. If statements in return statements in Scala REPL
        3. Basic for loop in Scala REPL
        4. For with List in Scala REPL
        5. For with if statements for filtering - Scala REPL
        6. Java code for filtering even numbers
      10. For comprehensions
        1. For comprehension in Scala REPL
        2. Java code for performing filtering with collections
      11. Scala collections
        1. Creating, removing, and getting an item from a mutable list in Scala REPL
        2. Scala tuples
        3. Scala immutable Map in Scala REPL
        4. Scala mutable Maps at Scala REPL
      12. Monads
        1. Scala Map function in Scala REPL
        2. Option Monad in Scala
        3. A list of all methods using the Scala REPL
      13. Scala class, traits, and OO programming
        1. A simple Scala class in Scala REPL
        2. Scala plain old Java object in Scala REPL
        3. Person class in Java
      14. Traits and inheritance
        1. Scala inheritance code in Scala REPL
        2. Scala traits sample code in Scala REPL
        3. Scala traits using variable mixing technique at Scala REPL
        4. Scala type alias sample in Scala REPL
      15. Case classes
        1. Scala case classes feature in Scala REPL
      16. Pattern Matcher
        1. Simple Pattern Matcher in Scala
        2. Advanced pattern matcher in Scala REPL
        3. Advanced complex pattern matcher in Scala REPL
      17. Partial functions
        1. Simple Partial function in Scala REPL
        2. Scala PartialFunction without OO using case statements in Scala REPL
        3. PartialFunction composition in Scala REPL
      18. Package objects
        1. package.scala
        2. MainApp.scala
      19. Functions
      20. Partial application
        1. Partial function in Scala REPL
      21. Curried functions
        1. Curried functions - Scala REPL
        2. Curried transformation in Scala REPL
      22. Operator overloading
        1. Scala operator overloading in Scala REPL
      23. Implicits
        1. Scala Implicits in SCALA REPL
        2. Implicit Parameter at Scala REPL
      24. Futures
        1. Simple Future code in Scala REPL
        2. A complete Future sample at Scala REPL
      25. Reactive Programing and RxScala
        1. Simple Observables Scala with RxScala
        2. Simple Observables Scala with RxScala - Execution in the console
        3. Complex Scala with RxScala Observables
      26. Summary
    9. 2. Creating Your App Architecture and Bootstrapping with SBT
      1. Introducing SBT
      2. Installing SBT on Ubuntu Linux
      3. Getting started with SBT
      4. Adding dependencies
      5. Generating Eclipse project files from SBT
      6. Application distribution
      7. Hello world SBT / Scala App
      8. Bootstrapping our Play framework app with Activator
      9. Activator shell
      10. Activator - compiling, testing, and running
      11. Summary
    10. 3. Developing the UI with Play Framework
      1. Getting started
      2. Creating our models
      3. Creating routes
      4. Creating our controllers
      5. Working with services
      6. Configuring the Guice module
      7. Working with views(UI)
      8. Summary
    11. 4. Developing Reactive Backing Services
      1. Getting started with reactive programming
        1. IPriceService - Scala trait
        2. PriceService - RxScala PriceService implementation
        3. Guice Injection - Module.scala
        4. NGServiceEndpoint
      2. Play framework and high CPU usage
        1. RndDoubleGeneratorController
        2. IRndService.scala - Scala trait
        3. RndService.scala - RndService implementation
        4. Module.scala - Guice Injections
        5. main.scala.html
        6. product_details.scala.html
      3. Summary
    12. 5. Testing Your Application
      1. Unit testing principles
        1. Making code testable
        2. Isolation and self-contained tests
        3. Effective naming
        4. Levels of testing
      2. Testing with Junit
      3. Behavior-Driven Development - BDD
      4. MyFirstPlaySpec.scala - First BDD with ScalaTest and the Play framework
      5. Testing with Play framework support
        1. ProductService.scala - FIX the code issue
        2. ImageServiceTestSpec.scala - ImageService Test
        3. ReviewServiceTestSpec.scala - ReviewService test
      6. Testing routes
        1. RoutesTestingSpec.scala - Play framework route testing
      7. Controller testing
        1. RndDoubleGeneratorControllerTestSpec.scala - RndDoubleGeneratorController tests
        2. IntegrationSpec.scala
        3. ProductControllerTestSpec.scala
        4. product_index.scala.html
        5. ImageControllerTestSpec.scala
        6. image_index.scala.html
        7. ReviewControllerTestSpec.scala
        8. review_index.scala.html
        9. ApplicationSpec.scala
        10. NGServiceImplTestSpec.scala
        11. NGServiceEndpointControllerTest.scala
      8. Summary
    13. 6. Persistence with Slick
      1. Introducing the Slick framework
      2. MySQL setup
      3. Configuring Slick in our Play framework app
        1. Configure the database connection
      4. FPM Mapping
        1. ProductDao
        2. ReviewDAO
        3. ImageDao
      5. Slick evolutions
      6. Refactoring services
      7. Refactoring controllers
      8. Configuring DAO packages in Guice
      9. Refactoring tests
      10. Generic mocks
      11. Service tests
      12. Controller tests
      13. Running the application
      14. Summary
    14. 7. Creating Reports
      1. Introducing JasperReports
      2. JasperReports workflow
      3. Jasper sessions
      4. Installing Jaspersoft Studio 6
      5. Configuring MySQL Data Adapter in Jaspersoft Studio
      6. Creating a product report
      7. Creating a review report
      8. Creating an image report
      9. Integrating JasperReports with Play framework
        1. build.sbt
        2. Generic report builder
        3. Adding the report to the product controller
        4. Adding the report to the review controller
        5. Adding the report to the image controller
      10. Routes - adding new report routes
      11. New centralized reports UI
        1. Adding the report button for each view
      12. Summary
    15. 8. Developing a Chat with Akka
      1. Adding the new UI introduction to Akka
      2. Introduction to the Actor model
        1. What is an Actor?
      3. Message exchange and mailboxes
      4. Coding actors with Akka
      5. Actor routing
      6. Persistence
      7. Creating our chat application
      8. The chat protocol
      9. The chat controller
        1. Implementing the chat controller
      10. Configuring the routes
      11. Working on the UI
      12. Adding Akka tests
        1. Scala test for Akka Actor
        2. Chat room Actor test
        3. Chat Bot Admin Actor test
      13. Summary
    16. 9. Design Your REST API
      1. Introduction to REST
      2. REST API design
        1. HTTP verbs design
        2. Uniform API
        3. Response with HTTP status codes
        4. REST API patterns
        5. API versioning
        6. Some anti-patterns to be avoided
      3. Creating our API with REST and JSON
        1. RestApiContoller
          1. REST API Front Controller implementation
        2. JSON mapping
        3. Configuring new routes
        4. Testing the API using the browser
      4. Creating a Scala client
        1. Configuring plugins.sbt
        2. Configuring build.sbt
        3. Scala client code
      5. Creating our REST client proxies
        1. Creating ScalaTest tests for the proxies
        2. Adding back pressure
          1. The leaky bucket algorithm
            1. Scala leaky bucket implementation
            2. Testing back pressure
        3. Adding Swagger support
        4. Swagger UI
          1. Build and install Swagger Standalone
      6. Summary
    17. 10. Scaling up
      1. Standalone deploy
      2. Reports folder
      3. Changing report builder
      4. Defining the secret
      5. Running the standalone deploy
      6. Architecture principles
        1. Service orientation (SOA/microservices)
        2. Performance
        3. Scalability/Resiliency
      7. Scalability principles
        1. Vertical and horizontal scaling (up and out)
        2. Caching
        3. Load balancer
        4. Throttling
        5. Database cluster
        6. Cloud computing/containers
        7. Auto Scaling
        8. A note about automation
        9. Don't forget about telemetry
        10. Reactive Drivers and discoverability
      8. Mid-Tier load balancer, timeouts, Back pressure, and caching
      9. Scaling up microservices with an Akka cluster
      10. Scaling up the infrastructure with Docker and AWS cloud
      11. Summary