我有一个grails项目,我想在其中配置Hazelcast。但是我做不到。我已经下载了hazelcast并添加到lib文件夹。然后我尝试在资源文件
中初始化hazelcastConfigurer(MethodInvokingFactoryBean){
targetClass = "com.hazelcast.core.Hazelcast"
targetMethod = "init"
Config hazelcastConfig = new Config("hazelcast.xml")
arguments = hazelcastConfig
}
它只是不编译并抛出错误
[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
是否有任何博客或网站可以帮助我在grails项目中配置?
下面是我如何在Grails 2.1.2应用程序中配置Hazelcast 3.2以使用Spring缓存注释:
BuildConfig.groovy
dependencies {
...
compile "com.hazelcast:hazelcast:3.2"
compile "com.hazelcast:hazelcast-spring:3.2"
}
Hazelcast实例配置,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>
启用Spring缓存注释,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>
将它们连接在一起- Spring缓存注释使用的缓存管理器bean配置为Hazelcast实例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'))
}
现在您可以使用Spring缓存注释,如@Cacheable
和@CacheEvict
与Hazelcast缓存:
@Cacheable(value = 'myCache')
SomethingDto getSomethingDto(Long id) {
SomethingDto dto = convertToSomethingDto(Something.get(id))
return dto
}
我通常将域对象转换为dto,因为如果将普通域对象存储到缓存中,如果惰性获取它的某些属性可能会导致问题。
一些笔记:
- 我把Spring XML文件放到
src/java
中,因为在grails-app/conf/spring
中它们没有在生产中找到,参见从grails应用程序中的自定义groovy文件加载Spring bean - 可以在
resources.groovy
中使用环境块实现每个环境的Hazelcast配置。我使用了它,但为了简单起见,我没有在这里发布。
如果您使用的是Hazelcast 3.0版本,它应该是targetMethod = "newHazelcastInstance"
而不是targetMethod = "init"
。