SlideShare uma empresa Scribd logo
1 de 20
Save 42% off The Java Module
System by Nicolai Parlog with code
slparlog at manning.com
Connecting the pieces
Modules are the atomic building blocks, the nodes in
our graph of interacting artifacts. But there can be no
graph without edges connecting the nodes! So the
modules express which other modules they depend on,
creating edges between them.
The next slide shows a graphic of the types of JVM
modules:
Need some background info on Java 9 modules? Check
out the author’s website:
http://blog.codefx.org/tag/jpms/
Observable
Unnamed
Named
Platform
Application
Automatic
Base
Initial
Types of modules
Let’s imagine that we have a module customer and a
module bar.
When a module customer requires module bar in its
declaration, then at run time customer will read bar or,
conversely, bar will be readable by customer.
barcustomer
1. requires bar
2. reads
With that in mind, you can see how phrases like
"customer requires bar" and "customer depend on bar“
mirror a static, compile-time relationship between
customer and bar; readability is the more dynamic,
run-time counterpart.
You might be asking: Why is it more dynamic?
While the requires clause is the primal originator of
readability edges, it is by no means the only one – both
command line arguments and the reflection API can be
used to add more.
However they came to be, readability edges are the basis
for reliable configuration and strong encapsulation.
Achieving reliable configuration
Reliable configuration aims to ensure that the particular
configuration of artifacts a Java program is compiled
against, or launched with, can sustain the program
without spurious run-time errors.
To this end it performs a couple of checks – which
happen during module resolution.
The next slide shows the verification sequence.
1. First and foremost, the module system checks whether the universe of
observable modules contains all required dependencies (both direct and
transitive) and reports an error if something is missing.
2. There must be no ambiguity, which means that no two artifacts can claim
that they are the same module. This is particularly interesting in the case
where two versions of the same module are present—unfortunately the
module system has no concept of versions and thus treats this as a
duplicate module. Accordingly, you get an error if it runs into this
situation.
3. There must be no static dependency cycles between modules. At run time it
is possible and even necessary for modules to access each other, but these
must not be compile dependencies.
4. Packages should have a unique origin so no two modules contain types in
the same package. If they do, this is called a split package and the module
system will refuse to compile or launch such configurations.
This verification is, of course, not airtight and it is still
possible for problems to hide long enough to crash a
running application. If, for example, the wrong version
of a module ends up in the right place, then the
application will still launch (all required modules are
present after all) but crash later when, for example, a
class or method is missing.
The module system is developed to exhibit consistent
behavior across compile and run time, though, so these
errors can be further minimized if it can be guaranteed
that compilation and launch are based on the same
artifacts (for example, a compilation against a module
with the wrong version would fail).
Unreliable configurations
Now that you know how the module system is supposed
to work (and when it won’t), let’s see if we can break
things – so you know what to do when things go wrong
(they will).
Imagine that we have a simple monitor application, and
consider monitor.observer.alpha and its declaration:
module monitor.observer.alpha {
requires monitor.observer;
exports monitor.observer.alpha;
}
Missing dependencies
Now let’s see what it would look like to try to compile it
with monitor.observer missing:
Missing dependency, no compilation!
monitor.observer.alpha/src/main/java/module-info:2:
error: module not found: monitor.observer
requires monitor.observer
^
1 error
A different error would get thrown if the module was
present at compile time, but got lost on the way to the
launch pad. In this instance, the JVM will quit with the
following:
Missing dependencies
Error occurred during initialization of VM
java.lang.module.ResolutionException:
Module monitor.observer not found,
required by monitor.observer.alpha
Duplicate modules
Since modules reference one another by name, any
situation where two modules claim to have the same
name is ambiguous. Which one is the correct one to
pick is highly dependent on the context and not
something the module system can generally decide.
So, instead of making a potentially bad decision, it
makes none at all and instead produces an error.
This commitment to failing fast allows the developer to
notice a problem and fix it before it causes any more
problems.
Duplicate modules
This is the compile error the module system produces
when trying to compile a module with two variants of
monitor.observer.beta on the module path:
The compiler cannot link the error to one of the files
under compilation because they are not the reason for
the problem. In this case, the artifacts on the module
path are causing the error.
error: duplicate module on application module path
module in monitor.observer.beta
1 error
Duplicate modules
In the event of an error going undetected until the JVM
is launched, we get a more precise message that lists
the JAR file names as well:
As we mentioned before, the module system has no
concept of versions – many errors can be traced to
having several versions of the same module on the
module path.
Error occurred during initialization of VM
java.lang.module.ResolutionException:
Two versions of module monitor.observer.beta found in mods
(monitor.observer.beta.jar and monitor.observer.gamma.jar)
Dependency cycles
Getting dependency cycles through the compiler takes
some doing, and, if not done properly, will result in a
compile error.
This is how it looks if monitor.persistence and
monitor.statistics depend on each other:
monitor.statistics/src/main/java/module-info:3:
error: cyclic dependence involving monitor.persistence
requires monitor.persistence;
^
1 error
Split packages
A split package occurs when two modules contain types
in the same package. For example, the monitor.statistics
module contains a class Statistician in the
monitor.statistics package.
Now let’s assume the monitor module contained a very
simple fallback implementation SimpleStatistician,
and to promote uniformity, the monitor module has
placed it in its own monitor.statistics package.
The error we would get at compile time is on the next
slide:
Split packages
Interestingly enough, the compiler only shows an error if
the module under compilation can access the split
package in the other module, which means the split
package must be exported.
monitor/src/main/java/monitor/statistics/SimpleStatistician.java:1:
error: package exists in another module: monitor.statistics
package monitor.statistics;
^
1 error
Split packages
Let’s see this another way: SimpleStatistician is
gone and this time monitor.statistics creates the split
module. It gets a Utils class in the monitor package,
and continues to only export the monitor.statistics
package. Compiling monitor.statistics is error-free, which
makes sense because it does not require monitor.
Compiling monitor is more interesting because it
depends on monitor.statistics and both contain types in
the package monitor. But because monitor.statistics
does not export the split package, compilation is
successful.
Split packages
That didn’t go so well… Indeed, the module system
checks for split packages on launch and here it doesn’t
matter whether they are exported or not: There can be
no two modules that contain types in the same package.
Error occurred during initialization of boot layer
java.lang.reflect.LayerInstantiationException:
Package monitor in both module monitor.statistics and module monitor
Let’s see what happens when we launch:
Grab your copy of The Java Module System and save
42% with code slparlog at manning.com.
Also see:

