Practical Domain-Driven Design in Enterprise Using Jakarta EE, MicroProfile, Spring Boot, and the Axon Framework

Vijay Nair Practical Domain-Driven Design in Enterprise Java: Using Jakarta EE, Eclipse MicroProfile, Spring Boot, and the Axon Framework

Vijay Nair Mountain View, CA, USA

ISBN-13 (pbk): 978-1-4842-4542-2 ISBN-13 (electronic): 978-1-4842-4543-9 https://doi.org/10.1007/978-1-4842-4543-9 Copyright © 2019 by Vijay Nair This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Steve Anglin Development Editor: Matthew Moodie Coordinating Editor: Mark Powers Cover designed by eStudioCalamar Cover image designed by Freepik (www.freepik.com) Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer- sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected]; for reprint, paperback, or audio rights, please email [email protected]. Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book's product page, located at www.apress.com/9781484245422. For more detailed information, please visit http://www.apress.com/source-code. Printed on acid-free paper To Tina and Maya Table of Contents

About the Author ����������������������������������������������������������������������������������������������������� ix About the Technical Reviewer ��������������������������������������������������������������������������������� xi Acknowledgments ������������������������������������������������������������������������������������������������� xiii Introduction �������������������������������������������������������������������������������������������������������������xv

Chapter 1: Domain Driven Design ����������������������������������������������������������������������������� 1 DDD Concepts ������������������������������������������������������������������������������������������������������������������������������� 2 Problem Space/Business Domain ������������������������������������������������������������������������������������������� 2 Sub-Domains/Bounded Contexts ��������������������������������������������������������������������������������������������� 4 The Domain Model ������������������������������������������������������������������������������������������������������������������������ 8 Aggregates/Entity Objects/Value Objects ������������������������������������������������������������������������������ 10 Domain Rules ������������������������������������������������������������������������������������������������������������������������ 11 Commands/Queries ��������������������������������������������������������������������������������������������������������������� 12 Events ������������������������������������������������������������������������������������������������������������������������������������ 12 Sagas ������������������������������������������������������������������������������������������������������������������������������������� 13 Summary ������������������������������������������������������������������������������������������������������������������������������������ 15

Chapter 2: Cargo Tracker ���������������������������������������������������������������������������������������� 17 Core Domain ������������������������������������������������������������������������������������������������������������������������������� 17 Cargo Tracker: Sub-Domains/Bounded Contexts ������������������������������������������������������������������������ 18 Cargo Tracker: Domain Model ����������������������������������������������������������������������������������������������������� 22 Aggregates ���������������������������������������������������������������������������������������������������������������������������� 22 Aggregate Identifiers ������������������������������������������������������������������������������������������������������������� 23 Entities ����������������������������������������������������������������������������������������������������������������������������������� 23 Value Objects ������������������������������������������������������������������������������������������������������������������������� 24

v Table of Contents

Cargo Tracker: Domain Model Operations ����������������������������������������������������������������������������������� 29 Sagas ������������������������������������������������������������������������������������������������������������������������������������������ 30 Domain Model Services �������������������������������������������������������������������������������������������������������������� 32 Domain Model Services Design �������������������������������������������������������������������������������������������������� 34 Cargo Tracker: DDD Implementations ����������������������������������������������������������������������������������������� 37 Summary ������������������������������������������������������������������������������������������������������������������������������������ 37

Chapter 3: Cargo Tracker: Jakarta EE ��������������������������������������������������������������������� 39 The Java EE Platform ������������������������������������������������������������������������������������������������������������������ 40 Rebranding to Jakarta EE and the Way Forward ������������������������������������������������������������������������� 40 Jakarta EE Platform Specifications ��������������������������������������������������������������������������������������������� 41 Technologies ����������������������������������������������������������������������������������������������� 42 Enterprise Application Technologies �������������������������������������������������������������������������������������� 45 Web Services in Jakarta EE ��������������������������������������������������������������������������������������������������� 47 Security Technologies ������������������������������������������������������������������������������������������������������������ 47 Jakarta EE Specification Summary ��������������������������������������������������������������������������������������� 48 Cargo Tracker as a Modular Monolith ����������������������������������������������������������������������������������������� 48 Bounded Context(s) with Jakarta EE ������������������������������������������������������������������������������������� 50 Implementing the Domain Model with Jakarta EE ���������������������������������������������������������������� 55 Implementing Domain Model Services with Jakarta EE �������������������������������������������������������� 78 Inbound Services ������������������������������������������������������������������������������������������������������������������� 78 RESTful API(s) ������������������������������������������������������������������������������������������������������������������������ 79 Native Web API(s) ������������������������������������������������������������������������������������������������������������������� 79 Application Services �������������������������������������������������������������������������������������������������������������� 81 Application Services: Events ������������������������������������������������������������������������������������������������� 84 Outbound Services ���������������������������������������������������������������������������������������������������������������� 88 Implementation Summary ����������������������������������������������������������������������������������������������������� 90 Summary ������������������������������������������������������������������������������������������������������������������������������������ 91

