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

Java 9: Building Robust Modular Applications

Book Description

Mastering advanced features of Java and implement them to build amazing projects

About This Book
  • Take advantage of Java's new modularity features to write real-world applications that solve a variety of problems
  • Explore the major concepts introduced with Java 9, including modular programming, HTTP 2.0, API changes, and more
  • Get to grips with tools, techniques and best practices to enhance application development
Who This Book Is For

This learning path is for Java developers who are looking to move a level up and learn how to build robust applications in the latest version of Java.

What You Will Learn
  • Package Java applications as modules using the Java Platform Module System
  • Implement process management in Java using the all-new process handling API
  • Integrate your applications with third-party services in the cloud
  • Interact with mail servers, using JavaMail to build an application that filters spam messages
  • Use JavaFX to build rich GUI-based applications, which are an essential element of application development
  • Leverage the possibilities provided by the newly introduced Java shell
  • Test your application's effectiveness with the JVM harness
  • See how Java 9 provides support for the HTTP 2.0 standard
In Detail

Java 9 and its new features add to the richness of the language; Java is one of the languages most used by developers to build robust software applications. Java 9 comes with a special emphasis on modularity with its integration with Jigsaw. This course is your one-stop guide to mastering the language.

You'll be provided with an overview and explanation of the new features introduced in Java 9 and the importance of the new APIs and enhancements. Some new features of Java 9 are ground-breaking; if you are an experienced programmer, you will be able to make your enterprise applications leaner by learning these new features. You'll be provided with practical guidance in applying your newly acquired knowledge of Java 9 and further information on future developments of the Java platform. This course will improve your productivity, making your applications faster. Next, you'll go on to implement everything you've learned by building 10 cool projects. You will learn to build an email filter that separates spam messages from all your inboxes, a social media aggregator app that will help you efficiently track various feeds, and a microservice for a client/server note application, to name just a few.

By the end of this course, you will be well acquainted with Java 9 features and able to build your own applications and projects.

This Learning Path contains the best content from the following two recently published Packt products:

  • Mastering Java 9
  • Java 9 Programming Blueprints
Style and approach

