Microprofile applications can be used in a large variety of contexts. In this tutorial we will learn how to use its API in the most common runtime environments.

Configuring Microprofile API with Thorntail

Thorntail offers a modern approach to packaging and running Java EE applications, including just enough of the environment to "java -jar" your application. Most interestingly, it's full MicroProfile compatible.

In order to use Microprofile API, you can just plug in the Microprofile API needed by your project, leaving aside the version which is defined in the Thorntail's BOM file. Here is the set of Microprofile dependencies you can include in your applications:

  <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>demo</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>io.thorntail</groupId>
      <artifactId>microprofile-openapi</artifactId>
    </dependency>
    <dependency>
      <groupId>io.thorntail</groupId>
      <artifactId>microprofile-jwt</artifactId>
    </dependency>
    <dependency>
      <groupId>io.thorntail</groupId>
      <artifactId>opentracing</artifactId>
    </dependency>
    <dependency>
      <groupId>io.thorntail</groupId>
      <artifactId>microprofile-fault-tolerance</artifactId>
    </dependency>
    <dependency>
      <groupId>io.thorntail</groupId>
      <artifactId>microprofile-health</artifactId>
    </dependency>
    <dependency>
      <groupId>io.thorntail</groupId>
      <artifactId>microprofile-restclient</artifactId>
    </dependency>
    <dependency>
      <groupId>io.thorntail</groupId>
      <artifactId>microprofile-metrics</artifactId>
    </dependency>
  </dependencies>

Aside from that, you can use also an "umbrella" fraction named "microprofile" which brings into the game all the above fractions:

<dependency>
      <groupId>io.thorntail</groupId>
      <artifactId>microprofile</artifactId>
</dependency>

In this tutorial Managing Microprofile Health Checks you can check how to use Microprofile Health extension on Thorntail and deploy it on Openshift:

Configuring Microprofile API with Quarkus

Quarkus has been designed from the grounds up to be fully compatible with Microprofile specifications. Once that you have a basic JAXRS application, we can start adding the Microprofile extensions we need.

First a basic project creation:

mvn io.quarkus:quarkus-maven-plugin:0.20.0:create \
    -DprojectGroupId=org.acme \
    -DprojectArtifactId=quarkus-demo \
    -DclassName="org.acme.Endpoint" \
    -Dpath="/secured"

Now let's see how to add the single extensions:

Health:

$ mvn quarkus:add-extension -Dextensions=health

Related dependency:

<dependency>
  <groupId>io.quarkus</groupId>
  <artifactId>quarkus-smallrye-health</artifactId>
</dependency>

Fault Tolerance:

$ mvn quarkus:add-extension -Dextensions=fault-tolerance

Related dependency:

<dependency>
  <groupId>io.quarkus</groupId>
  <artifactId>io.quarkus:quarkus-smallrye-fault-tolerance</artifactId>
</dependency>

Metrics:

$ mvn quarkus:add-extension -Dextensions=metrics

Related dependency:

<dependency>
  <groupId>io.quarkus</groupId>
  <artifactId>io.quarkus:quarkus-smallrye-metrics</artifactId>
</dependency>

OpenAPI:

$ mvnw quarkus:add-extension -Dextensions="openapi"

Related dependency:

<dependency>
  <groupId>io.quarkus</groupId>
  <artifactId>quarkus-smallrye-openapi</artifactId>
</dependency>

OpenTracing:

$ mvn quarkus:add-extension -Dextensions=opentracing

Related dependency:

<dependency>
  <groupId>io.quarkus</groupId>
  <artifactId>io.quarkus:quarkus-smallrye-opentracing</artifactId>
</dependency>

REST Client:

$ mvn quarkus:add-extension -Dextensions=rest-client

Related dependency:

<dependency>
  <groupId>io.quarkus</groupId>
  <artifactId>io.quarkus:quarkus-smallrye-rest-client</artifactId>
</dependency>

JWT:

$ mvn quarkus:add-extension -Dextensions=jwt

Related dependency:

<dependency>
  <groupId>io.quarkus</groupId>
  <artifactId>io.quarkus:quarkus-smallrye-jwt</artifactId>
</dependency>

Configuring MicroProfile with WildFly application server

WildFly application server ships out of the box with support for the following Microprofile extensions:

<extension module="org.wildfly.extension.microprofile.config-smallrye"/>
<extension module="org.wildfly.extension.microprofile.health-smallrye"/>
<extension module="org.wildfly.extension.microprofile.metrics-smallrye"/>
<extension module="org.wildfly.extension.microprofile.opentracing-smallrye"/>

These extensions are available in all server configurations therefore you don't need any special tweak to use these APIs. In terms of application development, to compile your applications you will need the following dependencies:

<dependency>
    <groupId>org.eclipse.microprofile.config</groupId>
    <artifactId>microprofile-config-api</artifactId>
    <version>1.3</version>
</dependency>
 
<dependency>
    <groupId>org.eclipse.microprofile.health</groupId>
    <artifactId>microprofile-health-api</artifactId>
    <version>2.0.1</version>
</dependency>
 
<dependency>
    <groupId>org.eclipse.microprofile.metrics</groupId>
    <artifactId>microprofile-metrics-api</artifactId>
    <version>2.0.1</version>
</dependency>

<dependency>
    <groupId>org.eclipse.microprofile.opentracing</groupId>
    <artifactId>microprofile-opentracing-api</artifactId>
    <version>1.3.1</version>
</dependency>

You can configure Microprofile applications on WildFly through a set of different options:

  • System.getProperties()
  • System.getenv()
  • All META-INF/microprofile-config.properties files on the ClassPath
  • All entries in microprofile-config-smallrye subsystem

You can check this tutorial Configuring Applications with Eclipse MicroProfile Config for some examples of using Microprofile API with WildFly application server.

0
0
0
s2smodern