vi Table of Contents

Chapter 4: Cargo Tracker: Eclipse MicroProfile ������������������������������������������������������ 93 Eclipse MicroProfile �������������������������������������������������������������������������������������������������������������������� 94 Eclipse MicroProfile: Capabilities ������������������������������������������������������������������������������������������������ 96 Eclipse MicroProfile: Core Specifications ������������������������������������������������������������������������������ 98 Eclipse MicroProfile: Supporting Specifications ������������������������������������������������������������������ 101 Eclipse MicroProfile Specification Summary ����������������������������������������������������������������������� 103 Cargo Tracker Implementation: Eclipse MicroProfile ���������������������������������������������������������������� 104 Implementation Choice: Helidon MP ������������������������������������������������������������������������������������ 105 Cargo Tracker Implementation: Bounded Context(s) ����������������������������������������������������������� 106 Bounded Contexts: Packaging ��������������������������������������������������������������������������������������������� 108 Bounded Contexts: Package Structure �������������������������������������������������������������������������������� 111 Cargo Tracker Implementation �������������������������������������������������������������������������������������������� 118 Domain Model: Implementation ������������������������������������������������������������������������������������������� 120 Core Domain Model: Implementation ���������������������������������������������������������������������������������� 120 Domain Model Operations ��������������������������������������������������������������������������������������������������� 135 Domain Model Services ������������������������������������������������������������������������������������������������������� 144 Implementation Summary ��������������������������������������������������������������������������������������������������� 187 Summary ���������������������������������������������������������������������������������������������������������������������������������� 188

Chapter 5: Cargo Tracker: Spring Platform ����������������������������������������������������������� 189 The Spring Platform ������������������������������������������������������������������������������������������������������������������ 190 Spring Boot: Capabilities ����������������������������������������������������������������������������������������������������� 193 Spring Cloud ������������������������������������������������������������������������������������������������������������������������ 194 Summary ���������������������������������������������������������������������������������������������� 195 Bounded Context(s) with Spring Boot ��������������������������������������������������������������������������������������� 196 Bounded Contexts: Packaging ��������������������������������������������������������������������������������������������� 198 Bounded Contexts: Package Structure �������������������������������������������������������������������������������� 199 interfaces ���������������������������������������������������������������������������������������������������������������������������� 201 application ��������������������������������������������������������������������������������������������������������������������������� 202 domain ��������������������������������������������������������������������������������������������������������������������������������� 203 infrastructure ����������������������������������������������������������������������������������������������������������������������� 204 Cargo Tracker Implementation �������������������������������������������������������������������������������������������� 207

vii Table of Contents

Domain Model: Implementation ������������������������������������������������������������������������������������������������ 210 Core Domain Model: Implementation ���������������������������������������������������������������������������������� 210 Domain Model Operations ��������������������������������������������������������������������������������������������������� 224 Commands �������������������������������������������������������������������������������������������������������������������������� 224 Queries �������������������������������������������������������������������������������������������������������������������������������� 229 Domain Events ��������������������������������������������������������������������������������������������������������������������� 229 Domain Model Services ������������������������������������������������������������������������������������������������������������ 236 Inbound Services ����������������������������������������������������������������������������������������������������������������� 237 Application Services ������������������������������������������������������������������������������������������������������������ 248 Outbound Services �������������������������������������������������������������������������������������������������������������� 255 Implementation Summary ��������������������������������������������������������������������������������������������������� 274 Summary ���������������������������������������������������������������������������������������������������������������������������������� 276

