- 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.
- Java范例大全
- 零起步玩轉(zhuǎn)掌控板與Mind+
- 基于Java技術(shù)的Web應(yīng)用開(kāi)發(fā)
- Cassandra Design Patterns(Second Edition)
- Full-Stack React Projects
- SharePoint Development with the SharePoint Framework
- Java項(xiàng)目實(shí)戰(zhàn)精編
- 深入理解Android:Wi-Fi、NFC和GPS卷
- Learning Concurrency in Kotlin
- Raspberry Pi Robotic Projects(Third Edition)
- SciPy Recipes
- FFmpeg開(kāi)發(fā)實(shí)戰(zhàn):從零基礎(chǔ)到短視頻上線
- ABAQUS6.14中文版有限元分析與實(shí)例詳解
- IBM RUP參考與認(rèn)證指南
- VMware vSphere Design Essentials