当前位置: 代码迷 >> Web前端 >> spring lucene rmi事例
  详细解决方案

spring lucene rmi事例

热度:1009   发布时间:2012-11-23 00:03:29.0
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);
  相关解决方案