Mais conteúdo relacionado

Mais de Manning Publications

Crafting interactive troubleshooting guides and team documentation for your K...
Crafting interactive troubleshooting guides and team documentation for your K...Crafting interactive troubleshooting guides and team documentation for your K...
Crafting interactive troubleshooting guides and team documentation for your K...Manning Publications
 
Entity Framework Core in Action, Second Edtion
Entity Framework Core in Action, Second EdtionEntity Framework Core in Action, Second Edtion
Entity Framework Core in Action, Second EdtionManning Publications
 
Microservices in .NET Core, Second Edition
Microservices in .NET Core, Second EditionMicroservices in .NET Core, Second Edition
Microservices in .NET Core, Second EditionManning Publications
 
Kubernetes in Action, Second Edition
Kubernetes in Action, Second EditionKubernetes in Action, Second Edition
Kubernetes in Action, Second EditionManning Publications
 
Machine Learning with TensorFlow, Second Edition
Machine Learning with TensorFlow, Second EditionMachine Learning with TensorFlow, Second Edition
Machine Learning with TensorFlow, Second EditionManning Publications
 
Spring Microservices in Action, Second Edition
Spring Microservices in Action, Second EditionSpring Microservices in Action, Second Edition
Spring Microservices in Action, Second EditionManning Publications
 
