Setting up HazelCast client and Spring CacheManager.

1,538 views
Skip to first unread message

kotakonda vinay kumar

unread,
Dec 16, 2013, 2:12:49 AM12/16/13
to haze...@googlegroups.com

Hi,

I am trying to setup hazelcast cache with client .

I have setup hazelcast cluster on 3 linux machines node1 , node2 , node3 .

I have included hazelcast-3.1.2.jar , hazelcast-client-3.1.2.jar, hazelcast-spring-3.1.2.jar to my project.

 

 

First one  I have set up hazel cast client and checked inserting map and getting map , they are working fine.

The below changes I have made for client setup .

1.       I have set up hazelcast-context.xml spring configuration file like below

 

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:hz="http://www.hazelcast.com/schema/spring"

       xmlns:context="http://www.springframework.org/schema/context"

       xsi:schemaLocation="

    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd

       http://www.springframework.org/schema/context       http://www.springframework.org/schema/context/spring-context.xsd

       http://www.hazelcast.com/schema/spring  http://www.hazelcast.com/schema/spring/hazelcast-spring-3.1.xsd">

 

       <context:component-scan base-package="com.company.hazelcast" />

       <mvc:annotation-driven />

       <mvc:resources location="/WEB-INF/resource/" mapping="/resource/**" />

       <mvc:resources location="file:#{systemProperties['project.module.home']}" mapping="/media/**" />

       <context:annotation-config />

<hz:client id="client">

              <hz:group name="user" password="pwd"/>

              <hz:network>

              <hz:member>node1:5701, node2:5701, node3:5701</hz:member>

              </hz:network>

</hz:client>

</beans>

 

I have a HazelCastNode1Test Class like below.

 

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = { "classpath:hazelcast-context.xml"})

public class HazelCastNode1Test {

      

       @Autowired

       private HazelcastInstance instance;

 

       @Before

       public void setUp() throws Exception {

              System.out.println("iNSTANCE name "+instance.getName());

       }

 

       @Test

       public void testHazelCastInstanceupdate() {

              Map<Integer, String> mapTestUsers = instance.getMap("testusers1");

              mapTestUsers.put(1, "user1");

              mapTestUsers.put(2, "user2");

              mapTestUsers.put(3, "user3");

              System.out.println("Test Users with key 1: "+ mapTestUsers.get(1));

              System.out.println("Test Users with key 2: "+ mapTestUsers.get(2));

              System.out.println("Test Users with key 3: "+ mapTestUsers.get(3));

              System.out.println("Node1 Map Size:" + mapTestUsers.size());

              }

 

@Test

       public void testHazelCastInstanceupdate() {

              Map<Integer, String> mapTestUsers = instance.getMap("testusers1");

              System.out.println("Test Users with key 1: "+ mapTestUsers.get(1));

              System.out.println("Test Users with key 2: "+ mapTestUsers.get(2));

              System.out.println("Test Users with key 3: "+ mapTestUsers.get(3));

              System.out.println("Node2 Map Size:" + mapTestUsers.size());

              }

 

}

 

I can see hz.client_0_user instance name and the data is cached properly .

 

 

Now I am trying to configure cacheManager .

 

I have setup spring configuration like below (hazelcast-cache-context.xml):

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:hz="http://www.hazelcast.com/schema/spring"

       xmlns:cache="http://www.springframework.org/schema/cache"

       xmlns:context="http://www.springframework.org/schema/context"

       xsi:schemaLocation="

    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd

       http://www.springframework.org/schema/context       http://www.springframework.org/schema/context/spring-context.xsd

       http://www.hazelcast.com/schema/spring  http://www.hazelcast.com/schema/spring/hazelcast-spring-3.1.xsd

       http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

 

       <context:component-scan base-package="com.company.hazelcast" />

       <mvc:annotation-driven />

       <mvc:resources location="/WEB-INF/resource/" mapping="/resource/**" />

       <mvc:resources location="file:#{systemProperties['project.module.home']}" mapping="/media/**" />

       <context:annotation-config />

   <!-- 

       <hz:client id="client">

              <hz:group name="user" password="pwd"/>

              <hz:network>

              <hz:member>node1:5701, node2:5701, node3:5701</hz:member>

              </hz:network>

       </hz:client>

         -->

        

        <hz:hazelcast id="instance">

        <hz:config>

            <hz:group name="user" password=”pwd"/>

            <hz:network port="5701" port-auto-increment="false">

                <hz:join>

                    <hz:multicast enabled="false" />

                    <hz:tcp-ip enabled="true">

                        <hz:members>node1, node2, node3</hz:members>

                    </hz:tcp-ip>

                </hz:join>

            </hz:network>

           

        </hz:config>

    </hz:hazelcast>

 

       <bean id="cacheManager" class="com.hazelcast.spring.cache.HazelcastCacheManager">

    <constructor-arg ref="instance"/>

    </bean>