Chapter 6: Cargo Tracker: Axon Framework ��������������������������������������������������������� 277 Event Sourcing �������������������������������������������������������������������������������������������������������������������������� 278 CQRS ����������������������������������������������������������������������������������������������������������������������������������������� 281 The Axon Framework ���������������������������������������������������������������������������������������������������������������� 285 Axon Components ���������������������������������������������������������������������������������������������������������������� 286 Axon Framework Domain Model Components �������������������������������������������������������������������� 288 Axon Infrastructure Components: Axon Server �������������������������������������������������������������������� 296 Cargo Tracker with Axon ����������������������������������������������������������������������������������������������������������� 300 Bounded Contexts with Axon ����������������������������������������������������������������������������������������������� 300 Bounded Contexts: Artifact Creation ������������������������������������������������������������������������������������ 303 Bounded Contexts: Package Structure �������������������������������������������������������������������������������� 305 Implementing the Domain Model with Axon ������������������������������������������������������������������������ 312 Implementation Summary ��������������������������������������������������������������������������������������������������� 363 Implementing Domain Model Services with Axon ��������������������������������������������������������������� 364 Inbound Services ����������������������������������������������������������������������������������������������������������������� 364 Application Services ������������������������������������������������������������������������������������������������������������ 370 Summary ���������������������������������������������������������������������������������������������������������������������������������� 372

Index ��������������������������������������������������������������������������������������������������������������������� 373 viii About the Author

Vijay Nair is currently Director of Platform Engineering for Oracle’s Banking SaaS applications. A Domain Driven Design (DDD) and distributed systems enthusiast, he has around 18 years of experience in architecting, building, and implementing mission-critical applications for the financial services industry around the world. He can be reached at his personal web site www.practicalddd.com or via Twitter at @FusionVJ. He lives in Mountain View, CA with his wife and daughter.

ix About the Technical Reviewer

Manuel Jordan Elera is an autodidactic developer and researcher who enjoys learning new technologies for his own experiments and creating new integrations. Manuel won the Springy Award – Community Champion and Spring Champion 2013. In his little free time, he reads the Bible and composes music on his guitar. Manuel is known as dr_pompeii. He has tech-reviewed numerous books for Apress, including Pro Spring, Fourth Edition (2014); Practical Spring LDAP (2013); Pro JPA 2, Second Edition (2013); and Pro (2013). Read his 13 detailed tutorials about many Spring technologies, contact him through his blog site at www.manueljordanelera.blogspot.com, and follow him on his Twitter account, @dr_pompeii­ .

xi Acknowledgments

The first person I would like to wholeheartedly thank for making this book possible is Jakarta EE (Enterprise Edition) guru Reza Rahman. While at Oracle, he kick-started the Cargo Tracker initiative as a blueprint for Java EE Patterns based on DDD. The opportunity that he gave me to participate in the project is something that I will always be grateful for. Thanks to fellow DDD and Axon Framework enthusiast Swapnil Surve, an architect based out of Phoenix, for the content review and suggestions for Chapters 5 and 6. A special thanks to Allard Buijze (Creator of the Axon Framework) for the content review for Chapter 6. Thanks to all the folks at Oracle who helped support the writing of the book, my senior management (Vikram, Ticks, Chet), and my own team who educate me every day (Sourabh, Shripad, Hari, Pawan, Dasharath, and Mahendran). To the City of Mountain View, CA, thank you for providing the sanctuary of a fabulous library where many an hour has been spent writing this book. Writing a book with a newborn is something sane people do not do. The support provided by my family has been immense during this entire journey, and I am eternally grateful for that. Thanks to my brothers, Gautam, Rohit, Sumit, and Sachin; to my sister, Vinitha, and her husband, Madhu; to my other babies, Varun and Arya; and finally to my parents and my wife’s parents for sacrificing their time to be with us and helping us out. Last but not the least, to my wife, Tina. Juggling work, taking care of the kid, and ensuring I got my writing time required a superhuman effort with a lot of personal sacrifice. This book is as much yours as it is mine. Thank you!

xiii Introduction

Domain Driven Design has never been more relevant than in today’s world of software development. DDD concepts and patterns help build well-designed enterprise applications, be it traditional monoliths or new age -based applications. This book aims to demystify the concepts of Domain Driven Design by providing a practical approach to its implementation for traditional monolithic applications as well as for new age microservices applications. Using a reference application – Cargo Tracker – the book walks through detailed implementations of the various DDD patterns for both styles of applications utilizing various tools and frameworks from the Enterprise Java Space (Jakarta EE, Eclipse MicroProfile, Spring Boot, and the Axon Framework). This gives a complete rounded view to the reader of the book intending to use any of these frameworks for their DDD journey. Enjoy reading!

xv