compass 整合hibernate spring 的第二种方式 ( 通过 定时器进行数据同步)compass-2.2.0 lucene-2.9.2. hibernate-distribut spring 2.5.6 paoding

<?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:aop="http://www.springframework.org/schema/aop"
    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-2.5.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-2.5.xsd"
    default-lazy-init="true">
    <context:annotation-config />
    <context:component-scan base-package="org.jixiuf" />

   

    <!-- 配置事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref local="sessionFactory" />
        </property>
    </bean>

    <!-- 配置事务特性 ,配置add、delete和update开始的方法,事务传播特性为required-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>

    <!--
        配置那些类的方法进行事务管理,当前cn.com.jobedu.oa.service包中的子包、类中所有方法需要,还需要参考tx:advice的设置
    -->
    <aop:config>
        <aop:pointcut id="allManagerMethod"
            expression="execution (* org.jixiuf.service.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod" />
    </aop:config>

    <!-- 定义数据源的Bean ,给Hibernate的sessionFactory-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver">
        </property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test">
        </property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>

        <property name="initialPoolSize">
            <value>10</value>
        </property>
        <property name="minPoolSize">
            <value>5</value>
        </property>
        <property name="maxPoolSize">
            <value>30</value>
        </property>
        <property name="acquireIncrement">
            <value>5</value>
        </property>
        <property name="maxIdleTime">
            <value>10</value>
        </property>
        <property name="maxStatements">
            <value>0</value>
        </property>
    </bean>



    <!-- 定义Hibernate的sessionFactory,通过该Bean,可以获得Hibernate的Session-->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                </prop>
                <!--设置二级缓冲-->
                <prop key="hibernate.cache.provider_class">
                    org.hibernate.cache.EhCacheProvider
                </prop>
                <!--设置二级缓冲,打开查询缓冲-->
                <prop key="hibernate.cache.use_query_cache">true</prop>
                <!--设置显示Hibernate操作的SQL语句-->
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hbm2ddl.auto">create</prop>
            </props>
        </property>
        <property name="annotatedClasses" ref="annotatedClasses" />


    </bean>
    <!-- annocation pojo class in hibernate  -->
    <bean id="annotatedClasses"
        class="org.springframework.beans.factory.config.ListFactoryBean">
        <property name="sourceList">
            <list>
                <value>org.jixiuf.pojo.User</value>
                        <value>org.jixiuf.pojo.Role</value>
            </list>
        </property>
    </bean>

    <bean id="hibernateTemplate" name="hibernateTemplate"
        class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>


    <bean id="annotationConfiguration"
        class="org.compass.annotations.config.CompassAnnotationsConfiguration">
    </bean>


    <bean id="compass" class="org.compass.spring.LocalCompassBean">
    <property name="connection">
        <value>file://c:/target</value>
    </property>
    <property name="classMappings">
        <list>
            <value>org.jixiuf.pojo.User</value>
             <value>org.jixiuf.pojo.Role</value>
        </list>
    </property>
    <property name="compassConfiguration" ref="annotationConfiguration" />

    <property name="compassSettings">
        <props>
            <prop key="compass.transaction.factory">
                org.compass.spring.transaction.SpringSyncTransactionFactory
                </prop>
            <prop key="compass.engine.analyzer.MMAnalyzer.CustomAnalyzer">net.paoding.analysis.analyzer.PaodingAnalyzer </prop>
        </props>
   
    </property>
 
        <property name="transactionManager" ref="transactionManager" />
    </bean>
    <bean id="hibernateGpsDevice" class="org.compass.gps.device.hibernate.HibernateGpsDevice">
      <property name="name">
    <value>hibernateDevice</value></property>
      <property name="sessionFactory" ref="sessionFactory" />
    
     
    </bean>

<!--这个类中有个index() 方法,它会将数据库中的数据同步到compass 的index 中 ,前提是pojo 对象进行了compass 相应的配置,可以用annotation 或者xml

compassGps.addDevice() 加入一个或多个 GpsDevice  如上面的 hibernateGpsDevice 然后依次调用 GpsDevice .index()进行索引的重建

 

-->
    <bean id="compassGps" class="org.compass.gps.impl.SingleCompassGps"  init-method="start" destroy-method="stop" >
       <property name="compass"><ref bean="compass" /></property>
       <property name="gpsDevices">
         <list>
             <ref bean="hibernateGpsDevice" />
         </list>
       </property>
     </bean>
  
   <!--  BuildIndexUtil  implements InitializingBean 随着spring 的初始化而启动
 *
 * 默认会在Web应用每次启动时重建索引,可以设置buildIndex属性为false来禁止此功能. 也可以不用本Builder,
 * 编写手动调用compassGps.index()的代码.
 
 org.jixiuf.util.BuildIndexUtil中会定时调用 compassGps.index();(每隔10000ms )
  -->

     <bean id="indexBuilder" class="org.jixiuf.util.BuildIndexUtil"   lazy-init="false" >
     <property name="compassGps" ref="compassGps" />
     <!-- spring启动后 延迟1s 运行程序,进行索引重建  -->
      <property name="delay" value="1000" />
      <!-- 每隔10s进行一次索引重建,监视数据库中数据的变化 -->
         <property name="period" value="10000" />
              <property name="buildIndex" value="true" />
        
     </bean>
    <bean id="compassTemplate"
        class="org.compass.core.CompassTemplate">
        <property name="compass" ref="compass" />
    </bean>

 

