Compiling and Running a Service
The directory structure of the adviceService application is analogous to that of the adviceApp (Figure 19.9, p. 1181).
We can compile all modules in the adviceService application as before. However, note that compilation of the serviceprovider module does not require any other modules except the serviceinterface module.
>javac -d mods \
–module-source-path src \
–module serviceprovider,serviceconsumer,servicelocator,serviceinterface
We can run the application as before:
>java –module-path mods \
–module serviceconsumer/org.advice.serviceconsumer.AdviceConsumer
Advice for UK locale: Keep calm and service on!
Printing all advice:
Keep calm and service on!
Gi aldri opp!
We leave it as an exercise to experiment other configurations of implementing services. In particular, merging the service locator with the service consumer is highly recommended.
19.10 Creating Runtime Images
The Java Linker tool, jlink, can be used to assemble a customized runtime image of a modular application. This image only includes the modules in the application and their dependencies, together with a minimal JRE (Java Runtime Environment) to run the application.
We only present the basic use of the jlink tool, and strongly encourage consulting the documentation for the JDK tools for more in-depth coverage.
At a minimum, the jlink tool requires the following information to create a runtime image:
- The module path given by the –module-path option (short form: -p) to find modules that are to be included in the runtime image.
- The names of the modules that should be included in the runtime image. These modules are specified using the –add-modules option, and are looked up in the module path. If any standard or JDK modules required by the application are not specified, they are automatically included by the tool. The dependencies between the modules are automatically resolved.
- The output directory in which to create the runtime image, specified with the –output option.
The following command creates a runtime image of the adviceApp in the advice directory, assuming that the JAR files for the application modules can be found in the directory mlib:
jlink –module-path mlib –add-modules model,view,controller,main,java.base \
–output advice
The standard module java.base is explicitly specified. If omitted, it will be automatically included by the jlink tool.
The output directory advice containing the runtime image has the following structure, shown below with some of the files in the runtime image. The size of this particular runtime image is approximately 39 MB. Note the java command to launch the application under the directory advice/bin.
The modules in the runtime image advice can be listed by the following command:
>advice/bin/java –list-modules
controller
[email protected]
main
model
view
The application can be run from the runtime image by the following command, as the entry point of the application is specified in the main modular JAR included in the runtime image:
>advice/bin/java –module main
See no evil.
Speak no evil.
Hear no evil.
No advice.
A runtime image is platform specific, and thus meant to be run on the platform it was created for. As the runtime image contains platform-specific files, it will most likely not run on other platforms.
It is not possible to automatically update a runtime image with a newer version of Java. The runtime image must be rebuilt with the appropriate version of Java.
Runtime images are beneficial in many ways: They have a smaller memory footprint, can boost performance, are more secure, and are easier to maintain. Not surprisingly, runtime images are ideal for running on smaller devices.
Leave a Reply