lucene使用教程6 --lucene在项目里的集成

设计良好的线程模式保证lucene的运行不影响原本项目的性能。而且大大降低了lucene和项目之间的耦合度。

索引的操作线程:

一下代码是从网上剪切过来的,可能有错误,但是原理都是一样的,明白了原理,什么都简单。稍后我会将我自己写的几个测试代码贴上去,可以 关注下后面的文字

public class MyIndexOperateThread extends Thread {

	private static Logger logger = LogManager
			.getLogger(MyIndexOperateThread.class);
	private Object obj;
	private String str;
	private Long id;
	private List<Long> ids;
	private Operate operate;

	/**
	 * 枚举
	 * 
	 * @author wangrenhui
	 * 
	 */
	public enum Operate {
		update, delete, add
	}

	/**
	 * @param operate 更新和添加操作
	 * @param obj 更新的对象
	 * @param moduleMark 模块标示
	 */
	public MyIndexOperateThread(Operate operate, Object obj, String moduleMark) {
		this.operate = operate;
		this.obj = obj;
		this.str = moduleMark;
	}

	/**
	 * 
	 * @param operate 操作
	 * @param id id
	 * @param className  类名
	 */
	public MyIndexOperateThread(Operate operate, Long id, String className) {
		this.operate = operate;
		this.id = id;
		this.str = className;
	}

	/**
	 * 
	 * @param operate  删除操作
	 * @param ids id集合
	 * @param className 类名
	 */
	public MyIndexOperateThread(Operate operate, List<Long> ids,
			String className) {
		this.operate = operate;
		this.ids = ids;
		this.str = className;
	}

	@Override
	public void run() {
		try {
			if (operate == Operate.delete) {
				if (id != null) {
					IndexCreateUtil.deleteDocument(id, str);
				} else if (ids != null) {
					IndexCreateUtil.deleteDocument(ids, str);
				}
			} else if (operate == Operate.update) {
				IndexCreateUtil.updateDocument(obj, str);
			} else if (operate == Operate.add) {
				IndexCreateUtil.addDocument(obj, str);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		} finally {
			MyIndexWriter.close(); // 注意不是调用 IndexWriter.Close() !
		}
	}
}

数据持久层的使用:

/**
	 * 
	 * @function 更新处方
	 * @param prescription  处方对象
	 */
	public void updatePrescription(Prescription prescription) {
		this.getHibernateTemplate().update(prescription);
		try {
			MyIndexOperateThread indexOperateThread = new MyIndexOperateThread(Operate.update, prescription, "Prescription");//更新索引的线程
			indexOperateThread.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值