Jpa-hibernate-spring and compass search enginer

初始化Bean,实现Spring运行加载

public class CompassIndexBuilder implements InitializingBean{

	private boolean buildIndex = false;
	private int lazySecond =10;
	private CompassGps compassGps;
	
	private Thread compassStartThread = new Thread( new Runnable(){
     	
		public void run() {
			try {
				Thread.sleep(lazySecond * 1000);
				Stopwatch watch  =new Stopwatch();
				compassGps.index();
				print("Compass builder index cost time :"+watch.reset());
			} catch (InterruptedException e) {
				print("Compass builder throw graveness error "+e);
			}		
		}
		
	});
	
	public void print(String str){
		 PrintWriter printWriter = new PrintWriter(System.out);
		 printWriter.println(str);
	}
	public void afterPropertiesSet() throws Exception {
		if(buildIndex){
			compassStartThread.setDaemon(true);
			compassStartThread.setName("Compass index builder");
			compassStartThread.start();
		}
	}
	public void setBuildIndex(boolean buildIndex) {
		this.buildIndex = buildIndex;
	}
	public void setLazySecond(int lazySecond) {
		this.lazySecond = lazySecond;
	}
	public void setCompassGps(CompassGps compassGps) {
		this.compassGps = compassGps;
	}

}


Compass Search,my tools
import java.util.ArrayList;
import java.util.List;

import org.compass.core.Compass;
import org.compass.core.CompassCallback;
import org.compass.core.CompassException;
import org.compass.core.CompassHits;
import org.compass.core.CompassQuery;
import org.compass.core.CompassQueryBuilder;
import org.compass.core.CompassSession;
import org.compass.core.CompassTemplate;
import org.hibernate.cfg.annotations.QueryBinder;

public class CompassSearch<E> implements Search<E>{

	private Compass compass;
	
	public void delete(E e) {
		CompassTemplate template = new CompassTemplate(compass);
		template.delete(e);
	}

	public List<E> find(final String keyword) {
		CompassTemplate template = new CompassTemplate(compass);
		return template.execute(new CompassCallback<List<E>>(){

			public List<E> doInCompass(CompassSession session) throws CompassException {
				List<E> results = new ArrayList<E>();
				CompassQueryBuilder queryBuilder= session.queryBuilder();
				CompassHits hits = null;
				CompassQuery allProperty = queryBuilder.queryString(keyword).toQuery();
				hits = allProperty.hits();
				int length = hits.length();
				for(int i=0;i<length; i++){
					results.add((E)hits.data(i));
				}
				return results;
			}		
		});
	}

	public void index(E e) {
		CompassTemplate template = new CompassTemplate(compass);
		template.create(e);
	}

	public void update(final E e) {
	  CompassTemplate template = new CompassTemplate(compass);
		template.execute(new CompassCallback<E>(){

			public E doInCompass(CompassSession session)
					throws CompassException {
				session.delete(e);
				session.create(e);
				return null;
			}
			
		});
	}

	public void setCompass(Compass compass) {
		this.compass = compass;
	}

}


<bean id="annotationConfiguration" class="org.compass.annotations.config.CompassAnnotationsConfiguration" />
    
    <bean id="compass" class="org.compass.spring.LocalCompassBean">
    		<property name="compassConfiguration">
    		    <ref  bean="annotationConfiguration"/>
    		</property>
            <property name="transactionManager">
            	<ref  bean="transactionManager"/>
            </property>
            <property name="settings">
            	<props>
            		<prop key="compass.engine.connection">d:/compass</prop>
            		<prop key="compass.transaction.factory">
            					org.compass.spring.transaction.SpringSyncTransactionFactory 
            		</prop>
            		<prop key="compass.engine.analyzer.MMAnalyzer.CustomAnalyzer">
            					org.mira.lucene.analysis.IK_CAnalyzer
            		</prop>
            	</props>       
            </property>
     <!--   <property name="resourceJarLocations">
            	<list>
            		<value>WEB-INF/lib/your.jar</value>
            	</list>
            </property>
     -->
           <property name="resourceDirectoryLocations">
           		<list>
           			<value>com/you/model</value>
           		</list>
           </property>
      
    </bean>
    
    <bean id="jpaGpsDevice" class="org.compass.gps.device.jpa.JpaGpsDevice">
    		<property name="name">
    			<value>JpaGpsDevice</value>
    		</property>
    		<property name="entityManagerFactory">
    			<ref  bean="entityManagerFactory"/>
    		</property>
    		<property name="mirrorDataChanges">
    			<value>true</value>
    		</property>
    </bean>
 
 	<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">
 				<ref  bean="jpaGpsDevice"/>
 			</property>
 	</bean>
 
    <bean id="compassTemplate" class="org.compass.core.CompassTemplate">
    		<property name="compass">
    			<ref  bean="compass"/>
    		</property>
    </bean>
    
    <bean id="compassIndexBuilder" class="com.crm.compass.CompassIndexBuilder">
    		<property name="buildIndex" value="true" /> 
    		<property name="lazySecond" value="10" />
    		<property name="compassGps">
    			<ref  bean="compassGps"/>
    		</property>
    </bean>
    
    <bean id="exploitCustomerSearch" class="com.crm.dao.compass.ExploitCustomerSearch">
    		<property name="compass">
    			<ref  bean="compass"/>
    		</property>
    </bean>



if you look do not understand ,please check http://lqw.iteye.com/blog/348970

你可能感兴趣的:(java,spring,Hibernate,jpa,Lucene)