Using the Data Grid in your Applications

The following sections will detail how to use the underlying Data Grid Hazelcast instance in Payara within your code.

Accessing the JNDI registered Hazelcast instance

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 Hazelcast package, you will need to set the Payara Hazelcast package as a dependency in the project pom.xml file (for Maven projects), or have the Hazelcast JAR set as a project dependency (if using a non-Maven based project). To add the Payara Hazelcast package as a dependency in a POM, enter the following in the dependencies section:

<dependency>
  <groupId>fish.payara.api</groupId>
  <artifactId>payara-api</artifactId>
  <version>5.2021.4</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 packaged with Payara. The JAR packaged with Payara can be found at <Payara Installation Directory>/glassfish/modules/hazelcast.jar.

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

Context ctx = new InitialContext();
HazelcastInstance instance = (HazelcastInstance) ctx.lookup("payara/Hazelcast");

You will have to wrap this in a try-catch clause, or throw the NamingException exception that this code can generate.

Using Hazelcast for Web and EJB Container Persistence

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

Setting Hazelcast as the Persistence provider through the Admin Console

Note on Payara Server 5.181 hazelcast is the default persistence provider.

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 using Asadmin

To configure the persistence provider with asadmin, 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:

asadmin> set ${Cluster-Config}.availability-service.ejb-container-availability.sfsb-ha-persistence-type=hazelcast