The WildFly Swarm Maven plugin

WildFly Swarm offers a Maven plugin solely. At the time of writing, there is no Gradle plugin available for WildFly Swarm. In general, Maven projects may be generated by means of a project generator. In this section, a very minimalistic temperature Microservices with minimal runtime using WildFly Swarm is demonstrated. Also, Swarm's ability to detect dependencies is leveraged to produce a project with minimal configuration overhead.

The first step is to create a brand new Java EE project with Maven, using the war packaging:

<packaging>war</packaging>

In Maven's properties section, a version of WildFly is defined as a project-wide variable. Such an approach is advantageous as version upgrades would mean minimum change in Maven :

<properties>
<version.thorntail>2.2.1.Final</version.thorntail>
<!-- Other properties omitted -->
</properties>

Wildfly Swarm Maven dependencies are contained in a single Bill of Materials (BOM). All the dependencies are defined in a single entry, using the previously-defined version property:

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.thorntail</groupId>
<artifactId>bom-all</artifactId>
<version>${version.thorntail}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

The WildFly Swarm Maven plugin is required to be activated explicitly. In the following example, a standalone executable artifact is created whenever the package goal is invoked. This also includes the install goal. The standalone artifact is to be found in {project-root}/target/ folder. It always has a suffix of swarm.jar. Such a Microservice only requires a standard JDK to run, everything else is included inside the artifact.  A simple java -jar application-name-swarm.jar is enough for the Microservice to run:

<build>
<finalName>wildfly-swarm-temperature-Microservice</finalName>
<plugins>
<plugin>
<groupId>io.thorntail</groupId>
<artifactId>thorntail-maven-plugin</artifactId>
<version>${version.thorntail}</version>
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

The structure of project dependencies is simple. The Java EE 8 API as a provided dependency is standard and expected. The temperature Microservice used throughout this book requires Plain Old Java Objects (POJO) contained in the common module and will probably not be part of the reader's Microservice. These are simply extracted to a separate module for readability and easy reuse. Note that there no WildFly Swarm dependencies are explicitly declared. WildFly Swarm is able to automatically detect the required dependencies:

<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>common</artifactId>
<version>1.0</version>
</dependency>
</dependencies>

The resulting Maven's pom.xml file used in the example project is demonstrated as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>javaee8-Microservices-book</artifactId>
<groupId>com.packtpub.Microservices</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>10-wildfly-swarm-temperature</artifactId>
<packaging>war</packaging>

<properties>
<version.thorntail>2.2.1.Final</version.thorntail>
<!-- Other properties omitted -->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.thorntail</groupId>
<artifactId>bom-all</artifactId>
<version>${version.thorntail}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<finalName>wildfly-swarm-temperature-Microservice</finalName>
<plugins>
<plugin>
<groupId>io.thorntail</groupId>
<artifactId>thorntail-maven-plugin</artifactId>
<version>${version.thorntail}</version>
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>common</artifactId>
<version>1.0</version>
</dependency>
</dependencies>

</project>

Before the application is run, a RESTful endpoint with JAX-RS inside is added. First, a simple JAX-RS configuration is required. This empty configuration class that extends the javax.ws.rs.core.Application class uses the @ApplicationPath annotation from the same package to configure a basic Microservice context root:

@ApplicationPath("/weather")
public class WeatherMicroservice extends Application {

}

Finally, the static temperature service is added:

package swarm;

import com.packtpub.Microservices.domain.weather.Temperature;
import com.packtpub.Microservices.domain.weather.TemperatureScale;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/temperature")
public class TemperatureResource {

/**
* Provides average temperature from all the city's sensors. The temperature
* is artificial.
*
* @return {@link Response} with constant temperature
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getAverageTemperature() {
Temperature temperature = new Temperature();
temperature.setTemperature(35D);
temperature.setTemperatureScale(TemperatureScale.CELSIUS);

return Response.ok(temperature).build();
}
}

Automatic dependency-discovery may be the observer when invoking the wildfly-swarm:run Maven goal. In the case of the sample MicroService, there is only one REST endpoint using JAX-RS to expose itself. Therefore, WildFly correctly detects JAX-RS. Undertow is used to serve HTTP requests. It is worth mentioning that Undertow is a high-performance product, dominating many benchmarks. The rest are services required to run. For example, logging functionality is used even at the application start itself:


INFO: Installed fraction: Logging - STABLE org.wildfly.swarm:logging:2018.3.3
INFO: Installed fraction: Elytron - STABLE org.wildfly.swarm:elytron:2018.3.3
INFO: Installed fraction: JAX-RS - STABLE org.wildfly.swarm:jaxrs:2018.3.3
INFO: Installed fraction: Undertow - STABLE org.wildfly.swarm:undertow:2018.3.3

The default context root is set to / and the default port that WildFly Swarm listens on is 8080. Therefore, the example temperature endpoint can be found under the http://localhost:8080/weather/temperature URL.

As additional functionality is plugged-in, WildFly will automatically detect it and bundle corresponding modules. This approach works flawlessly with the Java EE functionality. A sample Microservice that provides a static temperature measurement is available in the code samples bundled with this book.

..................Content has been hidden....................

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