Reputation: 132
I have a spring project (spring core 3.1.2) configured with Hibernate (hibernate core 4.2.8) and i want to set up Hazelcast as a 2nd level cache. I want to have the cache distributed in P2P, embedded cluster mode (each application instance runs a hazelcast instance on the same machine).
This is my current sessionFactory configuration.
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" scope="singleton">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.myProject.beans" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.database">ORACLE</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<!--enable batch operations-->
<prop key="hibernate.jdbc.batch_size">20</prop>
<prop key="hibernate.order_inserts">true</prop>
<prop key="hibernate.order_updates">true</prop>
<!-- 2nd level cache configuration-->
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">com.hazelcast.hibernate.HazelcastLocalCacheRegionFactory</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
</props>
</property>
</bean>
This configuration seems to work on my local machine as i ran a small test that checks for a 2nd level cache hit.
The question is: What other configurations must I make in order to have the cache distributed among the instances. How are the different machines going to "know about each other"? Also, is there a way to create a test scenario that checks that the cache is indeed distributed among a couple of machines?(ex: launch 2 jvms) An example would be greatly appreciated.
Any other tips or warnings about this configuration are welcome.
Disclaimer: Its the first time I use Hazelcast.
My Hazelcast version is 3.5.4
Thank you!
Upvotes: 3
Views: 3269
Reputation: 493
You don't need to do anymore config to form a cluster.
Just start another instance of your app and two hazelcast instances should see each other and form a cluster.
By default hazelcast members uses multicast to find each other, of course you can change this behaviour by adding a custom hazelcast.xml
to your project.
Here is a detailed example of Spring-Hibernate-Hazelcast integration.
applicationContext-hazelcast.xml
is the file to modify if you want to play with Hazelcast config.
For example, in this sample project port-auto-increment
is set to false
that means Hazelcast won't start if the specified port is already occupied. (5701 by default)
Just set this property to true
and start another Application
instance, you should see that the cache is distributed.
Please don't forget to comment out last line of Application
class before starting first instance to keep processes alive
Start first instance like below;
public static void main(String[] args) {
InitializeDB.start();
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
DistributedMapDemonstrator distributedMapDemonstrator = context.getBean(DistributedMapDemonstrator.class);
distributedMapDemonstrator.demonstrate();
//Hazelcast.shutdownAll(); Keep instances alive to see form a cluster
}
And second one like below;
public static void main(String[] args) {
//InitializeDB.start(); DB will be initialized already by the first instance
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
DistributedMapDemonstrator distributedMapDemonstrator = context.getBean(DistributedMapDemonstrator.class);
distributedMapDemonstrator.demonstrate();
//Hazelcast.shutdownAll(); Keep instances alive to see form a cluster
}
Upvotes: 3