Hibernate Search 之 进度Monitor 您所在的位置:网站首页 hibernate现在淘汰了吗 Hibernate Search 之 进度Monitor

Hibernate Search 之 进度Monitor

2023-06-06 03:04| 来源: 网络整理| 查看: 265

在 http://simonlei.iteye.com/blog/577068 这篇文章中,我找到了一个简单的办法来对

索引进行reindex。不过,现在又有个需求,我希望能够对 reindex的过程做一个进度条。

 

看了一下文档,fullTextSession.createIndexer() 得到的实际上是 MassIndexerImpl,使用这个类

的 startAndWait 方法来进行reindex,就是阻塞的,使用 start 就是异步的。而且,更美好的是,

MassIndexerImpl 类里面使用了一个 MassIndexerProgressMonitor,它使用了一个

SimpleIndexingProgressMonitor 的简单实现来进行log。

 

美中不足的是,这个 monitor 是一个私有field,而且不提供setter。这个就很痛苦了,不过也不难,

用reflect把它绕过去,然后自己做一个Monitor就OK了。

 

好了,上代码:

FullTextSession fullTextSession = Search.getFullTextSession( getSession()); DocumentIndexProgressMonitor monitor = new DocumentIndexProgressMonitor(); MassIndexer indexer = fullTextSession.createIndexer(); ReflectUtils.setFieldValue( MassIndexerImpl.class, "monitor", indexer, monitor); indexer.start();

 

setFieldValue 做什么了?看代码:

public static void setFieldValue( Class clz, String fieldName, Object obj, Object fieldValue) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Field field = clz.getDeclaredField( fieldName); field.setAccessible( true); field.set( obj, fieldValue); }

 

至于monitor的实现就简单了,不上代码了,自己参考hibernate search当中的源代码。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有