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 Hazelcast package, 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:
Context 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:
-
Select the instance’s configuration from the page tree:
-
Select "Availability Service" to view the "Availability Service" page:
Setting Hazelcast as the Persistence provider using Asadmin
To configure the persistence provider with the asadmin CLI, you have to use the set command.