</beans>

 

I have Test Class for Cache like below.

 

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = { "classpath:hazelcast-cache-context.xml"})

@EnableCaching

public class HazelCastCacheTest {

      

      

       @Cacheable(value =  "hazelcache" , key="#data")

       public boolean insertDataIntoCache(String data) {

           System.out.println("Inserting data = "+ data + " into cache");

           return true;

       }

      

      

       @Test

       public void testHazelCastInstanceupdate() {

              insertDataIntoCache("somecache");

              insertDataIntoCache("somecache1");

              insertDataIntoCache("somecache2");

       }

 

       @Test

       public void testHazelCastInstanceupdate1() {

              insertDataIntoCache("somecache");

              insertDataIntoCache("somecache1");

              insertDataIntoCache("somecache2");

       }

 

}

 

After this configuration, I ran HazelCastCacheTest I can see my localhost:5701 is added to the cluster (which I don’t want) and I can see “Inserting data somecache into cache“ is getting printed all the time.

I tried to configure hazelcast-cache-context.xml with only hz:client but not able to pass hazelcastinstance for constructer-arg in cacheManager bean id .

 

Please help me setting up cacheManager with hazelcast client or hazelCastInstance without including my localhost just node1:5701 ,node2:5702,node3:5701.

 

 



--
Thanks and Regards ,

Kotakonda Vinay Kumar.

kotakonda vinay kumar

unread,
Dec 16, 2013, 6:28:03 AM12/16/13
to haze...@googlegroups.com
Hi,
I have resolved myself this one.

I have changed my  hazelcast-cache-context.xml like below one .

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

    <context:component-scan base-package="com.company.hazelcast" />
    <mvc:annotation-driven />
    <mvc:resources location="/WEB-INF/resource/" mapping="/resource/**" />
    <mvc:resources location="file:#{systemProperties['project.module.home']}" mapping="/media/**" />
    <context:annotation-config />
   
    <hz:client id="client">
        <hz:group name="user" password="pwd"/>
        <hz:network>
            <hz:member>node1:5701</hz:member>
        </hz:network>
    </hz:client>
 

        <bean id="cacheManager" class="com.hazelcast.spring.cache.HazelcastCacheManager">
    <constructor-arg ref="client"/>
    </bean>
</beans>

and added
public interface CacheTestService {
    public boolean insertDataIntoCache(String data);
}


@Component
@EnableCaching
public class CacheTestServiceImpl implements CacheTestService {

    @Override

    @Cacheable(value =  "hazelcache" , key="#data")
    public boolean insertDataIntoCache(String data) {
        System.out.println("Inserting data = "+ data + " into cache");
        return true;
    }
   
}

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:hazelcast-cache-context.xml"})
@EnableCaching
public class HazelCastCacheTest {
   
    @Autowired
    private CacheTestService cacheTestService;

    @Test
    public void testHazelCastInstanceupdate() {
        cacheTestService.insertDataIntoCache("somecache3");
        cacheTestService.insertDataIntoCache("somecache3");
        cacheTestService.insertDataIntoCache("somecache3");
    }

    @Test
    public void testHazelCastInstanceupdate1() {
        cacheTestService.insertDataIntoCache("somecache3");
        cacheTestService.insertDataIntoCache("somecache3");
        cacheTestService.insertDataIntoCache("somecache3");
    }

}

      Now it the method insertDataIntoCache has been executed only once and i am able to connect to node1,node2,node3.



Thanks.

it's closed.

Reply all
Reply to author
Forward
0 new messages