Grokking Artificial Intelligence Algorithms
Grokking Artificial Intelligence AlgorithmsGrokking Artificial Intelligence Algorithms
Grokking Artificial Intelligence AlgorithmsManning Publications
 
GitOps and Kubernetes: a radical idea
GitOps and Kubernetes: a radical ideaGitOps and Kubernetes: a radical idea
GitOps and Kubernetes: a radical ideaManning Publications
 
Data Pipelines with Apache Airflow
Data Pipelines with Apache AirflowData Pipelines with Apache Airflow
Data Pipelines with Apache AirflowManning Publications
 
Learn PowerShell in a Month of Lunches: Linux and macOS Edition
Learn PowerShell in a Month of Lunches: Linux and macOS EditionLearn PowerShell in a Month of Lunches: Linux and macOS Edition
Learn PowerShell in a Month of Lunches: Linux and macOS EditionManning Publications
 
Hugo in Action: website creation made painless
Hugo in Action: website creation made painlessHugo in Action: website creation made painless
Hugo in Action: website creation made painlessManning Publications
 
Learn dbatools in a Month of Lunches
Learn dbatools in a Month of LunchesLearn dbatools in a Month of Lunches
Learn dbatools in a Month of LunchesManning Publications
 
Designing APIs with Swagger and OpenAPI
Designing APIs with Swagger and OpenAPIDesigning APIs with Swagger and OpenAPI
Designing APIs with Swagger and OpenAPIManning Publications
 
PostGIS in Action, Third Edition: the newly-updated guide
PostGIS in Action, Third Edition: the newly-updated guidePostGIS in Action, Third Edition: the newly-updated guide
PostGIS in Action, Third Edition: the newly-updated guideManning Publications
 

Mais de Manning Publications (20)

Cloud Native Machine Learning
Cloud Native Machine Learning Cloud Native Machine Learning
Cloud Native Machine Learning
 
Spring in Action, Sixth Edition
Spring in Action, Sixth EditionSpring in Action, Sixth Edition
Spring in Action, Sixth Edition
 
Crafting interactive troubleshooting guides and team documentation for your K...
Crafting interactive troubleshooting guides and team documentation for your K...Crafting interactive troubleshooting guides and team documentation for your K...
Crafting interactive troubleshooting guides and team documentation for your K...
 
Entity Framework Core in Action, Second Edtion
Entity Framework Core in Action, Second EdtionEntity Framework Core in Action, Second Edtion
Entity Framework Core in Action, Second Edtion
 
Code like a Pro in C#
Code like a Pro in C#Code like a Pro in C#
Code like a Pro in C#
 
Microservices in .NET Core, Second Edition
Microservices in .NET Core, Second EditionMicroservices in .NET Core, Second Edition
Microservices in .NET Core, Second Edition
 
Kubernetes in Action, Second Edition
Kubernetes in Action, Second EditionKubernetes in Action, Second Edition
Kubernetes in Action, Second Edition
 
Core Kubernetes
Core KubernetesCore Kubernetes
Core Kubernetes
 
Machine Learning Bookcamp
Machine Learning BookcampMachine Learning Bookcamp
Machine Learning Bookcamp
 
Machine Learning with TensorFlow, Second Edition
Machine Learning with TensorFlow, Second EditionMachine Learning with TensorFlow, Second Edition
Machine Learning with TensorFlow, Second Edition
 
Spring Security in Action
Spring Security in ActionSpring Security in Action
Spring Security in Action
 
Spring Microservices in Action, Second Edition
Spring Microservices in Action, Second EditionSpring Microservices in Action, Second Edition
Spring Microservices in Action, Second Edition
 
Grokking Artificial Intelligence Algorithms
Grokking Artificial Intelligence AlgorithmsGrokking Artificial Intelligence Algorithms
Grokking Artificial Intelligence Algorithms
 