This practical guide is filled with real-world examples. Its projects will help you get acquainted with concepts in depth.

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. Title Page - Courses
  2. Copyright and Credits - Courses
    1. Java 9: Building Robust Modular Applications
  3. Packt Upsell - Courses
    1. Why subscribe?
    2. PacktPub.com
  4. Preface
    1. Who this learning path is for
    2. What this learning path covers
    3. To get the most out of this learning path
      1. Download the example code files
      2. Conventions used
    4. Get in touch
      1. Reviews
  5. Mastering Java 9
  6. The Java 9 Landscape
    1. Java 9 at 20,000 feet
    2. Breaking the monolith
    3. Playing around with the Java Shell
    4. Taking control of external processes
    5. Boosting performance with G1
    6. Measuring performance with JMH
    7. Getting started with HTTP 2.0
    8. Encompassing reactive programming
    9. Expanding the wish list
    10. Summary
  7. Discovering Java 9
    1. Improved Contended Locking [JEP 143]
      1. Improvement goals
    2. Segmented code cache [JEP 197]
      1. Memory allocation
    3. Smart Java compilation, phase two [JEP 199]
    4. Resolving Lint and Doclint warnings [JEP 212]
    5. Tiered attribution for javac [JEP 215]
    6. Annotations pipeline 2.0 [JEP 217]
    7. New version-string scheme [JEP 223]
    8. Generating run-time compiler tests automatically [JEP 233]
    9. Testing class-file attributes generated by Javac [JEP 235]
    10. Storing interned strings in CDS archives [JEP 250]
      1. The problem
      2. The solution
    11. Preparing JavaFX UI controls and CSS APIs for modularization [JEP 253]
      1. JavaFX overview
      2. Implications for Java 9
    12. Compact strings [JEP 254]
      1. Pre-Java 9 status
      2. New with Java 9
    13. Merging selected Xerces 2.11.0 updates into JAXP [JEP 255]
    14. Updating JavaFX/Media to newer version of GStreamer [JEP 257]
    15. HarfBuzz Font-Layout Engine [JEP 258]
    16. HiDPI graphics on Windows and Linux [JEP 263]
    17. Marlin graphics renderer [JEP 265]
    18. Unicode 8.0.0 [JEP 267]
      1. New in Unicode 8.0.0
      2. Updated Classes in Java 9
    19. Reserved stack areas for critical sections [JEP 270]
      1. The pre-Java 9 situation
      2. New in Java 9
    20. Dynamic linking of language-defined object models [JEP 276]
      1. Proof of concept
    21. Additional tests for humongous objects in G1 [JEP 278]
    22. Improving test-failure troubleshooting [JEP 279]
      1. Environmental information
      2. Java process information
    23. Optimizing string concatenation [JEP 280]
    24. HotSpot C++ unit-test framework [JEP 281]
    25. Enabling GTK 3 on Linux [JEP 283]
    26. New HotSpot build system [JEP 284]
    27. Summary
  8. Java 9 Language Enhancements
    1. Working with variable handlers [JEP 193]
      1. Working with the AtoMiC Toolkit
      2. Using the sun.misc.Unsafe class
    2. Eliding depreciation warnings on import statements [JEP 211]
    3. Milling Project Coin [JEP 213]
      1. Using the @SafeVarargs annotation
      2. The try-with-resource statement
      3. Using the diamond operator
      4. Discontinuing use of the underscore
      5. Making use of private interface methods
    4. Processing import statements correctly [JEP 216]
    5. Summary
  9. Building Modular Applications with Java 9
    1. A modular primer
    2. Reviewing Java's platform module system [JEP-200]
    3. Modularizing JDK source code [JEP-201]
      1. Pre-Java 9 JDK source code organization
        1. Development tools
          1. Deployment
          2. Internationalization
          3. Monitoring
          4. RMI
          5. Security
          6. Troubleshooting
          7. Web services
        2. JavaFX tools
        3. Java runtime environment
        4. Source code
        5. Libraries
        6. C header files
        7. Database
      2. JDK source code reorganized
    4. Understanding modular run-time images [JEP-220]
      1. Runtime format adoption
      2. Runtime image restructure
      3. Supporting common operations
      4. De-privileging JDK classes
      5. Preserving existing behaviors
    5. Getting to know the module system [JEP-261]
      1. Module paths
      2. Access-control boundary violations
        1. Runtime
    6. Modular Java application packaging [JEP-275]
      1. Advanced look at the Java Linker
      2. Java Packager options
    7. JLink - The Java Linker [JEP-282]
    8. Encapsulating most internal APIs [JEP-260]
    9. Summary
  10. Migrating Applications to Java 9
    1. Quick review of Project Jigsaw
      1. Classpath
      2. The monolithic nature of the JDK
    2. How modules fit into the Java landscape
      1. Base module
      2. Reliable configuration
      3. Strong encapsulation
    3. Migration planning
      1. Testing a simple Java application
      2. Potential migration issues
        1. The JRE
        2. Access to internal APIs
        3. Accessing internal JARs
        4. JAR URL depreciation
        5. Extension mechanism
        6. The JDK's modularization
    4. Advice from Oracle
      1. Preparatory steps
        1. Getting the JDK 9 early access build
        2. Running your program before recompiling
        3. Updating third-party libraries and tools
        4. Compiling your application
          1. Pre-Java 9 -source and -target options
          2. Java 9 -source and -target options
        5. Running jdeps on your code
      2. Breaking encapsulation
        1. The --add-opens option
        2. The --add-exports option
        3. The --permit-illegal-access option
      3. Runtime image changes
        1. Java version schema
        2. JDK and JRE layout
      4. What has been removed
      5. Updated garbage collection
      6. Deployment
        1. JRE version selection
        2. Serialized applets
        3. JNLP update
          1. Nested resources
          2. FX XML extension
          3. JNLP file syntax
          4. Numeric version comparison
    5. Useful tools
      1. Java environment - jEnv
      2. Maven
        1. Obtaining the M2Eclipse IDE
    6. Summary
  11. Experimenting with the Java Shell
    1. What is JShell?
    2. Getting Started with JShell
    3. Practical uses of JShell
      1. Feedback modes
        1. Creating a custom feedback mode
      2. Listing your assets
      3. Editing in the JShell
        1. Modifying text
        2. Basic navigation
        3. Historical navigation
        4. Advanced editing commands
    4. Working with scripts
      1. Start up scripts
      2. Loading scripts
      3. Saving scripts
      4. Advanced scripting with JShell
    5. Summary
  12. Leveraging the New Default G1 Garbage Collector
    1. Overview of garbage collection
      1. Object life cycle
        1. Object creation
        2. Object mid-life
        3. Object destruction
      2. Garbage collection algorithms
        1. Mark and sweep
        2. Concurrent mark sweep (CMS) garbage collection
        3. Serial garbage collection
        4. Parallel garbage collection
        5. G1 garbage collection
      3. Garbage collection options
      4. Java methods relevant to garbage collection
        1. The System.gc() method
        2. The finalize() method
    2. Pre-Java 9 garbage collection
      1. Visualizing garbage collection
      2. Garbage collection upgrades in Java 8
      3. Case study - Games written with Java
    3. Collecting garbage with the new Java platform
      1. Default garbage collection
      2. Depreciated garbage collection combinations
      3. Unified garbage collection logging
        1. Unified JVM logging (JEP-158)
          1. Tags
          2. Levels
          3. Decorations
          4. Output
          5. Command-line options
        2. Unified GC logging (JEP-271)
          1. Garbage collection logging options
          2. The gc tag
          3. Macros
          4. Additional considerations
    4. Persistent issues
      1. Making objects eligible for garbage collection
    5. Summary
  13. Microbenchmarking Applications with JMH
    1. Microbenchmarking overview
      1. Approach to using JMH
        1. Installing Java 9 and Eclipse with Java 9 support
        2. Hands-on experiment
    2. Microbenchmarking with Maven
    3. Benchmarking options
      1. Modes
      2. Time units
    4. Techniques for avoiding microbenchmarking pitfalls
      1. Power management
      2. OS schedulers
      3. Time sharing
      4. Eliminating dead-code and constant folding
      5. Run-to-run variance
      6. Cache capacity
    5. Summary
  14. Making Use of the ProcessHandle API
    1. What are processes?
    2. The new ProcessHandle interface
    3. Getting the PID of the current process
    4. Getting information about a process
    5. Listing processes
      1. Listing children
      2. Listing descendants
      3. Listing all processes
    6. Waiting for processes
    7. Terminating processes
    8. A small process controller application
      1. Main class
      2. Parameters class
      3. The ParamsAndHandle class
      4. The ControlDaemon class
    9. Summary
  15. Fine-Grained Stack Tracing
    1. Overview of the Java Stack
    2. The importance of stack information
      1. Example - Restricting callers
      2. Example - Getting logger for caller
    3. Working with StackWalker
      1. Getting an instance of StackWalker
        1. RETAIN_CLASS_REFERENCE
        2. SHOW_REFLECT_FRAMES
        3. SHOW_HIDDEN_FRAMES
        4. Final thoughts on enum constants
      2. Accessing classes
      3. Walking methods
    4. StackFrame
    5. Performance
    6. Summary
  16. New Tools and Tool Enhancements
    1. The new HTTP client [JEP-110]
      1. The HTTP client before Java 9
      2. Java 9's new HTTP client
      3. New API limitations
    2. Simplified Doclet API [JEP-221]
      1. The pre-Java 9 Doclet API
        1. API enums
        2. API classes
        3. API interfaces
        4. Problems with the pre-existing Doclet API
      2. Java 9's Doclet API
        1. Compiler tree API
        2. Language model API
          1. The AnnotatedConstruct interface
          2. The SourceVersion enum
          3. The UnknownEntityException exception
    3. HTML5 Javadoc [JEP-224]
    4. Javadoc search [JEP-225]
      1. Introducing camel case search
    5. Remove launch-time JRE version selection [JEP-231]
    6. Parser API for Nashorn [JEP-236]
      1. Nashorn
        1. Using Nashorn as a command-line tool
        2. Using Nashorn as an embedded interpreter
      2. EMCAScript
      3. Parser API
    7. Multi-release JAR files [JEP-238]
      1. Identifying multi-release JAR files
      2. Related JDK changes
    8. Java-level JVM compiler interface [JEP-243]
    9. BeanInfo annotations [JEP-256]
      1. JavaBean
      2. BeanProperty
      3. SwingContainer
      4. BeanInfo classes
    10. TIFF image input/output [JEP-262]
    11. Platform logging API and service [JEP-264]
      1. The java.util.logging package
      2. Logging in Java 9
    12. XML Catalogs [JEP-268]
      1. The OASIS XML Catalog standard
      2. JAXP processors
      3. XML Catalogs prior to Java 9
      4. Java 9 platform changes
    13. Convenience factory methods for collections [JEP-269]
      1. Using collections before Java 9
      2. Using new collection literals
    14. Platform-specific desktop features [JEP-272]
    15. Enhanced method handles [JEP-274]
      1. Reason for the enhancement
      2. Lookup functions
      3. Argument handling
      4. Additional combinations
    16. Enhanced deprecation [JEP-277]
      1. What the @Deprecated annotation really means
    17. Summary
  17. Concurrency and Reactive Programming
    1. Reactive Programming
      1. Reactive programming standardization
    2. The New Flow API
      1. The Flow.Publisher interface
      2. The Flow.Subscriber interface
      3. The Flow.Subscription interface
      4. The Flow.Processor interface
      5. Sample implementation
    3. Additional Concurrency Updates
      1. Java concurrency
        1. Concurrency explained
        2. System configurations
        3. Java threads
        4. Concurrency improvements
      2. CompletableFuture API enhancements
        1. Class details
        2. Enhancements
    4. Spin-Wait Hints
    5. Summary
  18. Security Enhancements
    1. Datagram Transport Layer Security
      1. DTLS protocol version 1.0
      2. DTLS protocol version 1.2
      3. DTLS support in Java 9
    2. Creating PKCS12 keystores
      1. Keystore primer
      2. Java Keystore (JKS)
        1. Builder
        2. The CallbackHandlerProtection class
        3. The PasswordProtection class
        4. The PrivateKeyEntry class
        5. The SecretKeyEntry class
        6. The TrustedCertificateEntry class
      3. PKCS12 default in Java 9
    3. Improving security application performance
      1. Security policy enforcement
      2. Permission evaluation
      3. The java.Security.CodeSource package
      4. Package checking algorithm
    4. TLS application-layer protocol negotiation extension
      1. TLS ALPN extension
      2. The javax.net.ssl package
      3. The java.net.ssl package extension
    5. Leveraging CPU instructions for GHASH and RSA
      1. Hashing
    6. OCSP stapling for TLS
      1. OCSP stapling primer
      2. Changes for the Java 9 platform
    7. DRBG-based SecureRandom implementations
    8. Summary
  19. Command Line Flags
    1. Unified JVM Logging [JEP 158]
      1. Command-line options
      2. Decorations
      3. Levels
      4. Output
      5. Tags
    2. Compiler control [JEP 165]
      1. Compilation modes
        1. C1 compilation mode
        2. C2 compilation mode
        3. Tiered compilation
      2. Compiler control in Java 9
    3. Diagnostic commands [JEP 228]
    4. Heap profiling agent [JEP 240]
    5. Removing your JHAT [JEP 241]
    6. JVM command-line flag argument validation [JEP 245]
    7. Compile for older platform versions [JEP 247]
    8. Summary
  20. Best Practices In Java 9
    1. Support for UTF-8
      1. The ResourceBundle class
        1. The nested class
        2. Fields and constructors
        3. Methods
      2. Changes in Java 9
    2. Unicode 7.0.0
      1. The java.lang package
      2. The java.text package
      3. Additional significance
    3. The Linux/AArch64 port
    4. Multi-resolution Images
    5. Common Locale Data Repository (CLDR)
    6. Summary
  21. Future Directions
    1. Future Changes to the JDK
      1. JDK changes targeted for Java 10
        1. Repository consolidation
        2. Native-header tool removal
      2. JDK-related submitted proposals
        1. Parallelize the Full GC Phase in CMS
        2. REST APIs for JMX
        3. Support heap allocation
      3. JDK-related drafted proposals
        1. Finalization promptness
        2. Java memory model
        3. Foreign Function Interfaces
        4. Isolated methods
        5. Reducing metaspace waste
        6. Improving IPv6 support
        7. Unboxed argument lists for method handles
        8. Enhanced MandelblotSet demo using value types
        9. Efficient array comparison intrinsics
    2. Future changes to the Java Compiler
      1. Policy for retiring javac -source and -target options
      2. Pluggable static analyzers
    3. Future Changes to the Java Virtual Machine
      1. JVM-related submitted proposals
        1. Container aware Java
        2. Enable execution of Java methods on GPU
        3. Epsilon GC - The arbitrarily low overhead garbage (non-) collector
      2. JVM-related drafted proposals
        1. Provide stable USDT probe points on JVM compiled methods
        2. Concurrent monitor deflation
        3. Provide a low-overhead way of sampling Java heap allocations
        4. Diagnostic Command Framework
        5. Enhanced Class Redefinition
        6. Enable NUMA mode by default when appropriate
        7. Value objects
        8. Align JVM Access Checks
    4. Future Changes to JavaX
      1. JMX specific annotations for registration of managed resources
      2. Modernize the GTK3 Look and Feel Implementation
    5. Ongoing Special Projects
      1. Annotations pipeline 2.0
      2. Audio Synthesis Engine
      3. Caciocavallo
      4. Common VM Interface
      5. Compiler Grammar
      6. Da Vinci Machine
      7. Device I/O
      8. Graal
      9. HarfBuzz Integration
      10. Kona
      11. OpenJFX
      12. Panama
      13. Shenandoah
    6. Summary
  22. Java 9 Programming Blueprints
  23. Introduction
    1. New features in Java 8
      1. Lambdas
        1. Streams
      2. The new java.time package
      3. Default methods
    2. New features in Java 9
      1. Java Platform Module System/Project Jigsaw
      2. Process handling API
      3. Concurrency changes
      4. REPL
    3. Projects
      1. Process Viewer/Manager
      2. Duplicate File Finder
      3. Date Calculator
      4. Social Media Aggregator
      5. Email filter
      6. JavaFX photo management
      7. A client/server note application
      8. Serverless Java
      9. Android desktop synchronization client
    4. Getting started
    5. Summary
  24. Managing Processes in Java
    1. Creating a project
    2. Bootstrapping the application
    3. Defining the user interface
    4. Initializing the user interface
    5. Adding menus
    6. Updating the process list
    7. Summary
  25. Duplicate File Finder
    1. Getting started
    2. Building the library
      1. Concurrent Java with a Future interface
      2. Modern database access with JPA
    3. Building the command-line interface
    4. Building the graphical user interface
    5. Summary
  26. Date Calculator
    1. Getting started
    2. Building the library
      1. A timely interlude
        1. Duration
        2. Period
        3. Clock
          1. Instant
          2. LocalDate
          3. LocalTime
          4. LocalDateTime
          5. ZonedDateTime
        4. Back to our code
        5. A brief interlude on testing
    3. Building the command-line interface
    4. Summary
  27. Sunago - A Social Media Aggregator
    1. Getting started
      1. Setting up the user interface
      2. Setting up the controller
      3. Writing the model class
      4. Finishing up the controller
      5. Adding an image for the item
      6. Building the preferences user interface
      7. Saving user preferences
      8. Plugins and extensions with the Service Provider Interface
      9. Resource handling with try-with-resources
    2. Adding a network - Twitter
      1. Registering as a Twitter developer
      2. Adding Twitter preferences to Sunago
      3. OAuth and logging on to Twitter
      4. Adding a model for Twitter
      5. Implementing a Twitter client
      6. A brief look at internationalization and localization
      7. Making our JAR file fat
      8. Adding a refresh button
    3. Adding another network - Instagram
      1. Registering as an Instagram developer
      2. Implementing the Instagram client
      3. Loading our plugins in Sunago
    4. Summary
  28. Sunago - An Android Port
    1. Getting started
    2. Building the user interface
      1. Android data access
      2. Android services
      3. Android tabs and fragments
    3. Summary
  29. Email and Spam Management with MailFilter
    1. Getting started
    2. A brief look at the history of email protocols
      1. JavaMail, the Standard Java API for Email
      2. Building the CLI
      3. Building the GUI
      4. Building the service
    3. Summary
  30. Photo Management with PhotoBeans
    1. Getting started
    2. Bootstrapping the project
      1. Branding your application
    3. NetBeans modules
    4. TopComponent - the class for tabs and windows
    5. Nodes, a NetBeans presentation object
    6. Lookup, a NetBeans fundamental
    7. Writing our own nodes
    8. Performing Actions
    9. Services - exposing decoupled functionality
      1. PhotoViewerTopComponent
      2. Integrating JavaFX with the NetBeans RCP
      3. NetBeans preferences and the Options panel
        1. Adding a primary panel
        2. Adding a secondary panel
        3. Loading and saving preferences
        4. Reacting to changes in preferences
    10. Summary
  31. Taking Notes with Monumentum
    1. Getting started
      1. Microservice frameworks on the JVM
      2. Creating the application
      3. Creating REST Services
      4. Adding MongoDB
      5. Dependency injection with CDI
      6. Finish the notes resource
      7. Adding authentication
      8. Building the user interface
    2. Summary
  32. Serverless Java
    1. Getting started
    2. Planning the application
    3. Building your first function
      1. DynamoDB
      2. Simple Email Service
      3. Simple Notification Service
      4. Deploying the function
        1. Creating a role
        2. Creating a topic
        3. Deploying the function
        4. Testing the function
        5. Configuring your AWS credentials
    4. Summary
  33. DeskDroid - A Desktop Client for Your Android Phone
    1. Getting started
    2. Creating the Android project
      1. Requesting permissions
      2. Creating the service
    3. Server-sent events
      1. Controlling the service state
      2. Adding endpoints to the server
        1. Getting conversations
        2. Sending an SMS message
    4. Creating the desktop application
      1. Defining the user interface
      2. Defining user interface behavior
      3. Sending messages
      4. Getting updates
      5. Security
        1. Securing the endpoints
        2. Handling authorization requests
        3. Authorizing the client
    5. Summary
  34. What is Next?
    1. Looking back
    2. Looking forward
      1. Project Valhalla
        1. Value types
        2. Generic specialization
        3. Reified generics
      2. Project Panama
      3. Project Amber
        1. Local-Variable Type Inference
        2. Enhanced enums
        3. Lambda leftovers
    3. Looking around
      1. Ceylon
      2. Kotlin
    4. Summary
  35. Bibliography