官术网_书友最值得收藏!

  • Java EE 8 and Angular
  • Prashant Padmanabhan
  • 589字
  • 2021-07-02 19:22:41

WildFly Swarm

WildFly, formerly called JBoss, is an application server, and WildFly Swarm is a project that deconstructs the application server into modular parts. Swarm is aimed towards microservice applications, allowing the putting together of just enough subsets of the Java EE features that are required by the application.

An easy way to get started is to use the project generator page to select the features, or fractions, as they are called in Swarm terminology, and generate a maven project.

You can head over to the following link: http://wildfly-swarm.io/generator/:

The Wildfly Swarm link may change; it's best to use your favorite search engine, such as Google, to look this up.

The page presents a form where you can specify the maven projects Group ID, Artifact ID, and Dependencies. To create a microservice, let's use that page and fill in the following values:

  • Group ID: org.jee8ng
  • Artifact ID: issue-manager-users
  • Dependencies: JAX-RS with JSON-P

Once done, you can click on the Generate Project button which will download the zipped project file to your machine. If you unzip the file, you will be presented with the following structure of the Maven project:

├── pom.xml
└── src
└── main
└── java
└── org
└── jee8ng
└── issuemanagerusers
└── rest
└── HelloWorldEndpoint.java

The generated project is a simple Hello World project, containing one REST endpoint. Now, let's create a JaxrsBootup class, which is required to activate JAXRS capabilities:

package org.jee8ng.issuemanagerusers.rest;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("rest")
public class JaxrsBootup extends Application { }

Next, update the HelloWorldEndpoint.java file with the code given as follows, which returns a JSON response when invoked. The code builds a JsonObject instance which contains a name key and a string value associated with it:

package org.jee8ng.issuemanagerusers.rest;

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

@Path("/hello")
public class HelloWorldEndpoint {

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response doGet() {
JsonObject json = Json.createObjectBuilder()
.add("name", "hello microservice")
.build();
return Response.ok(json).build();
}
}

Open the pom.xml and update the finalName tags value to match, as shown in the following code:

<finalName>issue-manager-users</finalName>

Also update the dependencies section, with the following single dependency of microprofile:

 <dependencies> 
<!-- WildFly Swarm Fractions -->
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>micropofile</artifactId>
</dependency>
</dependencies>

You will notice that there's just one dependency specified. We don't even a need Java EE dependency, as the microprofile dependency already includes JAXRS, CDI, and JSON-P transitively.

Apart from these, we have the wildfly-swarm-plugin declaration, which enables the Uber JAR generation.

Open a terminal, and from the project root directory, run the following command, which will build and start the application:

 mvn wildfly-swarm:run

Once the application starts up, you can access the URL via a browser, or use the following curl command to fetch the response:

 curl http://localhost:8080/rest/hello

Response: {"name":"hello microservice"}

Stopping the application is as simple as pressing Ctrl + C on the running terminal window.

If you look within the target directory of the project, you can see that the wildfly-swarm-plugin has generated two artifacts:

Since the Uber JAR has the server runtime within it, we can execute it like any other standalone Java application JAR file:

  java -jar issue-manager-users-swarm.jar

That will start up the WildFly swarm instance with the minimal dependencies that have been used. This allows our application footprint to be smaller, as we are packaging only a subset of WildFly fractions/modules.

The Java heap memory for this application can be as low as 17-22 MB, which would have been a dream stat when compared to running a full WildFly application server.

主站蜘蛛池模板: 蓬莱市| 廊坊市| 宣化县| 丹阳市| 乌兰浩特市| 峨眉山市| 四会市| 新泰市| 涟水县| 台安县| 崇信县| 乌兰县| 曲靖市| 正蓝旗| 汉源县| 西安市| 广汉市| 云梦县| 沂南县| 扬州市| 错那县| 龙南县| 车险| 江达县| 宣城市| 长子县| 兰州市| 即墨市| 凤庆县| 乌兰察布市| 寿阳县| 冕宁县| 香河县| 精河县| 鹿邑县| 田林县| 德江县| 彩票| 响水县| 沾益县| 卓资县|