Share
Embed
Practicing The Java Module System
Practicing The Java Module System presentation - Devoxx PL
Rahman Usta
June 21, 2018
Transcript
36 slides
-
Agenda • Concept introduction • Practice times! http://bit.ly/java9-workshop 1. Generate
JREs 2. Create a hello world module 3. Control access between modules 4. Using Auto-modules 5. Using Unnamed modules 6. Testing HTTP/2 Client API 7. Testing JSHell -
Versions history 1995 ...> First release 1996 ...> Java 1.0
1997 ...> Java 1.1 (Reflection, JDBC) 1998 …> Java 1.2 (JIT, Swing) 2000 …> Java 1.3 (RMI, JNDI) 2002 …> Java 1.4 (Nio, Logging) 2004 …> Java 5 (Generics, Annotations) 2006 …> Java 6 (JDBC 4.0, Scripting) 2011 …> Java 7 (Project Coin, Fork-Join, Nio 2) 2014 …> Java 8 (Lambda, Method Ref, Stream API) 2017 …> Java 9 2018 (March) …> Java 10 -
Java 9 New features Jigsaw Related Changes • JEP 200
- The Modular JDK • JEP 201 - Modular Source Code • JEP 220 - Modular Run-Time Images • JEP 260 - Encapsulate Most Internal APIs • JEP 261 - Module System • JEP 282 - The Java Linker • JEP 238 - Multi-Release JAR Files 90+ new features http://openjdk.java.net/projects/jdk9/ • Other changes • Project Kulla (JShell) • HTTP/2 Client API • Flow API • Collections factories • API changes: Process API, • Stream API e.g. -
Goals of the Project Jigsaw • To increase access control
on the Java components • To increase security by removing access to propietry APIs • To increase performance of the Java platform • To get rid of the monolithic JDK • To modularize JDK source code • To bring Scalable JREs -
Monolithic X Monolithic Kernel => Micro Kernel Monolithic Services =>
Micro Services Monolithic JDK => Modular JDK -
Practice time! Let’s create different scale of JREs with JLink
tool. Resources: http://bit.ly/java9-workshop -
What is Java module ? • Associated Java package(s) are
grouped by a name. • Brings access control between modules $ java -d java.sql -
How to define a Java Module? Module definitions • exports
• requires • opens • provides, with -
New parameters for Java tools --p <module path> --module-path <module
path>... A ; separated list of directories, each directory is a directory of modules. --add-modules Adds modules to resolution list -m <module>[/<mainclass>] Root module and main class -
Practice time! Let’s create a modular Hello world! application •
Create a module • Package a module • Run a module • Link a module with JRE -
Access modifiers (< Java 9) Brings accessibility levels to Java
components (class, method, field) • public • protected • package-private • private No Access Control on Package Level -
Access modifiers (>= Java 9) Brings accessibility levels to Java
components (module, class, method, field) public to all modules public only to specific module(s) public only to its own module private protected package Has Access Control on Package Level -
Qualified vs Unqualified exports Qualified Export package is readable by
a spesific module exports com.a.b.c to com.to.a.module Unqualified Export package is readable by any module exports com.a.b.c; -
Named module Standard Java module declared with module-info.java Modules are
loaded from --module-path For example: java --module-path lib/abc-1.0.jar;... -
Automatic Module If a non-moduler JAR is added module path,
it becomes an automatic module. -
Practice time! Let’s use a non-modular JAR file in a
modular Java application. • Auto-modules are helpful for migration • Uses jansi library to print colored texts! -
Practice time! Let’s try to access from a non-module to
a module • Check compile time access -
Java 9 and Reflection Java Reflection API is one of
the most helpful API for the library developers. Many open-source libraries use it for example: Hibernate, Spring Framework. • Access Java classes, modules, fields, methods, constructors e.g. • Create Java objects • Call Java methods • Access private methods and fields • Access annotations -
Reflective Access between modules If a module want to access
to other module’s components, other component have to open or export required packages to caller module. exports: Makes packages accessible to other modules at compile time or run time. opens: Makes packages accessible to other modules at run time including access to private members. -
How to open a package or module Three ways: •
opens com.a.b.c; • open module com.a.module { // open module } • --add-opens com.a.module/com.a.b.c=com.other.module -
HTTP/2 Client API (an incubator module) module com.kodedu.http2 { requires
jdk.incubator.httpclient; } HttpResponse response = HttpRequest .create(URI.create("https://devoxx.pl")) .GET() .response(); System.out.println(response.body(HttpResponse.asString())); -
Practice time! Let’s run a HTTP/2 server and connect it
via Java HTTP/2 Client API. -
JShell ( Project Kulla) It is a REPL(read-eval-print-loop) tool for
Java. tryjshell.org