Now a day's monolithic applications comes into micro batches. Every micro batch independent from each others and deploy. Now the Java will come in this flavor using project Jigsaw
Java 9 comes with great feature call "Jigsaw", which modularize monolithic Java code into modules. Where we design independently standard modules with in different scopes. Primary goal of "jigsaw" to make scalability, maintainability, performance, security etc. For more information please click on this link.
Today we creating just a greeting example using project "Jigsaw" or modularize our code independently.
From above command we are creating two directories, which is basically our project modularization. These are two independent modules and for packaging, two independent jar. which we will see later.
In above code, we are using
In above module code, we are using
Create Main class
For more about Java 9 features, we are continuing with new blogs.
Java 9 comes with great feature call "Jigsaw", which modularize monolithic Java code into modules. Where we design independently standard modules with in different scopes. Primary goal of "jigsaw" to make scalability, maintainability, performance, security etc. For more information please click on this link.
Today we creating just a greeting example using project "Jigsaw" or modularize our code independently.
Prerequisite
- Download OpenJDK - 9
- Text Editor
Step - I
Create a source directory for our source code:$ mkdir -p jigsaw-sample1/src
Step - II
Categories our system into modules like in this example, i have one utility module and other main module. The utility module contains utility classes and main module access that utility class and their methods for performing operations.jigsaw-sample1/src $ mkdir com.knoldus.util com.knoldus.mainBefore jigsaw, we module our project using packages but still we have monolithic structure. Our package conventions is reverse domain of company. Jigsaw still recommend that convention.
From above command we are creating two directories, which is basically our project modularization. These are two independent modules and for packaging, two independent jar. which we will see later.
Step - III
Create a package structure within our module likecom.knoldus.util
directory contains directories com/knoldus/util
.jigsaw-sample1/src/com.knoldus.util $ mkdir -p com/knoldus/util jigsaw-sample1/src/com.knoldus.util $ cat > module-info.javaThe
module-info.java
contains the information of module and define the scope of module as below:module com.knoldus.util { exports com.knoldus.util; }
In above code, we are using
exports
for define, that other modules can use this module or not. Even if the scope of class in public and we are not define scope of utility module to exports, other modules not able to access utility classes. If this module require some other modules, then we need to define here, which we will see later.Step - IV
Like above code, now we are creating package structure of our main module andmodule-info.java
for module information.jigsaw-sample1/src/com.knoldus.main$ mkdir -p com/knoldus/main jigsaw-sample1/src/com.knoldus.main$ cat > module-info.javaThe
module-info.java
contains following code:module com.knoldus.main { requires com.knoldus.util; }
In above module code, we are using
requires
instead of exports
because we are using our utility module in main module. For accessing classes, we need to define our required module here.Step - V
Create utility classjigsaw-sample1/src/com.knoldus.util/com/knoldus/util$ cat > Greeting.java with following code:
package com.knoldus.util; public class Greeting { private Greeting() {} public static String greetings(String user){ return user + " greetings !!! "; } }
Create Main class
jigsaw-sample1/src/com.knoldus.main/com/knoldus/main$ cat > Main.javawith following code:
package com.knoldus.main; import static com.knoldus.util.Greetings.greetings; public class Main { public static void main(String[] args) { System.out.println(greetings("Hello Knoldus")); } }
Step - VI
The compilation using "Jigsaw" is really different. The byte-code is divided into modules. If you already have Java previous version in system, export JDK-9 temporary into terminal as below:export JIGSAW_HOME=/opt/jdk-9/ export JIGSAW_BIN=/opt/jdk-9/bin/Run the following command for compile Java code:
jigsaw-sample1$ mkdir mods jigsaw-sample1$ $JIGSAW_BIN/javac -d mods --module-source-path src $(find src -name "*.java") jigsaw-sample1$ $JIGSAW_BIN/java -p mods -m com.knoldus.main/com.knoldus.main.Main Hello Knoldus greetings !!!
Step - VII
When we deploy our Java applications, we are using jar packaging for easy deployment and distribution. With "Jigsaw" following are the way for package our modules and execute jar:jigsaw-sample1$ mkdir mlib jigsaw-sample1$ $JIGSAW_BIN/jar --create --file=mlib/com.knoldus.util@1.0.jar --module-version=1.0 -C mods/com.knoldus.util . jigsaw-sample1$ $JIGSAW_BIN/jar --create --file=mlib/com.main.jar --main-class=com.knoldus.main.Main -C mods/com.knoldus.main . jigsaw-sample1$ $JIGSAW_BIN/java -p mlib -m com.knoldus.mainNote: For downloading source code of above example, please click on link.
For more about Java 9 features, we are continuing with new blogs.
No comments:
Post a Comment