Hazelcast Session Clustering w/ Spring Configuration

1,176 views
Skip to first unread message

Matthew Tamayo-Rios

unread,
Oct 7, 2013, 6:01:00 PM10/7/13
to haze...@googlegroups.com
I'm trying to get Hazelcast Session Clustering working, but I am using the hazelcast spring namespace for configuration.  The web.xml example only shows how to configure session clustering from hazelcast.xml. 

I have managed to get it working by providing a stripped down version of hazelcast.xml that has the correct group and password information, along with a default map.  Unfortunately, this requires running two embedded instances of hazelcast per node instead of our original design of running only a single embedded instance per node.

Are there anyways to get everything running off a single instance on single node, when using session clustering?

Matthew

Peter Veentjer

unread,
Oct 8, 2013, 12:53:08 AM10/8/13
to haze...@googlegroups.com
Hi Matthew,

I ran into the same thing. Currently it isn't possible, but it shouldn't be difficult to add. I'll have a look at it:

https://github.com/hazelcast/hazelcast/issues/943


--
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.
For more options, visit https://groups.google.com/groups/opt_out.

Matthew Tamayo-Rios

unread,
Oct 8, 2013, 8:08:39 PM10/8/13
to haze...@googlegroups.com
Nice! 

Hopefully, it'll work both ways so that its possible to refer to an instance that is defined in a spring xml file.  Otherwise, I will have to convert my XML file back to hazelcast xml schema from spring schema.  I noticed that the specification mentioned on the issue suggested a util for extracting from the session cache.

I actually don't mind doing the conversion back, but it would be good to have a "preferred"/best practice way of defining hazelcast settings, so if something similar comes up in the future it is easier to make a clear decision.

cyc...@gmail.com

unread,
Oct 15, 2013, 10:29:17 PM10/15/13
to haze...@googlegroups.com
You can use spring delegatingfilterproxy to start the WebFilter

<filter>
        <filter-name>hazelcastWebFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <init-param>
            <param-name>targetFilterLifecycle</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>hazelcastWebFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

and then create the WebFilter instance by using spring, with 
<bean id="hazelcastWebFilter" class="com.hazelcast.web.WebFilter">
 <constructor-arg>
   <props>
     <prop key="client-config-location">/WEB-INF/hazelcast-client.xml</prop>
     <prop key="use-client">true</prop>
     <prop key="map-name">my-session</prop>
     <prop key="sticky-session">false</prop>
     <prop key="cookie-name">hz-session-id</prop>
     <prop key="cookie-secure">false</prop>
     <prop key="cookie-http-only">true</prop>
     <prop key="debug">true</prop>
     <prop key="instance-name">default</prop>
     <prop key="shutdown-on-destroy">true</prop>
   </props>
 </constructor-arg>
</bean>

and then join the instance by client.

Matthew Tamayo-Rios

unread,
Nov 3, 2013, 1:20:23 AM11/3/13
to haze...@googlegroups.com, cyc...@gmail.com
That's an awesome solution and much cleaner than what we wended up doing! This should be the solution in the official docs.

It allows you share the instance name + authentication information, instead of having a separate hazelcast.xml file to define a client to connect to the primary cluster.  I'm planning on switching over to this as soon as I get the chance. 

Peter Veentjer

unread,
Nov 3, 2013, 2:15:23 AM11/3/13
to haze...@googlegroups.com, cyc...@gmail.com
I went for a different solution (only possible with 3.2-SNAPSHOT since a new method is added to Hazelcast -> getOrCreateHazelcastInstance) some time ago. 

I add the following class in my own project:

public class HazelcastInstanceLoader {

    private final static ILogger logger = Logger.getLogger(HazelcastInstanceLoader.class);


    public static HazelcastInstance load(String instanceName) throws Exception {
        String configName = System.getProperty("hazelcast.config");
        if (configName == null) {
            configName = "hazelcast.xml";
        }

        Config config;
        if (configName.startsWith("file:")) {
            String filename = configName.substring("file:".length());
            logger.info("Using hazelcast configuration file: " + filename);
            config = new FileSystemXmlConfig(filename);
        } else {
            logger.info("Using hazelcast classpath resource: " + configName);
            config = new ClasspathXmlConfig(configName);
        }
        config.setInstanceName(instanceName);
        return Hazelcast.getOrCreateHazelcastInstance(config);
    }
}

And configured this in Spring like this:

  <bean id="hazelcastInstance"
          class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="staticMethod" value="com.hazelcast.webmonitor.HazelcastInstanceLoader.load"/>
        <property name="arguments">
            <list>
                <value>default</value>
            </list>
        </property>
    </bean>

And the web.xml  is configured like this:

 <filter>
        <filter-name>hazelcast-filter</filter-name>
        <filter-class>com.hazelcast.web.WebFilter</filter-class>
             <init-param>
            <param-name>instance-name</param-name>
            <param-value>default</param-value>
        </init-param>
    </filter>

The webfilter tries to look up the HazelcastInstance with name 'default'. But this is already created by Spring; so it will use that instance.

This way no client is needed, the same instance is shared between the webfilter and the application.

--
Reply all
Reply to author
Forward
0 new messages