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

Scala for Java Developers

Book Description

Build reactive scalable applications and integrate Java code with the power of Scala

In Detail

Scala for Java Developers is a step-by-step guide full of easy-to-follow code taken from real-world examples explaining the migration and integration of Scala in a Java project.

With this book, you will first get comfortable with the Scala syntax and its Java-like ecosystem, and then dive into new ways of building reactive web apps using the Typesafe stack including the actor-based Akka framework, the Play web framework, and the emerging Slick framework for persistence. The book will then teach you how to review useful tools for unit, integration, and functional testing; demonstrate how integrating with external systems applies to the Scala world and what its benefits are.

From learning the Scala syntax interactively to writing modern, scalable, reactive applications, this book will help you to take your skills to the next level by solving complex problems in a concise and maintainable way.

What You Will Learn

  • Apply and control the Scala Ecosystem
  • Migrate Java code to Scala
  • Discover Play Framework web development
  • Test data using Scala's testing frameworks
  • Manipulate XML and JSON in Scala
  • Learn the Scala syntax interactively
  • Integrate Java projects in Scala
  • Build reactive web apps using the Typesafe stack
  • Use new systems including the Akka framework, the Play web framework, and the emerging Slick framework
  • Tackle big data challenges

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 files e-mailed directly to you.

Table of Contents

  1. Scala for Java Developers
    1. Table of Contents
    2. Scala for Java Developers
    3. Credits
    4. Foreword
    5. About the Author
    6. Acknowledgments
    7. About the Reviewers
    8. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why Subscribe?
        2. Free Access for Packt account holders
    9. 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. Errata
        3. Piracy
        4. Questions
    10. 1. Programming Interactively within Your Project
      1. Advantages of using Scala for Java projects
        1. More concise and expressive
        2. Increased productivity
        3. Natural evolution from Java
        4. Better fit for asynchronous and concurrent code
      2. Learning Scala through the REPL
        1. Declaring val/var variables
        2. Defining classes
        3. Explaining case classes
      3. Operations on collections
        1. Transforming collections containing primitive types
        2. Collections of more complex objects
          1. Filter and partition
          2. Dealing with tuples
          3. Introducing Map
          4. Introducing the Option construct
          5. A glimpse at pattern matching
          6. The map method
          7. Looking at String Interpolation
          8. The groupBy method
          9. The foldLeft method
      4. Summary
    11. 2. Code Integration
      1. Creating a REST API from an existing database
        1. The sample database
        2. Setting up a Maven project
        3. Creating JPA entities and REST web services
        4. Running and testing the project
          1. Adding a unit test in Java
      2. Adding a test in Scala
      3. Setting up Scala within a Java Maven project
      4. Scala and Java collaboration
        1. Converting between collection types
        2. JavaBean-style properties
        3. Scala and Java object orientation
        4. Scala traits as enhanced Java interfaces
        5. Declaring objects
        6. Introducing companion objects
        7. Handling exceptions
      5. Differences in style between Java and Scala code
        1. Adjusting the code layout
        2. Naming conventions
      6. Summary
    12. 3. Understanding the Scala Ecosystem
      1. Inheriting Java Integrated Development Environments (IDEs)
      2. Building with Simple Build Tool (SBT)
        1. Getting started with SBT
          1. Creating a sample project
          2. Importing the project in Eclipse, IntelliJ IDEA, and NetBeans
          3. Creating a web application that runs on a servlet container
        2. Using sbt-assembly to build a single .jar archive
        3. Formatting code with Scalariform
      3. Experimenting with Scala Worksheets
      4. Working with HTTP
        1. Scala's for comprehension
      5. Taking advantage of Typesafe Activator
        1. Creating an application based on activator templates
      6. The REPL as a scripting engine
      7. Summary
    13. 4. Testing Tools
      1. Writing tests with ScalaTest
        1. BDD-style testing
        2. Functional testing
        3. Mocking with ScalaMock
      2. Testing with ScalaCheck
      3. Summary
    14. 5. Getting Started with the Play Framework
      1. Getting started with the classic Play distribution
      2. Getting started with the Typesafe Activator
      3. Architecture of a Play application
        1. Visualizing the framework stack
        2. Exploring the request-response lifecycle
        3. Handling a request in the controller
        4. Rendering the view
      4. Playing with authentication
      5. Practical tips when using Play
        1. Debugging with Play
        2. Dealing with version control
      6. Summary
    15. 6. Database Access and the Future of ORM
      1. Integrating an existing ORM – Hibernate and JPA
        1. Making JPA available in Scala
      2. Dealing with persistence in the Play Framework
        1. A simple example using Anorm
      3. Replacing ORM
      4. Learning about Slick
      5. Scaffolding a Play application
        1. Importing test data
        2. Visualizing the database in the H2browser
        3. Exploring the code behind the app generation
        4. Limitations of the playcrud utility
      6. Summary
    16. 7. Working with Integration and Web Services
      1. Binding XML data in Scala
        1. Running scalaxb from a SOAP web service
      2. Working with XML and JSON
        1. Manipulating XML
        2. Manipulating JSON
        3. Using Play JSON
      3. Handling Play requests with XML and JSON
        1. Mocking Play responses with JSON
        2. Calling web services from Play
      4. Summary
    17. 8. Essential Properties of Modern Applications – Asynchrony and Concurrency
      1. The pillars of Concurrency
      2. The Async library – SIP-22-Async
        1. Combining web services
        2. Combining services without await
      3. Getting started with Akka
        1. Understanding the Actor model
        2. Switching behavior
        3. Supervising actors to handle failure
        4. Testing actor systems
        5. Exploring further with Akka
      4. Summary
    18. 9. Building Reactive Web Applications
      1. Describing reactive applications
      2. Handling streams reactively
        1. Understanding Iteratees in Play
        2. Adapting Enumerator with Enumeratee
      3. Experimenting with WebSockets and Iteratees in Play
      4. Learning from activator templates
        1. Reactive stocks
        2. Reactive real-time search
        3. The Play-Akka-Angular-WebSocket template
      5. Playing with Actor Room
      6. Summary
    19. 10. Scala Goodies
      1. Exploring MongoDB
        1. Entering Casbah
        2. Applying MapReduce transformations
      2. Scratching the surface of Big Data
      3. Introducing DSLs in Scala
        1. Observing internal DSLs
        2. Tackling external DSLs through parser combinators
      4. Introducing Scala.js
      5. Final tips
        1. Copying and pasting in the REPL
        2. Timing code execution in the REPL
      6. Summary
    20. Index