Designing Hexagonal Architecture with Java - Second Edition

Book description

Learn to build robust, resilient, and highly maintainable cloud-native Java applications with hexagonal architecture and Quarkus

Key Features

  • Use hexagonal architecture to increase maintainability and reduce technical debt
  • Learn how to build systems that are easy to change and understand
  • Leverage Quarkus to create modern cloud-native applications
  • Purchase of the print or Kindle book includes a free PDF eBook

Book Description

We live in a fast-evolving world with new technologies emerging every day, where enterprises are constantly changing in an unending quest to be more profitable. So, the question arises — how to develop software capable of handling a high level of unpredictability. With this question in mind, this book explores how the hexagonal architecture can help build robust, change-tolerable, maintainable, and cloud-native applications that can meet the needs of enterprises seeking to increase their profits while dealing with uncertainties. This book starts by uncovering the secrets of the hexagonal architecture’s building blocks, such as entities, use cases, ports, and adapters. You’ll learn how to assemble business code in the domain hexagon, create features with ports and use cases in the application hexagon, and make your software compatible with different technologies by employing adapters in the framework hexagon. In this new edition, you’ll learn about the differences between a hexagonal and layered architecture and how to apply SOLID principles while developing a hexagonal system based on a real-world scenario. Finally, you’ll get to grips with using Quarkus to turn your hexagonal application into a cloud-native system.

By the end of this book, you’ll be able to develop robust, flexible, and maintainable systems that will stand the test of time.

What you will learn

  • Apply SOLID principles to the hexagonal architecture
  • Assemble business rules algorithms using the specified design pattern
  • Combine domain-driven design techniques with hexagonal principles to create powerful domain models
  • Employ adapters to enable system compatibility with various protocols such as REST, gRPC, and WebSocket
  • Create a module and package structure based on hexagonal principles
  • Use Java modules to enforce dependency inversion and ensure software component isolation
  • Implement Quarkus DI to manage the life cycle of input and output ports

Who this book is for

This book is for software architects and Java developers looking to improve code maintainability and enhance productivity with an architecture that allows changes in technology without compromising business logic. Intermediate knowledge of the Java programming language and familiarity with Jakarta EE will help you to get the most out of this book.

Publisher resources

Download Example Code

Table of contents

  1. Designing Hexagonal Architecture with Java
  2. Contributors
  3. About the author
  4. About the reviewers
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Conventions used
    6. Get in touch
    7. Share Your Thoughts
    8. Download a free PDF copy of this book
  6. Part 1: Architecture Fundamentals
  7. Chapter 1: Why Hexagonal Architecture?
    1. Technical requirements
    2. Reviewing software architecture
      1. Making decisions
      2. The invisible things
      3. Technical debt
      4. Vicious cycle
      5. It’s not for everyone
      6. Monolithic or distributed
    3. Understanding hexagonal architecture
      1. Domain hexagon
      2. Application hexagon
      3. Use cases
      4. Output ports
      5. Framework hexagon
      6. Advantages of the hexagonal approach
    4. Summary
    5. Questions
    6. Further reading
    7. Answers
  8. Chapter 2: Wrapping Business Rules inside Domain Hexagon
    1. Technical requirements
    2. Modeling a problem domain with entities
      1. The purity of domain entities
      2. Relevant entities
      3. Using UUIDs to define identity
    3. Enhancing descriptiveness with value objects
      1. Using value objects to compose entities
    4. Assuring consistency with aggregates
      1. Modeling an aggregate
    5. Working with domain services
    6. Using policy and specification to deal with business rules
      1. Creating specifications
      2. Creating policies
    7. Defining business rules as POJOs
    8. Summary
    9. Questions
    10. Further reading
    11. Answers
  9. Chapter 3: Handling Behavior with Ports and Use Cases
    1. Technical requirements
    2. Expressing software behavior with use cases
      1. How to create a use case
    3. Implementing use cases with input ports
    4. Using output ports to deal with external data
      1. It’s not only about repositories
      2. Where to use output ports
    5. Automating behavior with the Application hexagon
    6. Summary
    7. Questions
    8. Further reading
    9. Answers
  10. Chapter 4: Creating Adapters to Interact with the Outside World
    1. Technical requirements
    2. Understanding adapters
    3. Using input adapters to allow driving operations
      1. Creating input adapters
    4. Using output adapters to speak with different 
