spring hazelcast.xml - default config

1,207 views
Skip to first unread message

gise...@yachay.net

unread,
Mar 20, 2012, 2:11:39 PM3/20/12
to haze...@googlegroups.com
I am using a Spring 2.0- based hazelcast.xml configuration

My spring-based file only has the bean for hz:config (no hazelcast bean). My config bean is defined as:
<hz:config id="navis-cache-config">
...

It works fine for my main application where I do the following in the cache lifecycle:
            final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:hazelcast.xml");
            final Config cfg = (Config) context.getBean("navis-cache-config");
            Hazelcast.init(cfg);

But for testing, I would like to avoid having to put this code snippet in a base class.  Is there something I can change in the spring-based hazelcast.xml, so that
hazelcast uses the config bean defined in it by default?

I noticed that Hazelcast reads my spring-based file using XmlConfigBuilder. and then it creates the configuration using a default:
    public Config build() {
        Config config = new Config();
        config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
        return build(config);
    }

So pretty much, what I defined in the file (only one hz:config bean) is ignored completely.


Thanks.



Barry Lagerweij

unread,
Mar 20, 2012, 8:53:48 PM3/20/12
to haze...@googlegroups.com
Why would you want to use the default hazelcast instance (and the Hazelcast.* public static methods) when you're using Spring? Using dependency injection is much more elegant.

Barry






--
You received this message because you are subscribed to the Google Groups "Hazelcast" group.
To view this discussion on the web visit https://groups.google.com/d/msg/hazelcast/-/XyWZZRwftE0J.
To post to this group, send email to haze...@googlegroups.com.
To unsubscribe from this group, send email to hazelcast+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/hazelcast?hl=en.

Mehmet Dogan

unread,
Mar 21, 2012, 3:35:32 AM3/21/12
to haze...@googlegroups.com
Definitely, if you are already using Spring, you should really think about doing all using dependency injection.

If you really need default instance (maybe in some places outside of Spring context), you should create it as Spring bean.
<bean id="hazelcast" class="com.hazelcast.core.Hazelcast" factory-method="init" destroy-method="shutdown">
    <constructor-arg index="0" ref="navis-cache-config"/>
</bean>

Another way of accessing an instance outside of Spring context is giving a name to HazelcastInstance;

<hz:hazelcast id="instance">
        <hz:config>
            <hz:instance-name>my-instance</hz:instance-name> 
       ............
       </hz:config>
</hz:hazelcast>

and then retrieving that instance by Hazelcast.getHazelcastInstanceByName("my-instance").


PS: Spring configuration is not read by XmlConfigBuilder; it is used for Hazelcast config xml. Spring xml is parsed by com.hazelcast.spring.HazelcastConfigBeanDefinitionParser.

@mmdogan

gise...@yachay.net

unread,
Mar 21, 2012, 5:57:17 PM3/21/12
to haze...@googlegroups.com
thank you for the response.

1. Our applications uses lazy-initialization.
For the example below, I would have to load the spring-based hazelcast.xml file first, and getBean("navis-cache-config") so that I do not get an error
saying that it does not exist.

2. The method shutdown() has been deprecated since 1.9, so
destroy-method="shutdown"
will this work fine? We use lifecycles, so we have disabled the shutdown hook, and use the Hazelcast LifecycleService to shutdown hazelcast.

3. Calling

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:hazelcast.xml");
Hazelcast.getHazelcastInstanceByName("my-instance");

works but this does not make this instance the default one. When context is created, the instance defined is also created, so I cannot
make it the default afterwards. I get an error.

4. We use Hibernate 2nd level cache, and HazelcastCacheRegionFactory is looking for the defaultInstance.  So, I need to make my instance the default one.

5. Somehow, I was expecting that Hazelcast would identify what type of definition the configuration file is,  whether config or spring-based; if a spring-based one, it will look for some default bean id (as specified by the api/manual) to load the default instance.

@mmdogan




To unsubscribe from this group, send email to hazelcast+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/hazelcast?hl=en.

--
You received this message because you are subscribed to the Google Groups "Hazelcast" group.
To post to this group, send email to haze...@googlegroups.com.
To unsubscribe from this group, send email to hazelcast+unsubscribe@googlegroups.com.

Mehmet Dogan

unread,
Mar 22, 2012, 12:28:11 PM3/22/12
to haze...@googlegroups.com

If all your need for default HazelcastInstance is to use it as Hibernate L2 cache then try this ;

<hz:hibernate-region-factory id="regionFactory" instance-ref="hazelcast" />

<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.annotation. AnnotationSessionFactoryBean">

<property name="cacheRegionFactory" ref="regionFactory"/>

...
</bean>

To view this discussion on the web visit https://groups.google.com/d/msg/hazelcast/-/SOlFIahHr5kJ.

To post to this group, send email to haze...@googlegroups.com.
To unsubscribe from this group, send email to hazelcast+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages