spring lucene rmi例子

http://www.blogjava.net/freeman1984/archive/2010/01/27/310996.html

结合lucene

import java.util.List;
public interface ServerRmiI {
	public String sayHi(String name);
	public String test(String queryStr,String willSearch,int pageSize,int pageNo );
	public List<MusicResourceV2Domain> getLuceneData(String queryStr,String willSearch,int pageSize,int pageNo );
}
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class ServerRmiImpl implements ServerRmiI {
	public String sayHi(String name) {
		return "Hi," + name;
	}
	public static void main(String[] args) {
		ServerRmiImpl aa=new ServerRmiImpl();
		String abc=aa.test("张","singer_name",1,10);
		System.out.println(abc);
	}
	public List<MusicResourceV2Domain> getLuceneData(String queryStr,String willSearch,int pageSize,int pageNo ){//F:\c\WX_QM_Music_Resource_V2_0918
		String indexDir ="/usr/local/resin_3/lucene/";//"F:/c/WX_QM_Music_Resource_V2_0918";
		String q = queryStr;
		List<MusicResourceV2Domain> lm=new ArrayList<MusicResourceV2Domain>();
		try {
			IndexReader dir = IndexReader.open(FSDirectory.open(new File(indexDir)));
			IndexSearcher searcher = new IndexSearcher(dir); // 3
			Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_31);
			QueryParser parser = new QueryParser(Version.LUCENE_31, willSearch, analyzer);
			Query query = parser.parse(q); // 4
			long start = System.currentTimeMillis(); 
			TopDocs results = searcher.search(query, 500); // 5,设置500条记录
			ScoreDoc[] hits = results.scoreDocs;
			long end = System.currentTimeMillis();
			MusicResourceV2Domain mvd=null;
			for (ScoreDoc scoreDoc : hits) {
				mvd=new MusicResourceV2Domain();
				Document doc = searcher.doc(scoreDoc.doc); // 7
				mvd.setId(doc.get("id"));
				mvd.setMusicName(doc.get("music_name"));
				mvd.setSingerId(doc.get("singer_id"));
				mvd.setSingerName(doc.get("singer_name"));
				mvd.setAlbumId(doc.get("album_id"));
				mvd.setMd5Text(doc.get("md5_text"));
				mvd.setMd5Value(doc.get("md5_value"));
				mvd.setVersion(doc.get("version"));
				mvd.setVersionDesc(doc.get("version_desc"));
				mvd.setMusicNameExt(doc.get("music_name_ext"));
				mvd.setSingerNameExt(doc.get("singer_name_ext"));
				mvd.setMusicId(doc.get("music_id"));
				mvd.setMvId(doc.get("mv_id"));
				mvd.setAspireRingId(doc.get("aspire_ring_id"));
				mvd.setAspireSongId(doc.get("aspire_song_id"));
				mvd.setAspireVideoId(doc.get("aspire_video_id"));
				mvd.setMusicCmsId(doc.get("music_cms_id"));
				mvd.setVideoCmsId(doc.get("video_cms_id"));
				mvd.setManualMusicId(doc.get("manual_music_id"));
				mvd.setManualMvId(doc.get("manual_mv_id"));
				mvd.setTs_10301(doc.get("ts_10301"));
				mvd.setTs_10302(doc.get("ts_10302"));
				mvd.setTs_time_sec(doc.get("ts_time_sec"));
				mvd.setSize_10301(doc.get("size_10301"));
				mvd.setSize_10302(doc.get("size_10302"));
				mvd.setSize_10303(doc.get("size_10303"));
				lm.add(mvd);
			}
			int totalRecord=hits.length;			
			searcher.close();
			return lm;
		} catch (ParseException e) {
			System.out.println("☆☆★★-----ParseException-luceneSearch:"+e.getMessage());
			e.printStackTrace();
		}catch (Exception e) {
			System.out.println("☆☆★★-----Exception-luceneSearch:"+e.getMessage());
			e.printStackTrace();
		}
		return null;
	}
}
<?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: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/context 
 http://www.springframework.org/schema/context/spring-context-2.5.xsd"
	default-autowire="byName" default-lazy-init="true">
	<bean name="clentrmi" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
		<property name="serviceUrl" value="rmi://localhost:9021/serverRmiTest" />
		<property name="serviceInterface" value="com.haha.music.mvp2.kmusic.rmi.ServerRmiI" />
	</bean>
</beans>
<?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: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/context 
 http://www.springframework.org/schema/context/spring-context-2.5.xsd"
	default-autowire="byName" default-lazy-init="true">
	<bean name="rmiserver" class="com.haha.music.mvp2.kmusic.rmi.ServerRmiImpl" />
	<bean name="serverTest" class="org.springframework.remoting.rmi.RmiServiceExporter">
		<property name="service" ref="rmiserver" />
		<property name="serviceName" value="serverRmiTest" />
		<property name="serviceInterface" value="com.haha.music.mvp2.kmusic.rmi.ServerRmiI" />
		<property name="registryPort" value="9021" />
	</bean>
</beans>

客户端

ApplicationContext context = new ClassPathXmlApplicationContext(
				"classpath:lucene-client.xml");
		ServerRmiI rmiI = (ServerRmiI) context.getBean("clentrmi");
		System.out.println("-----rmiI.sayHi---"+rmiI.sayHi("rmi"));
		System.out.println("----client--------------------------------------");
		List<MusicResourceV2Domain> lm=rmiI.getLuceneData("张","singer_name",1,10);

需要的jar包

common-util-1.0.0.jar

commons-beanutils-1.6.jar

commons-codec-1.6.jar

commons-collections.jar

commons-dbcp-1.2.2.jar

commons-discovery.jar

commons-fileupload-1.2.2.jar

commons-httpclient-3.0.1.jar

commons-id-gump-15042008.jar

commons-io-1.3.1.jar

commons-lang-2.4.jar

commons-logging-1.1.1.jar

commons-pool-1.3.jar

lucene-core-3.6.1.jar

mysql-connector-java-5.0.7-bin.jar

spring-aop.jar

spring-beans.jar

spring-context.jar

spring-core.jar

spring-web.jar

spring-webmvc.jar

aopalliance.jar

服务端启动可以用main方法或起个servlet在resin启动的时候

public class LuceneStart implements ServletContextListener{

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		System.out.println("LuceneStart---------->>>>>>>>>>");
		ApplicationContext context = new ClassPathXmlApplicationContext("classpath:lucene-server.xml");
		context.getBean("serverTest");//启动lucene远程端口
		
	}
	public static void main(String[] args) {
		System.out.println("LuceneStart---------->>>>>>>>>>");
		ApplicationContext context = new ClassPathXmlApplicationContext("classpath:lucene-server.xml");
		context.getBean("serverTest");//启动lucene远程端口
	}

}

web.xml

<listener>
		<listener-class>lucene.servlet.LuceneStart</listener-class>
	</listener>

windows下测试

netstat-an|findstr9021

看90端口是否启动

相关推荐