Getting started with Hazelcast using Grails in 10 minutes
In computing, Hazelcast is an open source in-memory data grid based on Java. This blog will help you to tune the performance of your Grails application using Hazelcast.
Integrating Hazelcast with Grails
Step 1: Setup Hazelcast server using server.sh (Packaged within Hazelcast zipped bundle):
- Download the Hazelcast .zip from this link http://hazelcast.org/download/
- Execute server.sh from hazelcast-<version>/bin
- This will start Hazelcast cluster.
You will see something like this when you execute server.sh.
\
Voila !! this is your first Hazelcast cluster.
Step 2: Setting up Hazelcast client:
- Add dependency in your BuildConfig.groovy:
compile("com.hazelcast:hazelcast-client:3.5.2")
- Create a HazelcastClient.groovy file in your Grails application’s
~/src/groovy
:
class HazelcastClient {
private static HazelcastInstance _client
public static HazelcastInstance getClient() {
if (!(_client != null && _client.getLifecycleService().isRunning()))
{
ClientConfig clientConfig = new ClientConfig()
clientConfig.getNetworkConfig().addAddress(HAZELCAST SERVER URL)
_client = com.hazelcast.client.HazelcastClient.newHazelcastClient(clientConfig)
}
return _client
}
}
This getClient()
method will create a Hazelcast client and connects to Hazelcast server in HAZELCAST SERVER URL
Step 3: Populating data in Hazelcast:
IMap<Integer, String> map=getClient().getMap(<YOUR MAP NAME>)
map.put(1,"BOB")
map.put(2,"ROY")
Step 4: Fetching data from Hazelcast :
IMap<Integer, String> map=getClient().getMap(<YOUR MAP NAME>)
map.get(1) // "BOB"
Hope this will help!!
Also, there’s a lot about Grails 3.0 going the rounds in the Grails community, here’s a compilation of latest TOP 8 updates on Grails 3.0 –
For more reference: visit https://hazelcast.com/