</beans>

 

 

=====================================================================

package org.jixiuf.util;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import org.compass.gps.CompassGps;
import org.springframework.beans.factory.InitializingBean;

/**
 * implements InitializingBean 随着spring 的初始化而启动
 *
 * 默认会在Web应用每次启动时重建索引,可以设置buildIndex属性为false来禁止此功能. 也可以不用本Builder,
 * 编写手动调用compassGps.index()的代码.
 *
 */
public class BuildIndexUtil implements InitializingBean {

    private CompassGps compassGps;

    long delay = 1000; // 1s
    long period = 1000 * 30;// 30s
    // 是否构建索引,默认为构建 ,如果置为false 而此程序基本什么也不做
    boolean buildIndex = true;

    public boolean isBuildIndex() {
        return buildIndex;
    }

    public void setBuildIndex(boolean buildIndex) {
        this.buildIndex = buildIndex;
    }

    public long getDelay() {
        return delay;
    }

    public void setDelay(long delay) {
        this.delay = delay;
    }

    public long getPeriod() {
        return period;
    }

    public void setPeriod(long period) {
        this.period = period;
    }

    public CompassGps getCompassGps() {
        return compassGps;
    }

    public void setCompassGps(CompassGps compassGps) {
        this.compassGps = compassGps;
    }

    public void afterPropertiesSet() throws Exception {
        if (buildIndex)

            buildIndex();
    }

    public void buildIndex() {
        Timer timer = new Timer("buildIndexForCompass", true);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("================build index at "+new Date());
                compassGps.index();
            }
        }

        , delay, period);
    }
}

 

 

 

==========================================================

compass 整合spring hibernate 的两种方式 的比较 ,

第一种方式是基于事件的,即 一但通过hibernate 向数据库中插入数据,更新数据,删除数据等操作compass的

org.compass.gps.device.hibernate.embedded.CompassEventListener 类就会监控到数据的变化 

将新数据也添加到compass 的索引中,此种方式 有个缺点就是如果不是通过 hibernate 而是在后台用其它的工具操作数据库的

中的数据,它监测不到,故保险起见,也可以写一个定时调试程序 ,在特定的时间进行索引重建 (通过  compassGps.index(); )

当然定时的间隔可以长一点,甚至不必运行,

第二种方式 ,它不监测hibernate 更新数据的操作,而是直接针对数据库,每隔一段时间如10s ,便同步一次索引

这种方式内部如何实现不太确定 (好像是先删除原来 的所有 索引 ,然后根据数据库中的数据 重建 一次索引 ,如果真是这样,当数据是

很大时,这种操作负载应该比较大)



=-----------------------------------------------------------------------------------------------------------------
我理解错了,原来第二种方式 也可以进行实时的数据更新,并不需要定时器,
 定时器存在的目的只是为了防止 非hibernate 方式操作数据,如用命令行方式增添数据
<bean id="compassGps" class="org.compass.gps.impl.SingleCompassGps"
init-method="start" destroy-method="stop">
<property name="compass">
<ref bean="compass" />
</property>
<property name="gpsDevices">
<list>
<ref bean="hibernateGpsDevice" />
</list>
</property>
</bean>

<!--
implements InitializingBean 随着spring 的初始化而启动 * *
默认会在Web应用每次启动时重建索引,可以设置buildIndex属性为false来禁止此功能. 也可以不用本Builder, *
编写手动调用compassGps.index()的代码. org.jixiuf.util.BuildIndexUtil中会定时调用
compassGps.index();
-->
<bean id="indexBuilder" class="org.jixiuf.util.BuildIndexUtil" lazy-init="false" >
<property name="compassGps" ref="compassGps" />
<!-- spring启动后 延迟1s 运行程序,进行索引重建 -->
<property name="delay" value="1000" />
<!-- 每隔10s进行一次索引重建,监视数据库中数据的变化 -->
<property name="period" value="10000" />
</bean>

<bean id="hibernateGpsDevice" class="org.compass.gps.device.hibernate.HibernateGpsDevice">
<property name="name">
<value>hibernateDevice</value>
</property>
<property name="sessionFactory" ref="sessionFactory" />
<!-- 第一要设置这个参数 ,即做数据映射 ,即实时更新数据 ,但这并不够,
必须 在程序 中的某一处至少调用 一次compassGps.index(); -->
我本以为只要配置了这个参数就可以做数据实时更新,原来少了一步,
<property name="mirrorDataChanges" value="true" />

<property name="fetchCount" value="100" />


</bean>


上面的BuildIndexUtil 。afterPropertiesSet() 中就可以运行一次compassGps.index()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值