GitOps and Kubernetes: a radical idea
GitOps and Kubernetes: a radical ideaGitOps and Kubernetes: a radical idea
GitOps and Kubernetes: a radical idea
 
Data Pipelines with Apache Airflow
Data Pipelines with Apache AirflowData Pipelines with Apache Airflow
Data Pipelines with Apache Airflow
 
Learn PowerShell in a Month of Lunches: Linux and macOS Edition
Learn PowerShell in a Month of Lunches: Linux and macOS EditionLearn PowerShell in a Month of Lunches: Linux and macOS Edition
Learn PowerShell in a Month of Lunches: Linux and macOS Edition
 
Hugo in Action: website creation made painless
Hugo in Action: website creation made painlessHugo in Action: website creation made painless
Hugo in Action: website creation made painless
 
Learn dbatools in a Month of Lunches
Learn dbatools in a Month of LunchesLearn dbatools in a Month of Lunches
Learn dbatools in a Month of Lunches
 
Designing APIs with Swagger and OpenAPI
Designing APIs with Swagger and OpenAPIDesigning APIs with Swagger and OpenAPI
Designing APIs with Swagger and OpenAPI
 
PostGIS in Action, Third Edition: the newly-updated guide
PostGIS in Action, Third Edition: the newly-updated guidePostGIS in Action, Third Edition: the newly-updated guide
PostGIS in Action, Third Edition: the newly-updated guide
 

Último

5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceanilsa9823
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 

Último (20)

5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 

The Java Module System: reliably configuring Java 9 modules

  • 1. Save 42% off The Java Module System by Nicolai Parlog with code slparlog at manning.com
  • 2. Connecting the pieces Modules are the atomic building blocks, the nodes in our graph of interacting artifacts. But there can be no graph without edges connecting the nodes! So the modules express which other modules they depend on, creating edges between them. The next slide shows a graphic of the types of JVM modules: Need some background info on Java 9 modules? Check out the author’s website: http://blog.codefx.org/tag/jpms/
  • 4. Let’s imagine that we have a module customer and a module bar. When a module customer requires module bar in its declaration, then at run time customer will read bar or, conversely, bar will be readable by customer. barcustomer 1. requires bar 2. reads
  • 5. With that in mind, you can see how phrases like "customer requires bar" and "customer depend on bar“ mirror a static, compile-time relationship between customer and bar; readability is the more dynamic, run-time counterpart. You might be asking: Why is it more dynamic? While the requires clause is the primal originator of readability edges, it is by no means the only one – both command line arguments and the reflection API can be used to add more. However they came to be, readability edges are the basis for reliable configuration and strong encapsulation.
  • 6. Achieving reliable configuration Reliable configuration aims to ensure that the particular configuration of artifacts a Java program is compiled against, or launched with, can sustain the program without spurious run-time errors. To this end it performs a couple of checks – which happen during module resolution. The next slide shows the verification sequence.
  • 7. 1. First and foremost, the module system checks whether the universe of observable modules contains all required dependencies (both direct and transitive) and reports an error if something is missing. 2. There must be no ambiguity, which means that no two artifacts can claim that they are the same module. This is particularly interesting in the case where two versions of the same module are present—unfortunately the module system has no concept of versions and thus treats this as a duplicate module. Accordingly, you get an error if it runs into this situation. 3. There must be no static dependency cycles between modules. At run time it is possible and even necessary for modules to access each other, but these must not be compile dependencies. 4. Packages should have a unique origin so no two modules contain types in the same package. If they do, this is called a split package and the module system will refuse to compile or launch such configurations.
  • 8. This verification is, of course, not airtight and it is still possible for problems to hide long enough to crash a running application. If, for example, the wrong version of a module ends up in the right place, then the application will still launch (all required modules are present after all) but crash later when, for example, a class or method is missing. The module system is developed to exhibit consistent behavior across compile and run time, though, so these errors can be further minimized if it can be guaranteed that compilation and launch are based on the same artifacts (for example, a compilation against a module with the wrong version would fail).
  • 9. Unreliable configurations Now that you know how the module system is supposed to work (and when it won’t), let’s see if we can break things – so you know what to do when things go wrong (they will). Imagine that we have a simple monitor application, and consider monitor.observer.alpha and its declaration: module monitor.observer.alpha { requires monitor.observer; exports monitor.observer.alpha; }
  • 10. Missing dependencies Now let’s see what it would look like to try to compile it with monitor.observer missing: Missing dependency, no compilation! monitor.observer.alpha/src/main/java/module-info:2: error: module not found: monitor.observer requires monitor.observer ^ 1 error
  • 11. A different error would get thrown if the module was present at compile time, but got lost on the way to the launch pad. In this instance, the JVM will quit with the following: Missing dependencies Error occurred during initialization of VM java.lang.module.ResolutionException: Module monitor.observer not found, required by monitor.observer.alpha
  • 12. Duplicate modules Since modules reference one another by name, any situation where two modules claim to have the same name is ambiguous. Which one is the correct one to pick is highly dependent on the context and not something the module system can generally decide. So, instead of making a potentially bad decision, it makes none at all and instead produces an error. This commitment to failing fast allows the developer to notice a problem and fix it before it causes any more problems.
  • 13. Duplicate modules This is the compile error the module system produces when trying to compile a module with two variants of monitor.observer.beta on the module path: The compiler cannot link the error to one of the files under compilation because they are not the reason for the problem. In this case, the artifacts on the module path are causing the error. error: duplicate module on application module path module in monitor.observer.beta 1 error
  • 14. Duplicate modules In the event of an error going undetected until the JVM is launched, we get a more precise message that lists the JAR file names as well: As we mentioned before, the module system has no concept of versions – many errors can be traced to having several versions of the same module on the module path. Error occurred during initialization of VM java.lang.module.ResolutionException: Two versions of module monitor.observer.beta found in mods (monitor.observer.beta.jar and monitor.observer.gamma.jar)
  • 15. Dependency cycles Getting dependency cycles through the compiler takes some doing, and, if not done properly, will result in a compile error. This is how it looks if monitor.persistence and monitor.statistics depend on each other: monitor.statistics/src/main/java/module-info:3: error: cyclic dependence involving monitor.persistence requires monitor.persistence; ^ 1 error
  • 16. Split packages A split package occurs when two modules contain types in the same package. For example, the monitor.statistics module contains a class Statistician in the monitor.statistics package. Now let’s assume the monitor module contained a very simple fallback implementation SimpleStatistician, and to promote uniformity, the monitor module has placed it in its own monitor.statistics package. The error we would get at compile time is on the next slide:
  • 17. Split packages Interestingly enough, the compiler only shows an error if the module under compilation can access the split package in the other module, which means the split package must be exported. monitor/src/main/java/monitor/statistics/SimpleStatistician.java:1: error: package exists in another module: monitor.statistics package monitor.statistics; ^ 1 error
  • 18. Split packages Let’s see this another way: SimpleStatistician is gone and this time monitor.statistics creates the split module. It gets a Utils class in the monitor package, and continues to only export the monitor.statistics package. Compiling monitor.statistics is error-free, which makes sense because it does not require monitor. Compiling monitor is more interesting because it depends on monitor.statistics and both contain types in the package monitor. But because monitor.statistics does not export the split package, compilation is successful.
  • 19. Split packages That didn’t go so well… Indeed, the module system checks for split packages on launch and here it doesn’t matter whether they are exported or not: There can be no two modules that contain types in the same package. Error occurred during initialization of boot layer java.lang.reflect.LayerInstantiationException: Package monitor in both module monitor.statistics and module monitor Let’s see what happens when we launch:
  • 20. Grab your copy of The Java Module System and save 42% with code slparlog at manning.com. Also see: