Using the Data Grid in your Applications

This section details how to use the underlying Data Grid Hazelcast instance in Payara Server within application code.

Accessing the Hazelcast instance via JNDI

By default, the JNDI name of the hazelcast instance is payara/Hazelcast, though this can be altered using the set-hazelcast-configuration command. You will need to import the following packages into your Java class:

import com.hazelcast.core.HazelcastInstance;
import javax.naming.Context;
import javax.naming.InitialContext;

To import the requisite Hazelcast classes, you will need to set add the Public API artefact as a dependency in the project’s pom.xml file (in case of Maven projects), or have the Hazelcast JAR set as a project dependency (if using a non-Maven based project).

Here’s the Maven dependency:

<dependency>
  <groupId>fish.payara.api</groupId>
  <artifactId>payara-api</artifactId>
  <version>${page-version}</version>
</dependency>

The Hazelcast JAR (for non-Maven projects), can either be downloaded from the Hazelcast website, or you can make use of the JAR that is packaged in Payara Server. The JAR packaged with Payara can be found at <Payara Installation Directory>/glassfish/modules/hazelcast.jar.

The following code sample will initialize a HazelcastInstance variable in the instance embedded in Payara:

public class Component{

    public void businessMethod(){
        var ctx = new InitialContext();
        HazelcastInstance instance = (HazelcastInstance) ctx.lookup("payara/Hazelcast");
    }
}
It is recommended to wrap this block in a try-catch clause, since a NamingException exception can be generated due to the use of JNDI.

Using Hazelcast for Web and EJB Container Persistence

Hazelcast can be used as the persistence provider for both the Web and EJB containers.

Setting Hazelcast as the Persistence provider through the Admin Console

Payara Server’s initial configuration has the hazelcast option as the default setting.

To set up Hazelcast for persistence:

  1. Select the instance’s configuration from the page tree:

  2. Select "Availability Service" to view the "Availability Service" page:

    Availability Service

Setting Web Persistence

  1. Open the "Web Container Availability" tab, and select "Hazelcast" from the Persistence Type drop-down menu:

    Web Persistence
  2. Save the changes.

Setting EJB Persistence

  1. Open the "EJB Container Availability" tab and select "Hazelcast" from the Persistence Type drop-down menu:

    EJB Persistence
  2. Save the changes.

Setting Hazelcast as the Persistence provider from the Command Line

To configure the persistence provider with the asadmin CLI, you have to use the set command.

For the Web Container

To set Hazelcast as the persistence provider of the Web Container, run the following command:

asadmin> set <cluster-config>.availability-service.web-container-availability.persistence-type=hazelcast

For the EJB Container

To set Hazelcast as the persistence provider of the EJB Container, run the following command:

asadmin> set <cluster-config>.availability-service.ejb-container-availability.sfsb-ha-persistence-type=hazelcast