---
title: "LucenePool"
author: "xiaowei2002"
date: "2014-11-07T11:36:39Z"
tags: []
categories: ["Web前端"]
slug: "lucenepool"
draft: false
lastmod: "2014-11-07T11:36:39Z"
id: 2153340
url: "https://xiaowei2002.iteye.com/blog/2153340"
---

package com.yulong.lucene.core;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class LucenePool extends Thread {

	/**
	 * 是否执行线程方法
	 */
	private boolean isrun = true;

	/**
	 * 索引锁定池
	 *
	 * key		索引路径
	 * value	Map<线程名词,线程状态>
	 *
	 */
	private Map<String,Map> pool = new HashMap<String,Map>();

	/**
	 * 索引对象缓存池
	 */
	private Map<String,IndexWriter> index = new HashMap<String, IndexWriter>();

	/**
	 * 得到索引读写器
	 *
	 * @param key		索引路径
	 * @param value		Map<线程名词,线程状态>
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public IndexWriter get(String key,String threadname){
//		isrun = false;
		IndexWriter iw = null;
		try {
			iw = getTrueIndexWriter(key);
//			if(pool.containsKey(key)){
//				Map map = pool.get(key);
//				map.put(threadname, "true");
//				iw = index.get(key);
//			}else{//如果不存在索引读写器
//				Map map = new HashMap();
//				iw = getTrueIndexWriter(key);
//				map.put(threadname, "true");
//				pool.put(key, map);
//				index.put(key, iw);
//			}
		} catch (Exception e) {
			e.printStackTrace();
		}
//		isrun = true;
		return iw;
	}

	/**
	 * 通知线程处理完毕
	 *
	 * @param key
	 * @param threadname
	 */
	@SuppressWarnings("unchecked")
	public void over(String key,String threadname){
		Map map = pool.get(key);
		map.put(threadname, "false");
	}

	/**
	 * 得到可写的临时文件
	 *
	 * @return
	 * @throws IOException
	 * @throws CorruptIndexException
	 */
	private IndexWriter getTrueIndexWriter(String temppath) throws Exception{
		IndexWriter iwriter = null;
		File file = new File(temppath);
		if(!file.exists()){// 如果文件不存在就创建
			file.mkdirs();
		}
		//检查索引文件的文档熟路是否超过大小
		Directory directory = FSDirectory.open(new File(temppath));
		try{
			iwriter = new IndexWriter(directory, LuceneContacts.analyzer, false, new IndexWriter.MaxFieldLength(2500000));
		}catch (Exception e) {
			iwriter = new IndexWriter(directory, LuceneContacts.analyzer, true, new IndexWriter.MaxFieldLength(2500000));
		}
		return iwriter;
	}

	/**
	 * 处理检测是否存在可释放的索引器
	 */
	@SuppressWarnings("static-access")
	public void run(){
		while(true){
			if(isrun){//清理不可用的读写器
				String str = "";
				for(Iterator<String> it = pool.keySet().iterator();it.hasNext();){
					String key = it.next();
					Map map = pool.get(key);
					if(!map.containsValue("true")){
						IndexWriter iw = index.get(key);
						index.remove(key);
						if(iw!=null){
							if(str!="")
								str += ",";
							str += key;
							try {try {iw.close();} catch (Exception e) {}
							}finally{if(iw!=null){try {iw.close();} catch (Exception e) {}}}
						}
					}
				}
				if(str!=""){
					String temp = str.split(",");
					for (int i = 0; i < temp.length; i++) {
						System.out.println("*********************************************************");
						System.out.println("清理："+temp*);
						System.out.println("*********************************************************");
						pool.remove(temp*);
					}
				}
			}
			try {
				this.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}