0% found this document useful (0 votes)
49 views36 pages

Pro Jakarta EE 10: Open Source Enterprise Java-based Cloud-native Applications Development Peter Späth instant download

The document is a comprehensive guide to developing cloud-native applications using Jakarta EE 10, authored by Peter Späth. It covers various aspects of the development workflow, including setting up development environments, using IDEs like Eclipse and NetBeans, version control with Git and Subversion, and continuous integration with Jenkins. Additionally, it delves into advanced web tier topics, such as Facelets, custom components, and websockets.

Uploaded by

evalisuidong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views36 pages

Pro Jakarta EE 10: Open Source Enterprise Java-based Cloud-native Applications Development Peter Späth instant download

The document is a comprehensive guide to developing cloud-native applications using Jakarta EE 10, authored by Peter Späth. It covers various aspects of the development workflow, including setting up development environments, using IDEs like Eclipse and NetBeans, version control with Git and Subversion, and continuous integration with Jenkins. Additionally, it delves into advanced web tier topics, such as Facelets, custom components, and websockets.

Uploaded by

evalisuidong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Pro Jakarta EE 10: Open Source Enterprise Java-

based Cloud-native Applications Development


Peter Späth pdf download

https://ebookmass.com/product/pro-jakarta-ee-10-open-source-
enterprise-java-based-cloud-native-applications-development-
peter-spath/

Explore and download more ebooks at ebookmass.com


We believe these products will be a great fit for you. Click
the link to download now, or visit ebookmass.com
to discover even more!

Beginning Cloud Native Development with MicroProfile,


Jakarta EE, and Kubernetes: Java DevOps for Building and
Deploying Microservices-based Applications 1st Edition
Tarun Telang
https://ebookmass.com/product/beginning-cloud-native-development-with-
microprofile-jakarta-ee-and-kubernetes-java-devops-for-building-and-
deploying-microservices-based-applications-1st-edition-tarun-telang/

Beginning Cloud Native Development with MicroProfile,


Jakarta EE, and Kubernetes 1st Edition Tarun Telang

https://ebookmass.com/product/beginning-cloud-native-development-with-
microprofile-jakarta-ee-and-kubernetes-1st-edition-tarun-telang/

Beginning Spring Boot 3: Build Dynamic Cloud-Native Java


Applications and Microservices - Second Edition K. Siva
Prasad Reddy
https://ebookmass.com/product/beginning-spring-boot-3-build-dynamic-
cloud-native-java-applications-and-microservices-second-edition-k-
siva-prasad-reddy/

Building Modern Business Applications: Reactive Cloud


Architecture for Java, Spring, and PostgreSQL 1st Edition
Peter Royal
https://ebookmass.com/product/building-modern-business-applications-
reactive-cloud-architecture-for-java-spring-and-postgresql-1st-
edition-peter-royal-2/
Building Modern Business Applications: Reactive Cloud
Architecture for Java, Spring, and PostgreSQL 1st Edition
Peter Royal
https://ebookmass.com/product/building-modern-business-applications-
reactive-cloud-architecture-for-java-spring-and-postgresql-1st-
edition-peter-royal/

Pro Spring Security: Securing Spring Framework 6 and Boot


3–based Java Applications, Third Edition Massimo Nardone

https://ebookmass.com/product/pro-spring-security-securing-spring-
framework-6-and-boot-3-based-java-applications-third-edition-massimo-
nardone/

Modern Enterprise Architecture: Using DevSecOps and Cloud-


Native in Large Enterprises 1st Edition Jeroen Mulder

https://ebookmass.com/product/modern-enterprise-architecture-using-
devsecops-and-cloud-native-in-large-enterprises-1st-edition-jeroen-
mulder/

Beginning EJB 3: Java EE 7 Edition Jonathan Wetherbee

https://ebookmass.com/product/beginning-ejb-3-java-ee-7-edition-
jonathan-wetherbee/

Cloud-native Computing Pethuru Raj

https://ebookmass.com/product/cloud-native-computing-pethuru-raj/
Pro
Jakarta EE 10
Open Source Enterprise Java-based
Cloud-native Applications Development

Peter Späth
Pro Jakarta EE 10
Open Source Enterprise
Java-based Cloud-native
Applications Development

Peter Späth
Pro Jakarta EE 10: Open Source Enterprise Java-based Cloud-native Applications
Development

Peter Späth
Leipzig, Sachsen, Germany

ISBN-13 (pbk): 978-1-4842-8213-7 ISBN-13 (electronic): 978-1-4842-8214-4


