Hazelcast Spring cache manager starting with two members

580 views
Skip to first unread message

rodrigo

unread,
Jan 9, 2014, 4:15:56 PM1/9/14
to haze...@googlegroups.com
I have several spring hazelcast configuration files distributed on folders equivalent to their environment location (dev, qa, etc) and my framework has a utility that provides the url to those files. Based on that, what I'm trying to do it is, load the HazelcastCacheManager bean from a Spring Configuration class so I can dynamically obtain a Config file using FileSystemXmlConfig. Something like this:

    @Bean(name="cacheManager")
    public HazelcastCacheManager hazelcastCacheManager() {
       
        String cacheConfigFileLocation = myUtilityClass().getString("cache.config");     
        Config config = null;
        try {
            config = new FileSystemXmlConfig(cacheConfigFileLocation );
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
       
        HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(cacheConfigFileLocation );
        // this ended up creating two members
        HazelcastCacheManager cacheManager = new HazelcastCacheManager(hazelcastInstance);
        // same here
        //cacheManager.setHazelcastInstance(hazelcastInstance);
        return cacheManager;
    }

This is my config file:

<?xml version="1.0" encoding="UTF-8"?>
<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-3.2.xsd
        http://www.hazelcast.com/schema/spring
        http://www.hazelcast.com/schema/spring/hazelcast-spring-3.1.xsd">

    <hz:hazelcast id="hazelcast">
        <hz:config>
            <hz:group name="dev-user" password="dev-pass" />
            <hz:network port="5701" port-auto-increment="true">
                <hz:join>
                    <hz:multicast enabled="false" />
                    <hz:tcp-ip enabled="true">
                        <hz:member>127.0.0.1</hz:member>
                    </hz:tcp-ip>
                </hz:join>
            </hz:network>
        </hz:config>
    </hz:hazelcast>
</beans>

The problem is, Hazelcast instance gets created with TWO members and I only need one for obvious reasons. Any thoughts ?

Thanks!


Christoph Engelbert

unread,
Jan 9, 2014, 4:18:08 PM1/9/14
to haze...@googlegroups.com
Currently this seems to be only possible with Hibernate 3 but not 4 since the factory bean does not support setting an external cache manager =(

rodrigo

unread,
Jan 9, 2014, 4:23:51 PM1/9/14
to haze...@googlegroups.com
Sorry but I didn't get it, in my case all I'm doing is Spring cache integration via code instead of something like this:

<bean id="cacheManager" class="com.hazelcast.spring.cache.HazelcastCacheManager"
        p:hazelcastInstance-ref="hazelcast" />

Christoph Engelbert

unread,
Jan 9, 2014, 4:36:07 PM1/9/14
to haze...@googlegroups.com
Yeah that works on Hibernate 3. So maybe I misunderstood your question.

If you explicitly create a second instance for sure you get two instances:

HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(cacheConfigFileLocation );

Am Donnerstag, 9. Januar 2014 22:15:56 UTC+1 schrieb rodrigo:

rodrigo

unread,
Jan 9, 2014, 4:49:10 PM1/9/14
to haze...@googlegroups.com
do you know how do I load a hazelcast config file and pass it to the cache manager ?


    @Bean(name="cacheManager")
    public HazelcastCacheManager hazelcastCacheManager() {   
        HazelcastCacheManager cacheManager = new HazelcastCacheManager(?);
        return cacheManager;
    }

Thanks for your replies !!

Mehmet Dogan

unread,
Jan 9, 2014, 5:15:13 PM1/9/14
to haze...@googlegroups.com

There are two members because one is created by spring. Following bean definition creates a member ;

 <hz:hazelcast id="hazelcast">
....

And other one is created explicitly in hazelcastCacheManager() method.

HazelcastCacheManager never creates its own member, it only uses the member injected via constructor or setter method.

You should either remove hazelcast bean definition or remove explicit HazelcastInstance creation in hazelcastCacheManager() method, and use only one of them.

@mmdogan

~Sent from mobile

--
You received this message because you are subscribed to the Google Groups "Hazelcast" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hazelcast+...@googlegroups.com.
To post to this group, send email to haze...@googlegroups.com.
Visit this group at http://groups.google.com/group/hazelcast.
To view this discussion on the web visit https://groups.google.com/d/msgid/hazelcast/5f78ea2c-7370-46ae-9991-66e1c1867537%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

rodrigo

unread,
Jan 10, 2014, 1:17:21 PM1/10/14
to haze...@googlegroups.com
Thanks Mehmet! I don't think Spring starts hazelcast when loading the FileSystemXmlConfig but I did found that the spring context was starting the cache manager twice and thus creating the second member instance. I ended up changing the approach a little bit to avoid that.

Thanks all for the help!

Noctarius

unread,
Jan 10, 2014, 1:58:41 PM1/10/14
to haze...@googlegroups.com
You start a second node with:
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(cacheConfigFileLocation ); 
Reply all
Reply to author
Forward
0 new messages