1

I have a grails project and I want to configure Hazelcast in it. But I am not been able to do it. I have downloaded the hazelcast and added to lib folder. Then I tried to initialize in resource file

hazelcastConfigurer(MethodInvokingFactoryBean){
        targetClass = "com.hazelcast.core.Hazelcast"
        targetMethod = "init"
        Config hazelcastConfig = new Config("hazelcast.xml")
        arguments = hazelcastConfig
}

It just doesnot compile and throws error

[2014-06-04 22:08:25,293] - context.GrailsContextLoader Error initializing the a
pplication: Error creating bean with name 'hazelcastConfigurer': Invocation of i
nit method failed; nested exception is java.lang.NoSuchMethodException: com.haze
lcast.core.Hazelcast.init(com.hazelcast.config.Config)
org.springframework.beans.factory.BeanCreationException: Error creating bean wit
h name 'hazelcastConfigurer': Invocation of init method failed; nested exception
 is java.lang.NoSuchMethodException: com.hazelcast.core.Hazelcast.init(com.hazel
cast.config.Config)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
        at java.util.concurrent.FutureTask.run(FutureTask.java:166)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.
java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor
.java:615)
        at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.NoSuchMethodException: com.hazelcast.core.Hazelcast.init(co
m.hazelcast.config.Config)
        at java.lang.Class.getMethod(Class.java:1624)
        ... 5 more

Is there any blog or site that helps me to configure in grails project?

Biswas
  • 598
  • 2
  • 16
  • 34
  • Im in the same situation but i started by trying hazelgrails plugin ... does makes sense? ... my goal is just use it for hibernate caching – Rafael Feb 18 '15 at 16:12

2 Answers2

2

Here is how I configured Hazelcast 3.2 in Grails 2.1.2 app for usage with Spring cache annotations:

BuildConfig.groovy

dependencies {
    ...

    compile "com.hazelcast:hazelcast:3.2"
    compile "com.hazelcast:hazelcast-spring:3.2"
}

Hazelcast instance configuration, src/java/spring/hazelcast.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:hz="http://www.hazelcast.com/schema/spring"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context-3.1.xsd
                http://www.hazelcast.com/schema/spring
                http://www.hazelcast.com/schema/spring/hazelcast-spring-3.2.xsd">

    <hz:hazelcast id="hazelcastInstance">
        <hz:config>

            <hz:map name="myCache"
                    backup-count="0"
                    in-memory-format="OBJECT"
                    >
            </hz:map>

        </hz:config>
    </hz:hazelcast>

</beans>

Enabling Spring cache annotations, src/java/spring/spring-cache.xml:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

    <cache:annotation-driven/>

</beans>

Wiring it all together - cache manager bean used by Spring cache annotations is configured with Hazelcast instance bean, grails-app/conf/spring/resources.groovy:

beans = {          

        importBeans('classpath:/spring/spring-cache.xml')              
        importBeans('classpath:/spring/hazelcast.xml')

        cacheManager(com.hazelcast.spring.cache.HazelcastCacheManager, ref('hazelcastInstance'))                       
}

Now you can use Spring cache annotations like @Cacheable and @CacheEvict together with Hazelcast cache:

    @Cacheable(value = 'myCache')
    SomethingDto getSomethingDto(Long id) {
        SomethingDto dto = convertToSomethingDto(Something.get(id))
        return dto
    }

I usually convert domain objects to DTOs because storing plain domain object into cache can cause problems if some of it's properties are lazily fetched.

Few notes:

  • I put Spring XML files into src/java because in grails-app/conf/spring they were not found in production, see Load spring beans from custom groovy files in grails app
  • You can achieve per environment Hazelcast configuration with environment blocks in resources.groovy. I use that but for the sake of simplicity I didn't post it here.
Community
  • 1
  • 1
lukelazarovic
  • 1,510
  • 11
  • 19
  • This works :) Thanks. I also want to use hazelcast for sessions. Do you have any idea on that. – Biswas Jun 05 '14 at 15:44
  • Session data are already stored in-memory of the application server, is there any benefit from storing them in cache? – lukelazarovic Jun 05 '14 at 19:19
  • I would like to add session data in hazelcast. Assume that you have more than one web servers (A, B, C) with a load balancer in front of them. If server A goes down, your users on that server will be directed to one of the live servers (B or C), but their sessions will be lost. So we have to have all these sessions backed up in hazelcast if we do not want to lose the sessions upon server crashes. Do you know how to configure it? – Biswas Jun 06 '14 at 15:19
  • http://www.hazelcast.org/docs/3.2/manual/html-single/hazelcast-documentation.html#http-session-clustering-with-hazelcast-wm – lukelazarovic Jun 07 '14 at 11:46
  • I want to use an external hazelcast ... is that possible with this configuration? – Rafael Feb 18 '15 at 16:15
  • @Biswas see if [this stackoverflow post](http://stackoverflow.com/questions/35485124/web-session-replication-with-hazelcast-in-grails-hazelcastserializationexcepti) can help. – Ibrahim Feb 19 '16 at 19:34
1

If you're using Hazelcast version 3.0 it should be targetMethod = "newHazelcastInstance" instead of targetMethod = "init".

RAS
  • 8,100
  • 16
  • 64
  • 86