https://doi.org/10.1007/978-1-4842-8214-4
Copyright © 2023 by Peter Späth
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: Laura Berendson
Coordinating Editor: Mark Powers
Copy Editor: Kezia Endsley
Cover designed by eStudioCalamar
Cover image by Dapiki Moto on Unsplash (www.unsplash.com)
Distributed to the book trade worldwide by Apress Media, LLC, 1 New York Plaza, New York, NY 10004,
U.S.A. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], 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 e-mail [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 (https://github.com/Apress). For more detailed information, please visit
http://www.apress.com/source-code.
Printed on acid-free paper
To Nicole.
Table of Contents
About the Author�����������������������������������������������������������������������������������������������������xv

About the Technical Reviewer�������������������������������������������������������������������������������xvii

Introduction������������������������������������������������������������������������������������������������������������xix

Part I: Development Workflow Proposal��������������������������������������������������������� 1


Chapter 1: Installing a Development Server������������������������������������������������������������� 3
Installing and Running Under Linux���������������������������������������������������������������������������������������������� 4
Installing and Running Under Windows���������������������������������������������������������������������������������������� 5
GlassFish Application Debugging�������������������������������������������������������������������������������������������������� 6
Elaborated GlassFish Operation Instructions�������������������������������������������������������������������������������� 6

Chapter 2: Creating and Building Projects with Eclipse������������������������������������������� 7


Installing Eclipse��������������������������������������������������������������������������������������������������������������������������� 7
Configuring Eclipse����������������������������������������������������������������������������������������������������������������������� 9
Adding Java Runtimes���������������������������������������������������������������������������������������������������������������� 10
Adding Plugins���������������������������������������������������������������������������������������������������������������������������� 10
Using Eclipse to Administer a Jakarta EE Server������������������������������������������������������������������������ 11
Eclipse Everyday Usage�������������������������������������������������������������������������������������������������������������� 19
Changing the Project Layout������������������������������������������������������������������������������������������������������� 20

Chapter 3: Development with NetBeans as an IDE������������������������������������������������� 23


Installing NetBeans��������������������������������������������������������������������������������������������������������������������� 23
Starting a NetBeans Project�������������������������������������������������������������������������������������������������������� 24
Multi-IDE Projects����������������������������������������������������������������������������������������������������������������������� 25

v
Table of Contents

Chapter 4: Git and Subversion�������������������������������������������������������������������������������� 27


The Subversion Version Control System������������������������������������������������������������������������������������� 27
Subversion Clients���������������������������������������������������������������������������������������������������������������������� 32
The Git Version Control System��������������������������������������������������������������������������������������������������� 36
Git Clients������������������������������������������������������������������������������������������������������������������������������������ 39

Chapter 5: Continuous Integration�������������������������������������������������������������������������� 43


The Jenkins CI Server����������������������������������������������������������������������������������������������������������������� 43
Starting a Jenkins Project����������������������������������������������������������������������������������������������������������� 44
Build Triggers������������������������������������������������������������������������������������������������������������������������������ 46
Creating Real-World Builds��������������������������������������������������������������������������������������������������������� 49
Triggering Builds from Git����������������������������������������������������������������������������������������������������������� 53
Triggering Builds from Subversion���������������������������������������������������������������������������������������������� 55
Analyzing Jenkins Builds via REST��������������������������������������������������������������������������������������������� 56

Chapter 6: Corporate Maven Repositories�������������������������������������������������������������� 61


The Maven Repository Layout����������������������������������������������������������������������������������������������������� 63
A Simple Server to Provide Mavenized Artifacts������������������������������������������������������������������������� 64
The Marvin EAR Root Project������������������������������������������������������������������������������������������������� 65
The Marvin Web Project��������������������������������������������������������������������������������������������������������� 69
The Marvin EJB Project��������������������������������������������������������������������������������������������������������� 70
Laying Out the Projects Again������������������������������������������������������������������������������������������������ 71
The Web Project Code������������������������������������������������������������������������������������������������������������ 72
The EJB Project Code������������������������������������������������������������������������������������������������������������ 75
Building and Deploying the EAR�������������������������������������������������������������������������������������������� 79
Using the Corporate Repository��������������������������������������������������������������������������������������������� 79
A Sample Java Library����������������������������������������������������������������������������������������������������������� 80
Building and Uploading Company Libraries�������������������������������������������������������������������������������� 82

Part II: Advanced Web Tier Topics���������������������������������������������������������������� 87


Chapter 7: Facelets������������������������������������������������������������������������������������������������� 89
Faces Templating via Facelets���������������������������������������������������������������������������������������������������� 89
Installing Facelets����������������������������������������������������������������������������������������������������������������������� 90
vi
Table of Contents

Facelets Tags Overview�������������������������������������������������������������������������������������������������������������� 91


The <ui:include> Tag������������������������������������������������������������������������������������������������������������� 91
First Variation of the <ui:composition> Tag�������������������������������������������������������������������������� 91
Second Variation of the <ui:composition> Tag���������������������������������������������������������������������� 91
The <ui:insert> Tag��������������������������������������������������������������������������������������������������������������� 92
The <ui:define> Tag�������������������������������������������������������������������������������������������������������������� 93
The <ui:param> Tag�������������������������������������������������������������������������������������������������������������� 93
The <ui:component> Tag������������������������������������������������������������������������������������������������������ 93
The <ui:decorate> Tag���������������������������������������������������������������������������������������������������������� 94
The <ui:fragment> Tag���������������������������������������������������������������������������������������������������������� 95
The <ui:repeat> Tag�������������������������������������������������������������������������������������������������������������� 96
The <ui:debug> Tag�������������������������������������������������������������������������������������������������������������� 97
An Example Facelets Project������������������������������������������������������������������������������������������������������ 98

Chapter 8: Faces Custom Components����������������������������������������������������������������� 111


Custom Tag Libraries���������������������������������������������������������������������������������������������������������������� 111
Composite Components������������������������������������������������������������������������������������������������������������ 114
Custom Components in Java���������������������������������������������������������������������������������������������������� 117

Chapter 9: Flows��������������������������������������������������������������������������������������������������� 119


The Flow Process���������������������������������������������������������������������������������������������������������������������� 119
Basic Flow Setup���������������������������������������������������������������������������������������������������������������������� 120
Overriding Conventions������������������������������������������������������������������������������������������������������������� 123
Specifying a Different Flow Start Page�������������������������������������������������������������������������������� 124
Specifying a Different Return Page������������������������������������������������������������������������������������� 124
Programmatic Configuration����������������������������������������������������������������������������������������������������� 125
Handling Flow Outcome������������������������������������������������������������������������������������������������������������ 127
Passing Data Between Flows���������������������������������������������������������������������������������������������������� 127

Chapter 10: Websockets��������������������������������������������������������������������������������������� 131


Websockets on the Server Side������������������������������������������������������������������������������������������������ 131
Websockets on the Client Side�������������������������������������������������������������������������������������������������� 135

vii
Table of Contents

Chapter 11: Frontend Technologies���������������������������������������������������������������������� 137


No Third-Party Frontend Technology����������������������������������������������������������������������������������������� 138
Play 2���������������������������������������������������������������������������������������������������������������������������������������� 139
React����������������������������������������������������������������������������������������������������������������������������������������� 139
Angular 2����������������������������������������������������������������������������������������������������������������������������������� 141
Spring Boot������������������������������������������������������������������������������������������������������������������������������� 142
Vue�������������������������������������������������������������������������������������������������������������������������������������������� 142
Spring MVC������������������������������������������������������������������������������������������������������������������������������� 143
Ember���������������������������������������������������������������������������������������������������������������������������������������� 144
Act.Framework�������������������������������������������������������������������������������������������������������������������������� 145
Apache Struts 2������������������������������������������������������������������������������������������������������������������������ 145
GWT������������������������������������������������������������������������������������������������������������������������������������������� 146
Vaadin��������������������������������������������������������������������������������������������������������������������������������������� 147
DataTables�������������������������������������������������������������������������������������������������������������������������������� 147
D3js������������������������������������������������������������������������������������������������������������������������������������������� 148

Chapter 12: Form-Based Authentication�������������������������������������������������������������� 149


Enabling Security on the Sever������������������������������������������������������������������������������������������������� 149
Form-Based Authentication for Faces�������������������������������������������������������������������������������������� 151
Security Role Mapping�������������������������������������������������������������������������������������������������������������� 154
Form-Based Authentication XHTML Code��������������������������������������������������������������������������������� 155

Chapter 13: Client Certificates������������������������������������������������������������������������������ 159


Preparing Scripting������������������������������������������������������������������������������������������������������������������� 159
Generating Client Certificates��������������������������������������������������������������������������������������������������� 160
Storing the Client Certificate in the Browser���������������������������������������������������������������������������� 163
Storing the Client Certificate on the Server������������������������������������������������������������������������������ 164
Client Certificate Web Applications������������������������������������������������������������������������������������������� 166
Additional GlassFish Configuration������������������������������������������������������������������������������������������� 168
Client Certificate Example��������������������������������������������������������������������������������������������������������� 169

viii
Table of Contents

Chapter 14: REST Security������������������������������������������������������������������������������������ 175


Security Constraints for REST URLs������������������������������������������������������������������������������������������ 176
About JSON Web Tokens����������������������������������������������������������������������������������������������������������� 176
Preparing GlassFish������������������������������������������������������������������������������������������������������������������ 178
The JWT Login Process, Client Code����������������������������������������������������������������������������������������� 178
Server Code������������������������������������������������������������������������������������������������������������������������������ 184
The JWT Login Process, Server Code��������������������������������������������������������������������������������������� 185
Sending JWTs Back to the Server��������������������������������������������������������������������������������������������� 190
Handling JWTs in REST Endpoints�������������������������������������������������������������������������������������������� 191

Chapter 15: JAVA MVC������������������������������������������������������������������������������������������ 195


About MVC�������������������������������������������������������������������������������������������������������������������������������� 195
Installing Java MVC������������������������������������������������������������������������������������������������������������������� 196
Configuration Files�������������������������������������������������������������������������������������������������������������������� 198
Static Files�������������������������������������������������������������������������������������������������������������������������������� 203
Model Classes��������������������������������������������������������������������������������������������������������������������������� 203
Controller Classes��������������������������������������������������������������������������������������������������������������������� 206
Messages���������������������������������������������������������������������������������������������������������������������������������� 212
View Pages������������������������������������������������������������������������������������������������������������������������������� 214
Running the Pet Shop Application��������������������������������������������������������������������������������������������� 223

Part III: Advanced Architecture Related Topics������������������������������������������ 225


Chapter 16: Microprofiles������������������������������������������������������������������������������������� 227
Starting a MicroProfile Sample Project������������������������������������������������������������������������������������� 229
Installing a MicroProfile Server������������������������������������������������������������������������������������������������� 231
Changing the Application to a Microservice����������������������������������������������������������������������������� 231
Deploying and Running the Microservice��������������������������������������������������������������������������������� 234

Chapter 17: Custom CDI���������������������������������������������������������������������������������������� 241


CDI Specification����������������������������������������������������������������������������������������������������������������������� 241
Building Object Graphs������������������������������������������������������������������������������������������������������������� 242
Qualifiers����������������������������������������������������������������������������������������������������������������������������������� 246

ix
Table of Contents

Alternatives������������������������������������������������������������������������������������������������������������������������������� 249
Producers���������������������������������������������������������������������������������������������������������������������������������� 251
Scope���������������������������������������������������������������������������������������������������������������������������������������� 253
Interceptors������������������������������������������������������������������������������������������������������������������������������� 255
Decorators�������������������������������������������������������������������������������������������������������������������������������� 255

Chapter 18: Interceptors�������������������������������������������������������������������������������������� 257


CDI Interceptors������������������������������������������������������������������������������������������������������������������������ 257
JPA Lifecycle Listeners������������������������������������������������������������������������������������������������������������� 261
Servlet Listeners����������������������������������������������������������������������������������������������������������������������� 263
Faces Phase Listeners�������������������������������������������������������������������������������������������������������������� 264

Chapter 19: Bean Validation��������������������������������������������������������������������������������� 267


Where to Use Bean Validation��������������������������������������������������������������������������������������������������� 268
How to Add Constraints������������������������������������������������������������������������������������������������������������� 269
Built-in Constraints������������������������������������������������������������������������������������������������������������������� 269
Custom Constraints������������������������������������������������������������������������������������������������������������������� 272
Bean Validation Exceptions������������������������������������������������������������������������������������������������������� 274

Chapter 20: Jakarta EE Concurrency�������������������������������������������������������������������� 277


Preparing the Jakarta EE Server����������������������������������������������������������������������������������������������� 277
Using a ManagedExecutorService�������������������������������������������������������������������������������������������� 278
Using a ManagedScheduledExecutorService���������������������������������������������������������������������������� 280
Using the ManagedThreadFactory�������������������������������������������������������������������������������������������� 283
Enterprise Concurrency and Transactions��������������������������������������������������������������������������������� 284

Chapter 21: Batch Processing������������������������������������������������������������������������������ 285


Batch Processing Concepts������������������������������������������������������������������������������������������������������ 285
Preparing the Server for Batch Processing������������������������������������������������������������������������������� 286
An Employee Attendance Example Batch Processing��������������������������������������������������������������� 287
Starting a Batch Processing EAR Project���������������������������������������������������������������������������������� 288
Example Data Preparation�������������������������������������������������������������������������������������������������������� 293
Job Definition File��������������������������������������������������������������������������������������������������������������������� 293

x
Visit https://ebookmass.com today to explore
a vast collection of ebooks across various
genres, available in popular formats like
PDF, EPUB, and MOBI, fully compatible with
all devices. Enjoy a seamless reading
experience and effortlessly download high-
quality materials in just a few simple steps.
Plus, don’t miss out on exciting offers that
let you access a wealth of knowledge at the
best prices!
Table of Contents

Batch Processing Scheduling��������������������������������������������������������������������������������������������������� 295


Batch Processing Java Artifacts����������������������������������������������������������������������������������������������� 296
Building and Deploying the Application������������������������������������������������������������������������������������ 303

Part IV: Useful Supporting Technologies����������������������������������������������������� 305


Chapter 22: XML Binding�������������������������������������������������������������������������������������� 307
Why JAXB Is So Complicated���������������������������������������������������������������������������������������������������� 307
Writing a Java Object Tree as XML�������������������������������������������������������������������������������������������� 309
Adding a Schema���������������������������������������������������������������������������������������������������������������������� 313
Transforming from XML to Java������������������������������������������������������������������������������������������������ 316
Generating Java Classes from Schemas����������������������������������������������������������������������������������� 317

Chapter 23: JSON Handling���������������������������������������������������������������������������������� 319


JSON Documents���������������������������������������������������������������������������������������������������������������������� 319
JSON with REST Services��������������������������������������������������������������������������������������������������������� 320
Generating JSON����������������������������������������������������������������������������������������������������������������������� 323
Parsing JSON���������������������������������������������������������������������������������������������������������������������������� 324
Binding JSON to Java Objects��������������������������������������������������������������������������������������������������� 325

Chapter 24: Jakarta Mail�������������������������������������������������������������������������������������� 331


Installing Jakarta Mail��������������������������������������������������������������������������������������������������������������� 331
Generating and Sending Emails������������������������������������������������������������������������������������������������ 331

Chapter 25: Application Client (Groovy)��������������������������������������������������������������� 335


Providing an Enterprise Application������������������������������������������������������������������������������������������ 335
Building an Application Client��������������������������������������������������������������������������������������������������� 337
Running a GroovyConsole Application Client���������������������������������������������������������������������������� 338

Chapter 26: Adding Scripting Languages������������������������������������������������������������� 341


Installing Scripting Engines������������������������������������������������������������������������������������������������������ 341
Using Scripting in Java������������������������������������������������������������������������������������������������������������� 342

xi
Table of Contents

Part V: Advanced Resource Related Topics������������������������������������������������ 345


Chapter 27: Hibernate as ORM����������������������������������������������������������������������������� 347
Installing Hibernate������������������������������������������������������������������������������������������������������������������� 347
Adapting the Persistence Configuration������������������������������������������������������������������������������������ 348
Fetching the Hibernate Session������������������������������������������������������������������������������������������������ 349

Chapter 28: Connectors���������������������������������������������������������������������������������������� 351


Coding a Resource Adapter������������������������������������������������������������������������������������������������������� 351
Packaging and Deploying a Resource Adapter������������������������������������������������������������������������� 366
Deployment Descriptors������������������������������������������������������������������������������������������������������������ 369
Resource Adapter Deployment�������������������������������������������������������������������������������������������������� 369
Defining a Resource Adapter on the Server������������������������������������������������������������������������������ 370
Resource Adapter Clients���������������������������������������������������������������������������������������������������������� 371

Chapter 29: Caching��������������������������������������������������������������������������������������������� 373


Installing Ehcache��������������������������������������������������������������������������������������������������������������������� 373
Configuring Hibernate for Ehcache������������������������������������������������������������������������������������������� 374
Defining Cached Objects in the Code���������������������������������������������������������������������������������������� 377
Monitoring Caching Activities��������������������������������������������������������������������������������������������������� 378

Chapter 30: NoSQL������������������������������������������������������������������������������������������������ 381


Using CouchDB from Jakarta EE Applications��������������������������������������������������������������������������� 381
Using MongoDB from Jakarta EE Applications�������������������������������������������������������������������������� 385

Part VI: Security Enhancements����������������������������������������������������������������� 389


Chapter 31: Secured JMX������������������������������������������������������������������������������������� 391
Using SSL for Remote JMX Connections���������������������������������������������������������������������������������� 391
Generating the SSL Keys����������������������������������������������������������������������������������������������������������� 391
Configuring the Server�������������������������������������������������������������������������������������������������������������� 395
Configuring the Client��������������������������������������������������������������������������������������������������������������� 395
Disabling Random JMX Ports���������������������������������������������������������������������������������������������������� 397

xii
Table of Contents

Chapter 32: Java Web Tokens with Encryption���������������������������������������������������� 399


Installing Jose4j������������������������������������������������������������������������������������������������������������������������ 400
Encrypting Claims��������������������������������������������������������������������������������������������������������������������� 400
Decrypting Claims��������������������������������������������������������������������������������������������������������������������� 403
Further Reading������������������������������������������������������������������������������������������������������������������������ 405

Chapter 33: Java Enterprise Security������������������������������������������������������������������� 407


Form-Based Authentication������������������������������������������������������������������������������������������������������ 407
The Security API������������������������������������������������������������������������������������������������������������������������ 408
Authentication Data: IdentityStore�������������������������������������������������������������������������������������������� 408
Authentication Methods: HttpAuthenticationMechanism���������������������������������������������������������� 410

Part VII: Advanced Monitoring and Logging����������������������������������������������� 413


Chapter 34: Monitoring Workflow������������������������������������������������������������������������ 415
Using JMX as a Monitoring Technology������������������������������������������������������������������������������������ 415
Enabling Remote JMX��������������������������������������������������������������������������������������������������������������� 417
MBeans in Jakarta EE Applications������������������������������������������������������������������������������������������� 418
Aggregating Values������������������������������������������������������������������������������������������������������������������� 420
JMX Clients������������������������������������������������������������������������������������������������������������������������������� 423
Monitoring Frameworks������������������������������������������������������������������������������������������������������������ 425

Chapter 35: Logging Pipeline with Fluentd����������������������������������������������������������� 427


Installing Fluentd���������������������������������������������������������������������������������������������������������������������� 428
Running Fluentd������������������������������������������������������������������������������������������������������������������������ 429
Using Logfiles as Input�������������������������������������������������������������������������������������������������������������� 430
Filtering������������������������������������������������������������������������������������������������������������������������������������� 432
Using Multiple Routes��������������������������������������������������������������������������������������������������������������� 433
Fluentd Output�������������������������������������������������������������������������������������������������������������������������� 435
Further Reading������������������������������������������������������������������������������������������������������������������������ 436

xiii
Table of Contents

Chapter 36: Performance Troubleshooting����������������������������������������������������������� 437


Load and Performance Tests����������������������������������������������������������������������������������������������������� 437
NFR Testing Methodology���������������������������������������������������������������������������������������������������� 438
Where to Run NFR Tests������������������������������������������������������������������������������������������������������ 440
NFR Test Duration���������������������������������������������������������������������������������������������������������������� 441
NFR Tests with JMeter��������������������������������������������������������������������������������������������������������� 441
Frontend Tests with Selenium��������������������������������������������������������������������������������������������� 445
Analyzing Performance Figures������������������������������������������������������������������������������������������������ 446
Reducing the File Size��������������������������������������������������������������������������������������������������������� 446
Plotting a Performance Chart���������������������������������������������������������������������������������������������� 447
No Min or Max, Please��������������������������������������������������������������������������������������������������������� 449
Code-Level Monitoring with VisualVM�������������������������������������������������������������������������������������� 450
Code Optimization��������������������������������������������������������������������������������������������������������������������� 452

Chapter 37: Garbage Collection���������������������������������������������������������������������������� 455


The Importance of Garbage Collectors�������������������������������������������������������������������������������������� 455
G1 Garbage Collector���������������������������������������������������������������������������������������������������������������� 456
Shenandoah GC������������������������������������������������������������������������������������������������������������������������� 456
Zero Garbage Collector������������������������������������������������������������������������������������������������������������� 456
Garbage Collector Logs������������������������������������������������������������������������������������������������������������� 457

Chapter 38: Memory Troubleshooting������������������������������������������������������������������ 459


Identifying Memory Leaks��������������������������������������������������������������������������������������������������������� 459
More Evidence: Heap Dumps���������������������������������������������������������������������������������������������������� 463
Analyzing Heap Dumps������������������������������������������������������������������������������������������������������������� 466

Chapter 39: Custom Log4j Appender�������������������������������������������������������������������� 473


Including Log4j�������������������������������������������������������������������������������������������������������������������������� 475
A Statistics Appender���������������������������������������������������������������������������������������������������������������� 475

Index��������������������������������������������������������������������������������������������������������������������� 481

xiv
About the Author
Peter Späth graduated in 2002 as a physicist and became an IT consultant, mainly for
Java-related projects. In 2016, he decided to concentrate on writing books on various
technology-related topics, with his main focus on software development. With two
books about graphics and sound processing, two books about Android app
development, and several books about Java, Späth continues his efforts in writing
software development-related literature.

xv
About the Technical Reviewer
Massimo Nardone has more than 25 years of experience
in security, web/mobile development, cloud, and IT
architecture. His true IT passions are security and Android.
He has been programming and teaching others how to
program with Android, Perl, PHP, Java, VB, Python, C/C++,
and MySQL for more than 20 years. He holds a Master of
Science degree in computing science from the University
of Salerno, Italy. He has worked as a CISO, CSO, security
executive, IoT executive, project manager, software engineer,
research engineer, chief security architect, PCI/SCADA
auditor, and senior lead IT security/cloud/SCADA architect for many years. His technical
skills include security, Android, cloud, Java, MySQL, Drupal, Cobol, Perl, web and
mobile development, MongoDB, D3, Joomla, Couchbase, C/C++, WebGL, Python, Pro
Rails, Django CMS, Jekyll, Scratch, and more. He was a visiting lecturer and supervisor
for exercises at the Networking Laboratory of the Helsinki University of Technology
(Aalto University). He also holds four international patents (in the PKI, SIP, SAML,
and Proxy areas). He is currently working for Cognizant as the head of cybersecurity
and CISO to help clients in areas of information and cybersecurity, including strategy,
planning, processes, policies, procedures, governance, awareness, and so forth. In
June, 2017, he became a permanent member of the ISACA Finland Board. Massimo has
reviewed more than 45 IT books for different publishing companies and is the co-author
of Pro Spring Security: Securing Spring Framework 5 and Boot 2-based Java Applications
(Apress, 2019), Beginning EJB in Java EE 8 (Apress, 2018), Pro JPA 2 in Java EE 8 (Apress,
2018), and Pro Android Games (Apress, 2015).

xvii
Introduction
Java is not just a programming language, it’s also a platform used to host software. As
far as enterprise environments go, Java Enterprise Edition Jakarta EE (formerly JEE) has
an extensive collection of APIs that are particularly useful for addressing corporate IT
requirements.
This book covers advanced topics of Jakarta EE development. This includes pro-­
level web tier development, architecture-related matters, advanced XML and JSON
processing, application clients and scripting languages, resource handling, high-level
security enhancements, and advanced monitoring and logging techniques.
The target version of Jakarta EE is version 10. Unless otherwise noted, server scripts
have been tested on Ubuntu 22.04. Switching to Debian, Fedora, or OpenSUSE Leap
should not impose any problems.
The book is for advanced enterprise software developers with knowledge of
Java Standard Edition version 8 or later and some experience in Jakarta EE (or JEE)
development. Having read the book titled Beginning Jakarta EE from the same author
and publisher (ISBN: 978-1-4842-5078-5) will surely help, but it’s not a strict prerequisite.
I keep references to the beginner book at a bare minimum. I also assume that you can
use the online API references, so this book is not a complete reference in the sense that
not all API classes and methods are listed. Instead, the book includes techniques and
technologies that will help professional Java enterprise-level developers deal with topics
and master problems that arise in a corporate environment.
The book uses the Linux operating system as its development platform, although
the code can run on other platforms without requiring complex changes. The server
installations—like version control, continuous integration systems, and operating
instructions—all target the Linux operating system. This book also does not cover
hardware issues, except for some cases where hardware performance has a noticeable
impact on the software.
After finishing this book, you will be able to develop and run Jakarta EE 10 programs
of mid- to high-level complexity.

xix
Introduction

How to Read This Book


You can read this book sequentially from the beginning to the end, or you can
read chapters on an ad hoc basis if your work demands special attention to one or
another topic.

Source Code
All the source code in this book can be found at github.com/Apress/pro-jakarta-ee10.

xx
PART I

Development Workflow
Proposal
CHAPTER 1

Installing a Development
Server
This book uses GlassFish version 7.0.1 as a Jakarta EE server, although I try to avoid
vendor lock-in, so that, unless otherwise noted, you can test all the examples on different
Jakarta EE servers.
For the Eclipse IDE, there is a GlassFish plugin called GlassFish Tools, which you can
use if you want to. I don’t use it in this book for several reasons. First of all, the plugin
can have issues with your Eclipse installation. Second, if you don’t use the plugin and
instead use a terminal to start and stop the server and then use a build tool like Gradle to
install and uninstall Enterprise applications, you are already close to what you need for
integration testing and production setups. Third, it is easier to switch between different
Jakarta EE servers and different IDEs. Fourth, you don’t have to learn how to use that
plugin, including any peculiarities that might arise.
So for now, simply download and install the GlassFish server, version 7.0.1, from this
location:

  https://glassfish.org/download.html

Select the Full Platform variant.

Note GlassFish 7.0.1 runs on JDK 11 through 19. You can try higher JDK
versions for GlassFish 7.0.1, but they might not work.

3
© Peter Späth 2023
P. Späth, Pro Jakarta EE 10, https://doi.org/10.1007/978-1-4842-8214-4_1
Visit https://ebookmass.com today to explore
a vast collection of ebooks across various
genres, available in popular formats like
PDF, EPUB, and MOBI, fully compatible with
all devices. Enjoy a seamless reading
experience and effortlessly download high-
quality materials in just a few simple steps.
Plus, don’t miss out on exciting offers that
let you access a wealth of knowledge at the
best prices!
Exploring the Variety of Random
Documents with Different Content
This eBook is for the use of anyone anywhere in the United
States and most other parts of the world at no cost and with
almost no restrictions whatsoever. You may copy it, give it away
or re-use it under the terms of the Project Gutenberg License
included with this eBook or online at www.gutenberg.org. If you
are not located in the United States, you will have to check the
laws of the country where you are located before using this
eBook.

1.E.2. If an individual Project Gutenberg™ electronic work is derived


from texts not protected by U.S. copyright law (does not contain a
notice indicating that it is posted with permission of the copyright
holder), the work can be copied and distributed to anyone in the
United States without paying any fees or charges. If you are
redistributing or providing access to a work with the phrase “Project
Gutenberg” associated with or appearing on the work, you must
comply either with the requirements of paragraphs 1.E.1 through
1.E.7 or obtain permission for the use of the work and the Project
Gutenberg™ trademark as set forth in paragraphs 1.E.8 or 1.E.9.

1.E.3. If an individual Project Gutenberg™ electronic work is posted


with the permission of the copyright holder, your use and distribution
must comply with both paragraphs 1.E.1 through 1.E.7 and any
additional terms imposed by the copyright holder. Additional terms
will be linked to the Project Gutenberg™ License for all works posted
with the permission of the copyright holder found at the beginning
of this work.

1.E.4. Do not unlink or detach or remove the full Project


Gutenberg™ License terms from this work, or any files containing a
part of this work or any other work associated with Project
Gutenberg™.

1.E.5. Do not copy, display, perform, distribute or redistribute this


electronic work, or any part of this electronic work, without
prominently displaying the sentence set forth in paragraph 1.E.1
with active links or immediate access to the full terms of the Project
Gutenberg™ License.

1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.

1.E.7. Do not charge a fee for access to, viewing, displaying,


performing, copying or distributing any Project Gutenberg™ works
unless you comply with paragraph 1.E.8 or 1.E.9.

1.E.8. You may charge a reasonable fee for copies of or providing


access to or distributing Project Gutenberg™ electronic works
provided that:

• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”

• You provide a full refund of any money paid by a user who


notifies you in writing (or by e-mail) within 30 days of receipt
that s/he does not agree to the terms of the full Project
Gutenberg™ License. You must require such a user to return or
destroy all copies of the works possessed in a physical medium
and discontinue all use of and all access to other copies of
Project Gutenberg™ works.

• You provide, in accordance with paragraph 1.F.3, a full refund of


any money paid for a work or a replacement copy, if a defect in
the electronic work is discovered and reported to you within 90
days of receipt of the work.

• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.

1.E.9. If you wish to charge a fee or distribute a Project Gutenberg™


electronic work or group of works on different terms than are set
forth in this agreement, you must obtain permission in writing from
the Project Gutenberg Literary Archive Foundation, the manager of
the Project Gutenberg™ trademark. Contact the Foundation as set
forth in Section 3 below.

1.F.

1.F.1. Project Gutenberg volunteers and employees expend


considerable effort to identify, do copyright research on, transcribe
and proofread works not protected by U.S. copyright law in creating
the Project Gutenberg™ collection. Despite these efforts, Project
Gutenberg™ electronic works, and the medium on which they may
be stored, may contain “Defects,” such as, but not limited to,
incomplete, inaccurate or corrupt data, transcription errors, a
copyright or other intellectual property infringement, a defective or
damaged disk or other medium, a computer virus, or computer
codes that damage or cannot be read by your equipment.

1.F.2. LIMITED WARRANTY, DISCLAIMER OF DAMAGES - Except for


the “Right of Replacement or Refund” described in paragraph 1.F.3,
the Project Gutenberg Literary Archive Foundation, the owner of the
Project Gutenberg™ trademark, and any other party distributing a
Project Gutenberg™ electronic work under this agreement, disclaim
all liability to you for damages, costs and expenses, including legal
fees. YOU AGREE THAT YOU HAVE NO REMEDIES FOR
NEGLIGENCE, STRICT LIABILITY, BREACH OF WARRANTY OR
BREACH OF CONTRACT EXCEPT THOSE PROVIDED IN PARAGRAPH
1.F.3. YOU AGREE THAT THE FOUNDATION, THE TRADEMARK
OWNER, AND ANY DISTRIBUTOR UNDER THIS AGREEMENT WILL
NOT BE LIABLE TO YOU FOR ACTUAL, DIRECT, INDIRECT,
CONSEQUENTIAL, PUNITIVE OR INCIDENTAL DAMAGES EVEN IF
YOU GIVE NOTICE OF THE POSSIBILITY OF SUCH DAMAGE.

1.F.3. LIMITED RIGHT OF REPLACEMENT OR REFUND - If you


discover a defect in this electronic work within 90 days of receiving
it, you can receive a refund of the money (if any) you paid for it by
sending a written explanation to the person you received the work
from. If you received the work on a physical medium, you must
return the medium with your written explanation. The person or
entity that provided you with the defective work may elect to provide
a replacement copy in lieu of a refund. If you received the work
electronically, the person or entity providing it to you may choose to
give you a second opportunity to receive the work electronically in
lieu of a refund. If the second copy is also defective, you may
demand a refund in writing without further opportunities to fix the
problem.

1.F.4. Except for the limited right of replacement or refund set forth
in paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.

1.F.5. Some states do not allow disclaimers of certain implied


warranties or the exclusion or limitation of certain types of damages.
If any disclaimer or limitation set forth in this agreement violates the
law of the state applicable to this agreement, the agreement shall be
interpreted to make the maximum disclaimer or limitation permitted
by the applicable state law. The invalidity or unenforceability of any
provision of this agreement shall not void the remaining provisions.

1.F.6. INDEMNITY - You agree to indemnify and hold the Foundation,


the trademark owner, any agent or employee of the Foundation,
anyone providing copies of Project Gutenberg™ electronic works in
accordance with this agreement, and any volunteers associated with
the production, promotion and distribution of Project Gutenberg™
electronic works, harmless from all liability, costs and expenses,
including legal fees, that arise directly or indirectly from any of the
following which you do or cause to occur: (a) distribution of this or
any Project Gutenberg™ work, (b) alteration, modification, or
additions or deletions to any Project Gutenberg™ work, and (c) any
Defect you cause.

Section 2. Information about the Mission


of Project Gutenberg™
Project Gutenberg™ is synonymous with the free distribution of
electronic works in formats readable by the widest variety of
computers including obsolete, old, middle-aged and new computers.
It exists because of the efforts of hundreds of volunteers and
donations from people in all walks of life.

Volunteers and financial support to provide volunteers with the


assistance they need are critical to reaching Project Gutenberg™’s
goals and ensuring that the Project Gutenberg™ collection will
remain freely available for generations to come. In 2001, the Project
Gutenberg Literary Archive Foundation was created to provide a
secure and permanent future for Project Gutenberg™ and future
generations. To learn more about the Project Gutenberg Literary
Archive Foundation and how your efforts and donations can help,
see Sections 3 and 4 and the Foundation information page at
www.gutenberg.org.

Section 3. Information about the Project


Gutenberg Literary Archive Foundation
The Project Gutenberg Literary Archive Foundation is a non-profit
501(c)(3) educational corporation organized under the laws of the
state of Mississippi and granted tax exempt status by the Internal
Revenue Service. The Foundation’s EIN or federal tax identification
number is 64-6221541. Contributions to the Project Gutenberg
Literary Archive Foundation are tax deductible to the full extent
permitted by U.S. federal laws and your state’s laws.

The Foundation’s business office is located at 809 North 1500 West,


Salt Lake City, UT 84116, (801) 596-1887. Email contact links and up
to date contact information can be found at the Foundation’s website
and official page at www.gutenberg.org/contact

Section 4. Information about Donations to


the Project Gutenberg Literary Archive
Foundation
Project Gutenberg™ depends upon and cannot survive without
widespread public support and donations to carry out its mission of
increasing the number of public domain and licensed works that can
be freely distributed in machine-readable form accessible by the
widest array of equipment including outdated equipment. Many
small donations ($1 to $5,000) are particularly important to
maintaining tax exempt status with the IRS.

The Foundation is committed to complying with the laws regulating


charities and charitable donations in all 50 states of the United
States. Compliance requirements are not uniform and it takes a
considerable effort, much paperwork and many fees to meet and
keep up with these requirements. We do not solicit donations in
locations where we have not received written confirmation of
compliance. To SEND DONATIONS or determine the status of
compliance for any particular state visit www.gutenberg.org/donate.

While we cannot and do not solicit contributions from states where


we have not met the solicitation requirements, we know of no
prohibition against accepting unsolicited donations from donors in
such states who approach us with offers to donate.

International donations are gratefully accepted, but we cannot make


any statements concerning tax treatment of donations received from
outside the United States. U.S. laws alone swamp our small staff.

Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.

Section 5. General Information About


Project Gutenberg™ electronic works
Professor Michael S. Hart was the originator of the Project
Gutenberg™ concept of a library of electronic works that could be
freely shared with anyone. For forty years, he produced and
distributed Project Gutenberg™ eBooks with only a loose network of
volunteer support.
Project Gutenberg™ eBooks are often created from several printed
editions, all of which are confirmed as not protected by copyright in
the U.S. unless a copyright notice is included. Thus, we do not
necessarily keep eBooks in compliance with any particular paper
edition.

Most people start at our website which has the main PG search
facility: www.gutenberg.org.

This website includes information about Project Gutenberg™,


including how to make donations to the Project Gutenberg Literary
Archive Foundation, how to help produce our new eBooks, and how
to subscribe to our email newsletter to hear about new eBooks.
Welcome to our website – the perfect destination for book lovers and
knowledge seekers. We believe that every book holds a new world,
offering opportunities for learning, discovery, and personal growth.
That’s why we are dedicated to bringing you a diverse collection of
books, ranging from classic literature and specialized publications to
self-development guides and children's books.

More than just a book-buying platform, we strive to be a bridge


connecting you with timeless cultural and intellectual values. With an
elegant, user-friendly interface and a smart search system, you can
quickly find the books that best suit your interests. Additionally,
our special promotions and home delivery services help you save time
and fully enjoy the joy of reading.

Join us on a journey of knowledge exploration, passion nurturing, and


personal growth every day!

ebookmasss.com

You might also like