Using Hazelcast in your Applications

The following sections will detail how to use the Hazelcast embedded 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>4.1.2.174</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 that you can always select Hazelcast as a persistence provider, even if it not yet enabled.

To set up Hazelcast for persistence:

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

    Payara 4.1.2.172 Page Tree
  2. Select "Availability Service" to view the "Availability Service" page:

    Payara 4.1.2.172 Availability Service

Setting Web Persistence

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

    Payara 4.1.2.172 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:

    Payara 4.1.2.172 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

results matching ""

    No results matching ""