Example of combining SSH with compass

Source: Internet
Author: User
SSH to add compass is to add 5 jar, compass-2.1.0.jar compass-index-patch.jar lucene-core.jar
Lucene-highlighter.jar paoding-analysis.jar
I posted some of the following main code .. If you have any questions, please leave a message ..
Productmanagerimpl implementation class

Package com. revic. example. Servce. impl;

Import java. util. arraylist;
Import java. util. List;

Import org. Compass. Core. compass;
Import org. Compass. Core. compasshits;
Import org. Compass. Core. compasssession;
Import org. Compass. Core. compasstemplate;
Import org. Compass. Core. compasstransaction;

Import com. revic. example. Dao. productdao;
Import com. revic. example. model. product;
Import com. revic. example. model. testuser;
Import com. revic. example. Servce. productmanager;

Public class productmanagerimpl implements productmanager {
Private productdao;
Private compasstemplate;
Public compasstemplate getcompasstemplate (){
Return compasstemplate;
}
Public void setcompasstemplate (compasstemplate ){
This. compasstemplate = compasstemplate;
}
Public productdao getproductdao (){
Return productdao;
}
Public void setproductdao (productdao ){
This. productdao = productdao;
}
Public void addproduct (product P ){
Productdao. addproduct (P );
}
Public list searchproducts (string querystring ){
System. Out. println ("searchproducts ...");
Compass compass = compasstemplate. getcompass ();
Compasssession session = compass. opensession ();
List list = new arraylist ();
Compasstransaction Tx = session. begintransaction ();

Compasshits hits = session. querybuilder (). querystring ("name:" + querystring). toquery (). Hits ();
System. Out. println ("querystring:" + querystring );
System. Out. println ("hits:" + hits. getlength ());
For (INT I = 0; I Product hit = (product) hits. Data (I );
List. Add (hit );
}
TX. Commit ();
Session. Close ();
Return list;
}
Public list searchtestuser (string querystring ){
System. Out. println ("searchtestuser ...");
Compass compass = compasstemplate. getcompass ();
Compasssession session = compass. opensession ();
List list = new arraylist ();
Compasstransaction Tx = session. begintransaction ();

Compasshits hits = session. querybuilder (). querystring ("username:" + querystring). toquery (). Hits ();
System. Out. println ("querystring:" + querystring );
System. Out. println ("hits:" + hits. getlength ());
For (INT I = 0; I Testuser hit = (testuser) hits. Data (I );
List. Add (hit );
}
TX. Commit ();
Session. Close ();
Return list;
}

}

Compassindexbuilder class

Package com. revic. example. Servce. impl;
Import org. Compass. GPS. compassgps;
Import org. springframework. Beans. Factory. initializingbean;

Public class compassindexbuilder implements initializingbean {
// Whether to create an index. It can be set to false to invalidate the builder.
Private Boolean buildindex = false;

// Index operation thread delay start time, in seconds
Private int lazytime = 10;

// Compass Encapsulation
Private compassgps;

// Index thread
Private thread indexthread = new thread (){

@ Override
Public void run (){
Try {
Thread. Sleep (lazytime * 1000 );
System. Out. println ("begin compass index ...");
Long begintime = system. currenttimemillis ();
// Re-create the index.
// If the index file defined in the compass object already exists, a temporary index will be created during the index process,
// Overwrite the index.
Compassgps. Index ();
Long costtime = system. currenttimemillis ()-begintime;
System. Out. println ("compss index finished .");
System. Out. println ("costed" + costtime + "milliseconds ");
} Catch (interruptedexception e ){
E. printstacktrace ();
}
}
};


Public void afterpropertiesset () throws exception {
If (buildindex ){
Indexthread. setdaemon (true );
Indexthread. setname ("compass indexer ");
Indexthread. Start ();
}
}

Public void setbuildindex (Boolean buildindex ){
This. buildindex = buildindex;
}

Public void setlazytime (INT lazytime ){
This. lazytime = lazytime;
}

Public void setcompassgps (compassgps ){
This. compassgps = compassgps;
}
}

ApplicationContext-compass.xml files

<? XML version = "1.0" encoding = "UTF-8"?>

<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
Default-Lazy-init = "true">

<Bean id = "annotationconfiguration"
Class = "org. Compass. Annotations. config. compassannotationsconfiguration">
</Bean>

