多线程写Lucene索引

主要是用了线程池来实现writer的多线程

import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;

public class ThreadedIndexWriter extends IndexWriter {
	private ExecutorService threadPool;
	private Analyzer defaultAnalyzer;

	private class Job implements Runnable { // 保留要加入索引的一个文档
		Document doc;
		Analyzer analyzer;
		Term delTerm;

		public Job(Document doc, Term delTerm, Analyzer analyzer) {
			this.doc = doc;
			this.analyzer = analyzer;
			this.delTerm = delTerm;

		}

		public void run() { // 实际增加和更新文档
			try {
				if (delTerm != null) {
					ThreadedIndexWriter.super.updateDocument(delTerm, doc,
							analyzer);
				} else {
					ThreadedIndexWriter.super.addDocument(doc, analyzer);
				}
			} catch (IOException ioe) {
				throw new RuntimeException(ioe);
			}
		}
	}

	@SuppressWarnings("deprecation")
	public ThreadedIndexWriter(Directory dir, Analyzer a, boolean create,
			int numThreads, int maxQueueSize, IndexWriter.MaxFieldLength mfl)
			throws CorruptIndexException, IOException {
		super(dir, a, create, mfl);
		defaultAnalyzer = a;
		threadPool = new ThreadPoolExecutor(
				// 创建线程池
				numThreads, numThreads, 0, TimeUnit.SECONDS,
				new ArrayBlockingQueue<Runnable>(maxQueueSize, false),
				new ThreadPoolExecutor.CallerRunsPolicy());
	}

	public void addDocument(Document doc) { // 让线程池增加文档
		threadPool.execute(new Job(doc, null, defaultAnalyzer));
	}

	public void addDocument(Document doc, Analyzer a) { // 让线程池增加文档
		threadPool.execute(new Job(doc, null, a));
	}

	public void updateDocument(Term term, Document doc) { // 让线程池更新文档
		threadPool.execute(new Job(doc, term, defaultAnalyzer));

	}

	// 让线程池更新文档
	public void updateDocument(Term term, Document doc, Analyzer a) {
		threadPool.execute(new Job(doc, term, a));
	}

	public void close() throws CorruptIndexException, IOException {
		finish();
		super.close();
	}

	public void close(boolean doWait) throws CorruptIndexException, IOException {
		finish();
		super.close(doWait);
	}

	public void rollback() throws CorruptIndexException, IOException {
		finish();
		super.rollback();
	}

	private void finish() { // 关闭线程池
		threadPool.shutdown();
		while (true) {
			try {
				if (threadPool.awaitTermination(Long.MAX_VALUE,
						TimeUnit.SECONDS)) {
					break;
				}
			} catch (InterruptedException ie) {
				Thread.currentThread().interrupt();
				throw new RuntimeException(ie);
			}
		}
	}
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值