baliman
baliman

Reputation: 620

Hazelcast client spring configuration

I have createed a Hazelcast manager which uses spring, hibernate, jpa. I can start my hazelcast instance.

The problem I have is I dont know how to configure a hazelcast client using spring config. I want to use in some other server component a hazelcast-client

I really have no idea how to start

any help would be appreciated

Below is my spring config for hazelcast server

Johan

<?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"
       xmlns:tx="http://www.springframework.org/schema/tx" 
       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/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.hazelcast.com/schema/spring
                           http://www.hazelcast.com/schema/spring/hazelcast-spring.xsd">

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>file:${ecs.config.path}/ecs.properties</value>
                <value>classpath*:config/ecs.properties</value>
            </list>
        </property> 
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="ignoreResourceNotFound" value="true" />
    </bean>

    <context:component-scan base-package="nl.ict.psa.ecs.hazelcast.dao,nl.ict.psa.ecs.hazelcast.mapstores,nl.ict.psa.ecs.hazelcast.service" />

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${datasource.driverClassName}" />
        <property name="url" value="${datasource.url}" />
        <property name="username" value="${datasource.username}"/>
        <property name="password" value="${datasource.password}"/>
    </bean>

    <bean id="hazelcast" class="com.hazelcast.core.Hazelcast"/>

    <bean id="entityManagerFactoryBean"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

        <property name="dataSource" ref="dataSource" />
        <property name="persistenceUnitName" value="PU" />
        <property name="packagesToScan">
            <list>
                <value>nl.ict.psa.hazelcast.model.ecs</value>
                <value>nl.ict.psa.hazelcast.model.ecs.ocr</value>
            </list>
        </property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.archive.autodetection">class</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            </props>
        </property>
    </bean>

    <tx:annotation-driven />

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactoryBean" />
    </bean>

    <bean id="transpInfo" class="nl.ict.psa.ecs.hazelcast.mapstores.TranspInfoMapStore"/>

    <hz:hazelcast id="instance">
        <hz:config>
            <hz:network port="5701" port-auto-increment="true">
                <hz:join>
                    <hz:multicast enabled="false"/>
                </hz:join>
            </hz:network>

            <hz:map name="transp" read-backup-data="true">
                <hz:map-store enabled="true" write-delay-seconds="60"
                              initial-mode="LAZY"
                              implementation="transpInfo"/>
            </hz:map>
        </hz:config>
    </hz:hazelcast>

</beans>

Upvotes: 0

Views: 8934

Answers (2)

Neil Stevenson
Neil Stevenson

Reputation: 3150

Something like this (from https://github.com/neilstevenson/spring-boot-autoconfigure-test/tree/master/hazelcast-imdg-client)

@Configuration
@ConditionalOnMissingBean(ClientConfig.class)
static class HazelcastClientConfigConfiguration {

    @Bean
    public ClientConfig clientConfig() throws Exception {
            return new XmlClientConfigBuilder().build();
    }
}

@Configuration
@ConditionalOnMissingBean(HazelcastInstance.class)
static class HazelcastClientConfiguration {

    @Bean
    public HazelcastInstance hazelcastInstance(ClientConfig clientConfig) {
        return HazelcastClient.newHazelcastClient(clientConfig);
    }
}

Try to avoid XML, Spring is moving away from it.

Upvotes: 1

Kumar Abhishek
Kumar Abhishek

Reputation: 3124

Configured Multiple Ip's Using Pragmatically.

@Bean
public ClientConfig clientConfig() {
  ClientConfig clientConfig = new ClientConfig();
  ClientNetworkConfig networkConfig = clientConfig.getNetworkConfig();
  networkConfig.addAddress("172.17.0.4:5701", "172.17.0.6:5701")
    .setSmartRouting(true)
    .addOutboundPortDefinition("34700-34710")
    .setRedoOperation(true)
    .setConnectionTimeout(5000)
    .setConnectionAttemptLimit(5);

  return clientConfig;

}

@Bean
public HazelcastInstance hazelcastInstance(ClientConfig clientConfig) {
  return HazelcastClient.newHazelcastClient(clientConfig);
}

I would suggest to must read. http://docs.hazelcast.org/docs/latest/manual/html-single/index.html#configuring-client-connection-strategy

Upvotes: 5

Related Questions