data sources
      1. Creating output adapters
    5. Summary
    6. Questions
    7. Answers
    8. Further reading
  11. Chapter 5: Exploring the Nature of Driving and Driven Operations
    1. Technical requirements
    2. Reaching the hexagonal application with driving operations
    3. Integrating web applications with the hexagonal system
      1. Creating the Add Network page
      2. Creating the Get Router page
      3. Running test agents
      4. Calling the hexagonal system from other applications
    4. Handling external resources with driven operations
      1. Data persistence
      2. Messaging and events
      3. Mock servers
    5. Summary
    6. Questions
    7. Answers
  12. Part 2: Using Hexagons to Create a Solid Foundation
  13. Chapter 6: Building the Domain Hexagon
    1. Technical requirements
    2. Bootstrapping the Domain hexagon
    3. Understanding the problem domain
    4. Defining value objects
    5. Defining entities and specifications
      1. The Equipment and Router abstract entities
      2. The core router entity and its specifications
      3. Edge router entity and its specifications
      4. Switch entity and its specifications
    6. Defining domain services
      1. Router service
      2. Switch service
      3. Network service
    7. Testing the Domain hexagon
    8. Summary
    9. Questions
    10. Answers
  14. Chapter 7: Building the Application Hexagon
    1. Technical requirements
    2. Bootstrapping the Application hexagon
    3. Defining use cases
      1. Creating written descriptions for router management use cases
      2. Defining the use case interface for router management
      3. Creating written descriptions for switch management use cases
      4. Defining the use case interface for switch management
      5. Creating written descriptions for network management use cases
      6. Defining the use case interface for network management
    4. Implementing use cases with input ports
      1. Testing the Application hexagon
    5. Summary
    6. Questions
    7. Answers
  15. Chapter 8: Building the Framework Hexagon
    1. Technical requirements
    2. Bootstrapping the Framework hexagon
    3. Implementing output adapters
      1. The router management output adapter
      2. The switch management output adapter
    4. Implementing the input adapters
      1. The router management input adapter
      2. The switch management input adapter
      3. The network management input adapter
    5. Testing the Framework hexagon
    6. Summary
      1. Questions
    7. Answers
  16. Chapter 9: Applying Dependency Inversion with Java Modules
    1. Technical requirements
    2. Introducing the JPMS
    3. Inverting dependencies on a hexagonal application
      1. Providing services with use cases and input ports
      2. Providing services with output ports and output adapters
      3. Making the input adapters dependent on abstractions
    4. Using the Java platform’s ServiceLoader class to retrieve JPMS provider implementations
      1. Initializing RouterManagementGenericAdapter
      2. Initializing SwitchManagementGenericAdapter
      3. Initializing NetworkManagementGenericAdapter
    5. Summary
    6. Questions
    7. Answers
    8. Further reading
  17. Part 3: Becoming Cloud-Native
  18. Chapter 10: Adding Quarkus to a Modularized Hexagonal Application
    1. Technical requirements
    2. Revisiting the JVM
      1. Speeding up runtime performance with JIT compilation
      2. Improving startup time with AOT compilation
    3. Introducing Quarkus
      1. Creating REST endpoints with JAX-RS
      2. Employing dependency injection with Quarkus DI
      3. Validating objects
      4. Configuring a data source and using Hibernate ORM
    4. Adding Quarkus to a modularized hexagonal application
    5. Summary
    6. Questions
    7. Answers
  19. Chapter 11: Leveraging CDI Beans to Manage Ports and Use Cases
    1. Technical requirements
    2. Learning about Quarkus DI
      1. Working with beans
    3. Transforming ports, use cases, and adapters into CDI beans
      1. Implementing CDI for router management objects
      2. Implementing CDI for switch management objects
      3. Implementing CDI for network management classes and interfaces
    4. Testing use cases with Quarkus and Cucumber
    5. Summary
    6. Questions
    7. Answers
  20. Chapter 12: Using RESTEasy Reactive to Implement Input Adapters
    1. Technical requirements
    2. Exploring the approaches to handling server’s requests
      1. Imperative
      2. Reactive
    3. Implementing input adapters with RESTEasy Reactive
      1. Implementing the Reactive input adapter for router management
      2. Implementing the Reactive input adapter for switch management
      3. Implementing the Reactive input adapter for network management
    4. Adding OpenAPI and Swagger UI
    5. Testing Reactive input adapters
    6. Summary
    7. Questions
    8. Answers
  21. Chapter 13: Persisting Data with Output Adapters and Hibernate Reactive
    1. Technical requirements
    2. Introducing Hibernate Reactive and Panache
      1. Hibernate Reactive features
      2. Panache features
    3. Enabling reactive behavior on output adapters
      1. Configuring reactive data sources
      2. Configuring entities
      3. Implementing reactive repository classes
      4. Implementing reactive output adapters
      5. Reactive router management of the MySQL output adapter
      6. Reactive switch management of the MySQL output adapter
    4. Testing the reactive output adapters
    5. Summary
    6. Questions
    7. Answers
  22. Chapter 14: Setting Up Dockerfile and Kubernetes Objects for Cloud Deployment
    1. Technical requirements
    2. Preparing the Docker image
      1. Creating a Docker image with an uber .jar artifact
      2. Creating a Docker image with a native executable
    3. Creating Kubernetes objects
      1. Reviewing Kubernetes' main objects
      2. Configuring Kubernetes objects for the hexagonal system
    4. Deploying on minikube
    5. Summary
    6. Questions
    7. Answers
  23. Part 4: Hexagonal Architecture and Beyond
  24. Chapter 15: Comparing Hexagonal Architecture with Layered Architecture
    1. Technical requirements
    2. Reviewing the layered architecture
    3. Creating an application using the layered architecture
      1. Implementing the data layer
      2. Implementing the service layer
      3. Implementing the API layer
      4. Testing the layered application
    4. Refactoring a layered architecture application into a hexagonal one
      1. Implementing the Domain hexagon
      2. Implementing the Application hexagon
      3. Implementing the Framework hexagon
      4. Testing the hexagonal application
    5. Assessing the benefits and disadvantages of hexagonal and layered architectures
    6. Summary
    7. Questions
    8. Answers
  25. Chapter 16: Using SOLID Principles with Hexagonal Architecture
    1. Technical requirements
    2. Understanding SOLID principles
      1. Single Responsibility Principle (SRP)
      2. Open-Closed Principle (OCP)
      3. Liskov Substitution Principle (LSP)
      4. Interface Segregation Principle (ISP)
      5. Dependency Inversion Principle (DIP)
    3. Applying SOLID on a hexagonal architecture system
      1. Applying the SRP
      2. Applying the OCP
      3. Applying the LSP
      4. Applying the ISP
      5. Applying the DIP
    4. Exploring other design patterns
      1. Singleton
      2. Builder
      3. Abstract factory
    5. Summary
    6. Questions
    7. Answers
  26. Chapter 17: Good Design Practices for Your Hexagonal Application
    1. Technical requirements
    2. Using Domain-Driven Design to shape the Domain hexagon
      1. Understanding the business we are in
      2. Promoting collaboration to increase knowledge
      3. Applying DDD techniques to build the Domain hexagon
      4. Implementing bounded contexts and subdomains in a hexagonal system
    3. The need for creating ports and use cases
    4. Dealing with multiple adapter categories
    5. Conclusion – the hexagonal journey
    6. Summary
    7. Questions
    8. Answers
  27. Index
    1. Why subscribe?
  28. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts
    3. Download a free PDF copy of this book

Product information

  • Title: Designing Hexagonal Architecture with Java - Second Edition
  • Author(s): Davi Vieira
  • Release date: September 2023
  • Publisher(s): Packt Publishing
  • ISBN: 9781837635115