You can declare Hazelcast beans for Spring context using beans namespace (default spring beans namespace) as well to declare hazelcast maps, queues and others. Hazelcast-Spring integration requires either hazelcast-spring jar or hazelcast-all jar in the classpath.
<bean id="instance" class="com.hazelcast.core.Hazelcast" factory-method="newHazelcastInstance"> <constructor-arg> <bean class="com.hazelcast.config.Config"> <property name="groupConfig"> <bean class="com.hazelcast.config.GroupConfig"> <property name="name" value="dev"/> <property name="password" value="pwd"/> </bean> </property> <!-- and so on ... --> </bean> </constructor-arg> </bean> <bean id="map" factory-bean="instance" factory-method="getMap"> <constructor-arg value="map"/> </bean>
Hazelcast has Spring integration (requires version 2.5 or greater) since 1.9.1 using hazelcast namespace.
Add namespace xmlns:hz="http://www.hazelcast.com/schema/spring" to beans tag in context file:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:hz="http://www.hazelcast.com/schema/spring" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.hazelcast.com/schema/spring http://www.hazelcast.com/schema/spring/hazelcast-spring-2.0.xsd">
Use hz namespace shortcuts to declare cluster, its items and so on.
After that you can configure Hazelcast instance (node):
<hz:hazelcast id="instance"> <hz:config> <hz:group name="dev" password="password"/> <hz:network port="5701" port-auto-increment="false"> <hz:join> <hz:multicast enabled="false" multicast-group="224.2.2.3" multicast-port="54327"/> <hz:tcp-ip enabled="true"> <hz:members>10.10.1.2, 10.10.1.3</hz:members> </hz:tcp-ip> </hz:join> </hz:network> <hz:map name="map" backup-count="2" max-size="0" eviction-percentage="30" read-backup-data="true" cache-value="true" eviction-policy="NONE" merge-policy="hz.ADD_NEW_ENTRY"/> </hz:config> </hz:hazelcast>
You can easily configure map-store and near-cache too. (For map-store you should set either class-name or implementation attribute.)
<hz:config> <hz:map name="map1"> <hz:near-cache time-to-live-seconds="0" max-idle-seconds="60" eviction-policy="LRU" max-size="5000" invalidate-on-change="true"/> <hz:map-store enabled="true" class-name="com.foo.DummyStore" write-delay-seconds="0"/> </hz:map> <hz:map name="map2"> <hz:map-store enabled="true" implementation="dummyMapStore" write-delay-seconds="0"/> </hz:map> <bean id="dummyMapStore" class="com.foo.DummyStore" /> </hz:config>
It's possible to use placeholders instead of concrete values. For instance, use property file app-default.properties for group configuration:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:/app-default.properties</value> </list> </property> </bean> <hz:hazelcast id="instance"> <hz:config> <hz:group name="${cluster.group.name}" password="${cluster.group.password}"/> <!-- ... --> </hz:config> </hz:hazelcast>
Similar for client
<hz:client id="client" group-name="${cluster.group.name}" group-password="${cluster.group.password}"> <hz:members>10.10.1.2:5701, 10.10.1.3:5701</hz:members> </hz:client>
You can declare beans for the following Hazelcast objects:
map
multiMap
queue
topic
set
list
executorService
idGenerator
atomicNumber
Example:
<hz:map id="map" instance-ref="instance" name="map"/> <hz:multiMap id="multiMap" instance-ref="instance" name="multiMap"/> <hz:queue id="queue" instance-ref="instance" name="queue"/> <hz:topic id="topic" instance-ref="instance" name="topic"/> <hz:set id="set" instance-ref="instance" name="set"/> <hz:list id="list" instance-ref="instance" name="list"/> <hz:executorService id="executorService" instance-ref="instance" name="executorService"/> <hz:idGenerator id="idGenerator" instance-ref="instance" name="idGenerator"/> <hz:atomicNumber id="atomicNumber" instance-ref="instance" name="atomicNumber"/>
If you are using Hibernate with Hazelcast as 2nd level cache provider, you
can easily create
CacheProvider
or
RegionFactory
instances within
Spring configuration. That way it is possible to use same
HazelcastInstance
as Hibernate L2
cache instance.
<hz:hibernate-cache-provider id="cacheProvider" instance-ref="instance" /> ... <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" scope="singleton"> <property name="dataSource" ref="dataSource"/> <property name="cacheProvider" ref="cacheProvider" /> ... </bean>
Or by Spring version 3.1
<hz:hibernate-region-factory id="regionFactory" instance-ref="instance" /> ... <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" scope="singleton"> <property name="dataSource" ref="dataSource"/> <property name="cacheRegionFactory" ref="regionFactory" /> ... </bean>