Buy Support Online

Using Payara Embedded & Payara Micro with Maven

15 May 2015
fish_3_large.jpgAt long last, Payara Embedded and Payara Micro are on Maven Central, allowing you to set them as dependencies and use them in your projects. In this blog I'll briefly cover how to do this.I assume if you’re reading this you already have a Maven project set up, so I’ll start from there. If you haven’t, then just create a new Maven Java project in your favourite IDE.
First off, open the POM.xml file of the module or project that needs to use Payara Embedded or Micro, and add one of the embedded distributions as a dependency:

 

Payara Embedded All

 

<dependency>
    <groupId>fish.payara.extras</groupId>
    <artifactId>payara-embedded-all</artifactId>
    <version>4.1.152.1</version>
    <type>jar</type>
</dependency>

 

Payara Embedded Web

<dependency>
    <groupId>fish.payara.extras</groupId>
    <artifactId>payara-embedded-web</artifactId>
    <version>4.1.152.1</version>
    <type>jar</type>
</dependency>

 

Payara Micro

<dependency>
    <groupId>fish.payara.extras</groupId>
    <artifactId>payara-micro</artifactId>
    <version>4.1.152.1</version>
    <type>jar</type>
</dependency>

 

And that’s about it! With one of the above added as a dependency, you can now begin using Payara embedded in your application and project. For example, the following code will create and start an embedded Payara server, with the HTTP and HTTP listener set to ports 8083 and 8184 respectively:

 

import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.embeddable.BootstrapProperties;
import org.glassfish.embeddable.GlassFishRuntime;
import org.glassfish.embeddable.GlassFish;
import org.glassfish.embeddable.GlassFishException;
import org.glassfish.embeddable.GlassFishProperties;

/**
 * Basic Example showing how to programmatically create, edit, and
 * start an embedded Payara Server.
 * 
 * @author Andrew Pielage
 */
public class EmbeddedExample 
{
    public static void main(String [] args)
    {
        try 
        {
            BootstrapProperties bootstrap = new BootstrapProperties();
            GlassFishRuntime runtime = GlassFishRuntime.bootstrap();
            GlassFishProperties glassfishProperties = new GlassFishProperties();
            glassfishProperties.setPort("http-listener", 8083);
            glassfishProperties.setPort("https-listener", 8184);
            GlassFish glassfish = runtime.newGlassFish(glassfishProperties);
            glassfish.start();
        }
        
        catch (GlassFishException ex) 
        {
            Logger.getLogger(EmbeddedExample.class.getName()).log(Level.SEVERE, 
                    null, ex);
        }
    }
}

 

 

Note: You won’t be able to drop in Payara Micro quite like this; Payara Micro is based on the GlassFish Web distribution, and using it programmatically works a little differently (See Steve's introductory blog for an example).

That’s it for now, if you need further help with using Payara Embedded, you can find the GlassFish guide for using the embedded server here: 
http://docs.oracle.com/cd/E26576_01/doc.312/e24932/embedded-server-guide.htm#GSESG00001
If you are needing some help with the new Payara Micro, don’t worry, we should have some documentation for it up on our GitHub wiki soon; we’ll let you know when. As always, let us know if there’s any killer feature, improvement, or fix that you want to make it into Payara.

 

 

Comments

Subscribe