<Bean id = "Compass" class = "org. Compass. Spring. localcompassbean">
<Property name = "resourcedirectorylocations">
<List>
<Value> classpath: COM/revic </value>
</List>
</Property>
<Property name = "connection">
<Value>/Lucene/indexes </value>
</Property>

<Property name = "classmappings">
<List>
<Value> com. revic. example. model. Product </value>
<Value> com. revic. example. model. testuser </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"/>
<Property name = "mirrordatachanges">
<Value> true </value>
</Property>
</Bean>
<! -- Synchronously update indexes -->
<Bean id = "compassgps" class = "org. Compass. GPS. impl. singlecompassgps"
Init-method = "start" Destroy-method = "stop">
<Property name = "Compass" ref = "Compass"/>
<Property name = "gpsdevices">
<List>
<Bean
Class = "org. Compass. Spring. device. springsynctransactiongpsdevicewrapper">
<Property name = "gpsdevice" ref = "hibernategpsdevice"/>
</Bean>
</List>
</Property>
</Bean>

<Bean id = "compasstemplate"
Class = "org. Compass. Core. compasstemplate">
<Property name = "Compass" ref = "Compass"/>
</Bean>

<! -- Re-index regularly (using quartz) or with spring applicationcontext -->
<Bean id = "compassindexbuilder"
Class = "com. revic. example. Servce. impl. compassindexbuilder"
Lazy-init = "false">
<Property name = "compassgps" ref = "compassgps"/>
<Property name = "buildindex" value = "true"/>
<Property name = "lazytime" value = "10"/>
</Bean>
</Beans>

ApplicationContext-common.xml files

<? 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"
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>
<Bean id = "datasource"
Class = "org. Apache. commons. DBCP. basicdatasource">
<Property name = "driverclassname"
Value = "oracle. JDBC. Driver. oracledriver">
</Property>
<Property name = "url"
Value = "JDBC: oracle: thin: @ 192.168.0.1: 1521: ora9i">
</Property>
<Property name = "username" value = "××"> </property>
<Property name = "password" value = "××"> </property>
</Bean>
<Bean id = "sessionfactory"
Class = "org. springframework. Orm. hibernate3.localsessionfactorybean">
<Property name = "datasource">
<Ref bean = "datasource"/>
</Property>
<Property name = "hibernateproperties">
<Props>
<Prop key = "hibernate. dialect">
Org. hibernate. dialect. oracle9dialect
</Prop>
<Prop key = "hibernate. show_ SQL"> true </prop>
</Props>
</Property>
<Property name = "mappingresources">
<List>
<Value> COM/revic/example/model/product. HBM. xml </value>
<Value> COM/revic/example/model/testuser. HBM. xml </value>
</List>
</Property> </bean>

<! -- Configure the Transaction Manager -->
<Bean id = "transactionmanager" class = "org. springframework. Orm. hibernate3.hibernatetransactionmanager">
<Property name = "sessionfactory">
<Ref bean = "sessionfactory"/>
</Property>
</Bean>

<! -- Configure the propagation feature of transactions -->
<TX: Advice id = "txadvice" transaction-Manager = "transactionmanager">
<TX: Attributes>
<TX: method name = "Search *" propagation = "required"/>
<TX: method name = "add *" propagation = "required"/>
<TX: method name = "del *" propagation = "required"/>
<TX: method name = "Modify *" propagation = "required"/>
<TX: method name = "*" Read-Only = "true"/>
</TX: Attributes>
</TX: Advice>

<! -- Which methods of those classes are involved in the transaction -->
<AOP: config>
<AOP: pointcut id = "allmanagermethod" expression_r_r = "execution (* COM. revic. example. *. * (...)"/>
<AOP: Advisor pointcut-ref = "allmanagermethod" advice-ref = "txadvice"/>
</AOP: config>
</Beans>

ApplicationContext-bean.xml file,

<? XML version = "1.0" encoding = "UTF-8"?>
<Beans
Xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<Bean id = "productdao" class = "com. revic. example. Dao. impl. productdaoimpl">
<Property name = "sessionfactory" ref = "sessionfactory"> </property>
</Bean>
<Bean id = "productmanager" class = "com. revic. example. Servce. impl. productmanagerimpl">
<Property name = "productdao" ref = "productdao"> </property>
<Property name = "compasstemplate" ref = "compasstemplate"> </property>
</Bean>
<Bean id = "addbean" class = "com. revic. example. Action. productaction">
<Property name = "productmanager" ref = "productmanager"> </property>
</Bean>
</Beans>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.