How to create a JEE application with WildFly Swarm in two simple steps

Guess what, WildFly Swarm has been released and it's simpler than ever to configure and run your application as microservices. Let's create one basic application in a matter of minutes!

WildFly Swarm offers an innovative way to package and deliver Java EE application which does not need an available container where you have to provision your application. It's all there in a JAR file which can be executed as a standalone Java application. The only twist is adding swarm dependencies to your Maven project. This is now even simpler as you can use a Project generator to do it for you.

Using Swarm Project Generator

The Swarm Project generator is available at: http://wildfly-swarm.io/generator/

As you can see from the following picture, all is up to you is to choose the Maven project settings and the libraries needed to run your application:

swarm project tutorial wildfly

Simply type in the required dependencies and the Ajax text box will search for dependencies which match your search. Cool isn't it ?

When you are done, simply click on Generate Project which will download an empty Maven project which contains the pre-configured pom.xml with your dependencies. Let's see what has been created for us:

<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/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>demo</artifactId>
  <name>Wildfly Swarm Example</name>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <properties>
    <version.wildfly.swarm>1.0.0.Final</version.wildfly.swarm>
    <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>org.wildfly.swarm</groupId>
        <artifactId>bom</artifactId>
        <version>${version.wildfly.swarm}</version>
        <scope>import</scope>
        <type>pom</type>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <build>
    <finalName>demo</finalName>
    <plugins>
      <plugin>
        <groupId>org.wildfly.swarm</groupId>
        <artifactId>wildfly-swarm-plugin</artifactId>
        <version>${version.wildfly.swarm}</version>
        <executions>
          <execution>
            <goals>
              <goal>package</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <!-- Java EE 7 dependency -->
    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-api</artifactId>
      <version>7.0</version>
      <scope>provided</scope>
    </dependency>
    <!-- Wildfly Swarm Fractions -->
    <dependency>
      <groupId>org.wildfly.swarm</groupId>
      <artifactId>ejb</artifactId>
    </dependency>
    <dependency>
      <groupId>org.wildfly.swarm</groupId>
      <artifactId>undertow</artifactId>
    </dependency>
  </dependencies>
</project>

Let's add a couple of classes to test our Project. At first an EJB:

package swarm.demo.ejb;

import javax.ejb.Stateless;

@Stateless
public class DemoEJB {

    public String greet(String str) {
        return "Hello " +str;
    }
}

Next a Servlet calling the EJB:

package swarm.demo.servlet;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import swarm.demo.ejb.DemoEJB;

 
@WebServlet("/Hello")
public class HelloServlet extends HttpServlet {

	@EJB DemoEJB ejb;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write(ejb.greet("Francesco"));
    }
}

As you can see there's nothing which binds you to WildFly Swarm environment. It's a basic Java EE project.

Building and running the Project

In order to execute, at first build the project with:

$ mvn package

Now you can run it in two ways: by using java -jar [yourproject]:

$ java -jar target/demo-swarm.jar

Or if you prefer by using Swarm plugin:

$ mvn wildfly-swarm:run

WildFly application server will start, embedding the demo application on the root context. You can test it by reaching the Servlet's URL: http://localhost:8080/Hello

Plenty of examples are available on github at: https://github.com/wildfly-swarm/wildfly-swarm-examples

Follow us on Twitter