WildFly Swarm REST example

Microservices are here. They have been here for a long time now and it looks like they are going to be here for a while. Some technologies have born in recent years to help developers to implement their systems following this architectural style. One of these technologies is WildFly Swarm.

Probably, if you are familiar with the Java EE world, you know the WildFly application server. WildFly Swarm is a derivate of this application server but it has been created with microservices architectural style in mind. Taking the definition from the WildFly Sward documentation page:

WildFly Swarm is a project that has taken the WildFly Java Application Server and deconstructed it into fine-grained parts. WildFly Swarm then allows the selective reconstitution of those parts back together with your application to allow building self-contained executable “uberjars”.

It has some other components and functions but, mainly, reading the definition we can understand the concept.

As I have said this technology is here to fill the gap that the old and big application servers cannot cover (This is a huge discussion) in the microservises world. It is comparable with Spring Boot or Payara Micro. With its different flavors and features, all of them cover the same gap.

One of the things that it calls our attention in the definition is the word “uberjars“. Considered a buzzword or maybe not, it is just defining a JAR file that contains a package and all its dependencies. In this case, for example, it is going to be a package that it is going to contain our code plus all the dependencies to run a server with our application just executing the order “java -jar file.jar” in a command line prompt.

After this little explanation, let’s go to build our first WildFly Swarm application. It is going to be a very simple REST application just with a method returning some text. And it is going to look like basically as a tradicional Java EE application. One of the best things of migrating a Java EE 7 application to WildFly Swarm is that the code doesn’t change at all. We can take the old and monolithic code and run it as an uberjar just with a few modifications in the maven pom.xml file.

The first step is to define the correct pom.xml file. In this file we are going to include the proper dependencies to build our REST service and a plugin that it is going to allow us to create our uberjar and/or run our application.

...
<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>
 
<dependencies>
    ...
    <!-- Wildfly Swarm Fractions -->
    <dependency>
        <groupId>org.wildfly.swarm</groupId>
        <artifactId>jaxrs</artifactId>
    </dependency>     
</dependencies>
 
<build>
    <finalName>RestWildFlySwarm</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>

The second thing, it is just add the necessary code to the project to implement the REST service. Like I have said before, it is simple Java EE 7 code, I don’t think it need any especial explanation, if you need some additional explanation you have in this blog some articles about REST services and how to implement them in Java EE 7. One of this examples can be found here.

package com.example.rest;
 
import javax.ws.rs.core.Application;
import javax.ws.rs.ApplicationPath;
 
@ApplicationPath("/rest")
public class RestApplication extends Application {
}
package com.example.rest;
 
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
 
@Path("/messages")
@Produces(MediaType.APPLICATION_JSON)
public class MessagesEndPoint {
    @GET
    public Response getMessage() {
        return Response.ok("This is our first WildFly Swarm App. Awesome!").build();
    }
}

And that’s all we need to add to our first project. Now, we just need to run it.

The first way to do this, it is just running a simple maven command:

mvn wildfly-swarm:run

This first time is going to download a big bunch of dependencies and stuff, and it is going to take some time. Future executions will be much faster.

The second way to execute our application is creating our uberjar file and running it from the console.

mvn wildfly-swarm:run

You can see the full code for the example here.

See you.

WildFly Swarm REST example

Creating a basic datasource in Glassfish 4

Today we are going to see how to create a datasource in our Glassfish server in a few steps. In this case, it is going to be for a MySQL database:

First of all, if we have not done yet, we should copy the MySQL driver in our folder lib inside the domain folder where we are going to deploy our application. After that, let’s start to configure our datasource.

As default, the admin console for Glassfish server is in the port 4848. You should be able to see this admin console connecting to your server with this port. In my case, I have the server installed in my own machine, then, my URL to connect to the admin console looks like this:

http://localhost:4848/

If the connections is performed correctly, the browser should prompt you asking for the admin credentials, and after introduce them correctly, you should see the admin console with a menu in the left side.

In this menu, we are going to open the category:

Resources -> JDBC

Under this category, you should find two more categories:

  • JDBC Resources
  • JDBC Connection Pools

Let’s open the JDBC Connection Pools category and click in New… to create a new connection pool for our datasource.

Here, we can see that the process to create a new connection pool has two steps. We should introduce the following data:

In the first step:

  • Name: In my case, ExamplePool.
  • Resource type: We should select javax.sql.DataSource from the dropdown list.
  • Database Driver Vendor: We should select MySql for this example.

In the second step you can accept all the default values and go directly to the Additional Properties table. Here, the best option, specially if we want a full control of everything, we can delete all the properties in there, and define new properties. In this case, we are only go to add the basic ones to connect to our database, but there are a lot of different properties that can be added. Then, let’s add our properties:

  • User: The user to connect to de DB. In my case, dbuser
  • Password: The user’s password to connect. In my case, resudb. In this case, MySQL allows us to have users without passwords, but Glassfish is going to return an error if the user does not have a password. Then, if your user does not have a password, you should set one. *
  • Url: The URL to connect to our schema. In this case, I am using the schema example. The URL to connect, in my case, looks like: jdbc:mysql://localhost:3306/example

In most cases, setting these properties is enough, but I have detected that there are IDEs where is not enough. In this case, as a precaution, we can set an extra property to avoid future problems:

  • driverClass: In this case, the MySQL driver class com.mysql.jdbc.Driver

Now, it is time to save our pool. After that, if you click in the left menu in the pool’s name, you will see the configuration of the pool in the main page and a new button labeled as Ping. Let’s click on it to confirm that the configuration is correctly done. If everything goes well, a message Ping Succeeded should be shown.

Now that our pool is ready, we should create our datasource. For this, we should click in the left menu in the JDBC Resources category and click in the New… button. In here, we only need to add the JNDI name, in general, something like jdbc/example and choose in the dropdown list the pool name that we have previously created (ExamplePool). After that, we can save it and, we are ready to go.

That’s all. Now, we only need to use our new datasource in our applications.

See you.

* To set a password for a MySQL user we should execute:

UPDATE mysql.user
   SET user.Password = PASSWORD('newPassword')
 WHERE user.User = 'username';
Creating a basic datasource in Glassfish 4