By Steve Millidge

There seems to be a mythology around that Java EE is heavyweight and needs complex application serverScreen Shot 2015-02-23 at 11.17.37
installation, configuration and management and therefore is not well suited for deploying microservices. However Payara Embedded blows away that myth.

The Payara Embedded Web Profile jar is just 54Mb in size and requires no configuration or installation. You can bootstrap and deploy REST services into Payara Embedded straight from your Java mai().

Payara Embedded packages a full GlassFish Java EE 7 application server in a single library jar file. There are two variants full Java EE and Web Profile. In this blog we will use the Web Profile jar to run RESTful services.

Download Payara Embedded and Run

Download the Payara-embedded-web 4.1.151 (Web Profile Implementation) Jar from http://www.payara.co/downloads . All you need to do is set the jar up as a library in your favourite IDE.

Then bootstrap Payara using the code below;

Listing 1

package embeddedpayara;

import java.io.File;

import org.glassfish.embeddable.GlassFish;

import org.glassfish.embeddable.GlassFishException;

import org.glassfish.embeddable.GlassFishProperties;

import org.glassfish.embeddable.GlassFishRuntime;

public class EmbeddedPayara {

public static void main(String[] args) throws GlassFishException {

GlassFishRuntime runtime = GlassFishRuntime.bootstrap();

GlassFishProperties gfproperties = new GlassFishProperties();

gfproperties.setPort("http-listener", 8080);

GlassFish gf = runtime.newGlassFish(gfproperties);

gf.start();

gf.getDeployer().deploy(new File("/opt/NetBeansProjects/WebService/dist/WebService.war"));

}

}

You’ll notice the last line in main deploys a war file, so let’s create it.

Build a JAX-RS RESTful service

The war file we’ll create is a simple “Hello World” REST service. It contains two classes the ApplicationConfig and the REST resource itself. The ApplicationConfig class adds the REST resources.

Listing 2

package fish.payara.webservices.example;

import java.util.Set;

import javax.ws.rs.core.Application;

@javax.ws.rs.ApplicationPath("webresources")

public class ApplicationConfig extends Application {

    @Override

    public Set<Class<?>> getClasses() {

        Set<Class<?>> resources = new java.util.HashSet<>();

        addRestResourceClasses(resources);

        return resources;

    }

    private void addRestResourceClasses(Set<Class<?>> resources) {

        resources.add(fish.payara.webservices.example.GenericResource.class);

    }

}

The GenericResource class creates a RESTful resource that returns Hello World when accessed.

Listing 3

package fish.payara.webservices.example;

import javax.ws.rs.Produces;

import javax.ws.rs.GET;

import javax.ws.rs.Path;

@Path("generic")

public class GenericResource {

public GenericResource() {

}

@GET

@Produces("application/xml")

public String getXml() {

return "&lt;test&gt;hello world&lt;/test&gt;";

}

}

These classes should be packaged up into a war file. This can then be deployed directly into your embedded Payara by referencing it in the bootstrap code in listing 1 above.

The REST war file needs no other libraries packaged in it, resulting in a very thin war only 3K in size. This simplifies deployment dependencies and reduces the deployment footprint of your microservices.

Run the Example

When you run the application it will bootstrap Payara and deploy the war file.

Screen Shot 2015-02-23 at 11.30.44

Which can then be accessed from port 8080

Small Footprint

The resulting REST service runs in a very small footprint as the VisualVM screen shot below shows. In this case the Payara embedded process is running in a JVM with a max heap size of 50MB and is still only using 30MB of JVM heap. Truly a microservice.

Wrap Up

Payara Embedded is the ideal light-weight platform for deploying RESTful microservices. Payara Embedded has all the capabilities of full Payara so Java EE apis including JPA, CDI and Batch are all available to your services as well as the shared data caching capabilities of Hazelcast to transparently share data across multiple microservice JVMs.

Payara Embedded is a small footprint, light-weight library, that requires no installation or configuration.

That blows away the myth that Java EE is heavyweight.