Building and running the Weather Microservice

Before running the Microservice, it has to be compiled and packaged. Maven, the project management tool, automates this process. The whole project is represented by Maven's .pom file, named pom.xml. This file is present in the project's root folder. Maven, when executed, searches for pom.xml in the very folder in which it has been executed. In the example application, there is only one folder and one file to be found. First and most important is the src/ folder, with the application's code and resources. The name of this folder, src,  is standardized by Maven unless updated to some custom folder; the sources of the application are expected to be there:

.{project-root}
├── pom.xml
└── src

In order to build the project, change the working directory to .{project-root}. After changing the working directory, the next step is to instruct Maven to compile, verify, and package the whole application in one instruction. After the packaging is completed, Maven reports a BUILD SUCCESS:

> mvn package
[INFO] Scanning for projects...
......several output omitted......
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

After mvn package is executed, Maven downloads all the necessary dependencies, compiles the Java code, and packages the result into a Java Web Archive (WAR). The resulting WAR is placed by Maven into a unified directory, named target/, in the .{project-root} folder:

.{project-root}
├── pom.xml
├── src
└── target

The name of the WAR placed inside the target/ directory consists of several parts: {artifactId}.{version}.{packaging}. These variables are placed in the pom.xml file itself during the project creation phase. Maven uses them to construct the name of application artifacts during the build process.

Since Java EE is used, there is only one dependency, Java EE API. This dependency is scoped as provided, telling Maven not to include it in the resulting WAR. Therefore, no additional code is bundled in the resulting WAR, only the Microservice itself, resulting in a very small deployment unit of several kilobytes.

With the Weather Microservice built in the /target folder, the Microservice is now ready to be run in Payara Micro. To run the Microservice, do the following:

  1. Start Payara Micro.
  2. Deploy the Microservice.

Both steps can be executed with a single command:

java -jar /path/to/payara-micro-{version}.jar --deploy /path/to/weather-service-1.0.war

By issuing that command, Payara Micro is invoked as a standard Java application and instructed to deploy a single WAR. The process of startup and deployment is automatic and does not require user interaction. The first phase is the start of Payara Micro itself. When Payara Micro is successfully started, the second phase, where deployment units are handled, is initiated. The set of deployment units is defined by the --deploy {deployment-unit}.{war|jar|ear} flag.

If there is a problem with running Payara Micro, the cause is usually other applications bound to ports used by Payara Micro. Typically, Payara Micro listens on port 8080, which is commonly used by many other programs.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.71.94