簡體   English   中英

帶彈簧的Hazelcast配置

[英]Hazelcast configuration with spring

我正在嘗試配置Hazelcast(3.5.4)作為Hibernate(4.2.8)和Spring(3.1.2)Cache的二級緩存。

我添加了dependecies:

        <dependency>
            <groupId>com.hazelcast</groupId>
            <artifactId>hazelcast-hibernate4</artifactId>
            <version>${hazelcast-version}</version>
        </dependency>

        <dependency>
            <groupId>com.hazelcast</groupId>
            <artifactId>hazelcast</artifactId>
            <version>${hazelcast-version}</version>
        </dependency>

        <dependency>
            <groupId>com.hazelcast</groupId>
            <artifactId>hazelcast-spring</artifactId>
            <version>${hazelcast-version}</version>
        </dependency>

使用文檔作為參考我已經配置了sessionFactory,如下所示:

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

    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.somePackage" />
        <property name="cacheRegionFactory" ref="regionFactory" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.database">ORACLE</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <!--enable 2nd level cache-->
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.use_query_cache">false</prop>
            </props>
        </property>
    </bean>

不幸的是,這不起作用,因為我得到以下異常:

Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'cacheRegionFactory' of bean class [org.springframework.orm.hibernate4.LocalSessionFactoryBean]: Bean property 'cacheRegionFactory' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

我意識到文檔示例使用了Hibernate 3。

什么是Hibernate 4的適當配置?

這是我的hazelcast實例配置文件:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   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.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.hazelcast.com/schema/spring
            http://www.hazelcast.com/schema/spring/hazelcast-spring-3.5.xsd">

<context:annotation-config />

<hz:hazelcast id="instance">
    <hz:config>
        <hz:spring-aware />
        <hz:group name="dev" password="password"/>
        <hz:network port="5701" port-auto-increment="true">
            <hz:join>
                <hz:multicast enabled="true"
                              multicast-group="224.2.2.3"
                              multicast-port="54327"/>
            </hz:join>
        </hz:network>
        <hz:map name="default"
                in-memory-format="BINARY"
                backup-count="1"
                async-backup-count="0"
                time-to-live-seconds="0"
                max-idle-seconds="0"
                eviction-policy="NONE"
                max-size="0"
                max-size-policy="PER_NODE"
                eviction-percentage="30"
                min-eviction-check-millis="100"
                merge-policy="com.hazelcast.map.merge.PutIfAbsentMapMergePolicy"/>
    </hz:config>
</hz:hazelcast>

謝謝 !


稍后編輯

使用İbrahimGürses建議似乎修復了未定義的屬性錯誤,但出現了一個新問題:

如果我將hazelcast配置文件命名為hazelcast.xml,則會收到java.lang.IllegalStateException: Failed to load ApplicationContext由以下原因引起的java.lang.IllegalStateException: Failed to load ApplicationContext

Caused by: com.hazelcast.config.InvalidConfigurationException: Your xsd schema couldn't be load

如果我給它一個不同的名稱,例如hazelcast-config.xml,我會得到相同的異常,這次由以下原因引起:

Caused by: java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask@2cd62003 rejected from java.util.concurrent.ScheduledThreadPoolExecutor@61ab89b0[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 7]

我在sessionFactory配置文件之前導入( <import resource="" /> )我的hazelcast配置文件。

您獲得異常的原因是org.springframework.orm.hibernate4.LocalSessionFactoryBean未定義cacheRegionFactory 您可以在不使用cacheRegionFactory情況下執行二級緩存配置。

這是您的hazelcast實例配置文件,只是使用<hz:instance-name>myInstance</hz:instance-name>為您的hazelcast實例提供<hz:instance-name>myInstance</hz:instance-name>

 <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   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.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.hazelcast.com/schema/spring
            http://www.hazelcast.com/schema/spring/hazelcast-spring-3.5.xsd">

<context:annotation-config />

<hz:hazelcast id="instance">
    <hz:config>
        <hz:instance-name>myInstance</hz:instance-name>
        <hz:spring-aware />
        <hz:group name="dev" password="password"/>
        <hz:network port="5701" port-auto-increment="true">
            <hz:join>
                <hz:multicast enabled="true"
                              multicast-group="224.2.2.3"
                              multicast-port="54327"/>
            </hz:join>
        </hz:network>
        <hz:map name="default"
                in-memory-format="BINARY"
                backup-count="1"
                async-backup-count="0"
                time-to-live-seconds="0"
                max-idle-seconds="0"
                eviction-policy="NONE"
                max-size="0"
                max-size-policy="PER_NODE"
                eviction-percentage="30"
                min-eviction-check-millis="100"
                merge-policy="com.hazelcast.map.merge.PutIfAbsentMapMergePolicy"/>
    </hz:config>
</hz:hazelcast>

這是sessionFactory bean最后添加的兩個屬性, HazelcastLocalCacheRegionFactory將使用名為myInstance實例定義,在這種情況下你不需要使用<hz:hibernate-region-factory> config。

 <bean id="sessionFactory"
              class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="packagesToScan" value="com.somePackage" />
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.database">ORACLE</prop>
                    <prop key="hibernate.show_sql">false</prop>
                    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                    <!--enable 2nd level cache-->
                    <prop key="hibernate.cache.use_second_level_cache">true</prop>
                    <prop key="hibernate.cache.use_query_cache">false</prop>
                    <prop key="hibernate.cache.region.factory_class">com.hazelcast.hibernate.HazelcastLocalCacheRegionFactory</prop>
                    <prop key="hibernate.cache.hazelcast.instance_name">myInstance</prop>
                </props>
            </property>
    </bean>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM