一、前言
基于Hibernate的org.hibernate.search.batchindexing.MassIndexerProgressMonitor(hibernate-search-engine-4.3.0.final.jar)的搜索引擎索引创建进度过程监控,实现创建lucense全文索引透明化跟踪,下面通过自定义IndexingProgressMonitor结合具体使用示例进行说明,具体如下所示
import java.util.concurrent.TimeUnit;@b@import java.util.concurrent.atomic.AtomicLong;@b@import org.hibernate.search.batchindexing.MassIndexerProgressMonitor;@b@import org.hibernate.search.util.logging.impl.Log;@b@import org.hibernate.search.util.logging.impl.LoggerFactory;@b@@b@public class IndexingProgressMonitor implements MassIndexerProgressMonitor {@b@@b@ private static final Log log = LoggerFactory.make();@b@ private final AtomicLong documentsDoneCounter = new AtomicLong();@b@ private final AtomicLong totalCounter = new AtomicLong();@b@ private volatile long startTime;@b@ private final int logAfterNumberOfDocuments;@b@@b@ /**@b@ * Logs progress of indexing job every 50 documents written.@b@ */@b@ public IndexingProgressMonitor() {@b@ this( 50 );@b@ }@b@@b@ /**@b@ * Logs progress of indexing job every <code>logAfterNumberOfDocuments</code>@b@ * documents written.@b@ *@b@ * @param logAfterNumberOfDocuments log each time the specified number of documents has been added@b@ */@b@ public IndexingProgressMonitor(int logAfterNumberOfDocuments) {@b@ this.logAfterNumberOfDocuments = logAfterNumberOfDocuments;@b@ }@b@@b@ public void entitiesLoaded(int size) {@b@ //not used@b@ }@b@@b@ public void documentsAdded(long increment) {@b@ long current = documentsDoneCounter.addAndGet( increment );@b@ if ( current == increment ) {@b@ startTime = System.nanoTime();@b@ }@b@ if ( current % getStatusMessagePeriod() == 0 ) {@b@ printStatusMessage( startTime, totalCounter.get(), current );@b@ }@b@ }@b@@b@ public void documentsBuilt(int number) {@b@ //not used@b@ }@b@@b@ public void addToTotalCount(long count) {@b@ totalCounter.addAndGet( count );@b@ log.indexingEntities( count );@b@ }@b@@b@ public void indexingCompleted() {@b@ log.indexingEntitiesCompleted( totalCounter.get() );@b@ }@b@@b@ protected int getStatusMessagePeriod() {@b@ return logAfterNumberOfDocuments;@b@ }@b@@b@ protected void printStatusMessage(long startTime, long totalTodoCount, long doneCount) {@b@ long elapsedMs = TimeUnit.NANOSECONDS.toMillis( System.nanoTime() - startTime );@b@ log.indexingDocumentsCompleted( doneCount, elapsedMs );@b@ float estimateSpeed = doneCount * 1000f / elapsedMs;@b@ float estimatePercentileComplete = doneCount * 100f / totalTodoCount;@b@ log.indexingSpeed( estimateSpeed, estimatePercentileComplete );@b@ }@b@ @b@ public long getDocumentsDoneCounter(){@b@ return this.documentsDoneCounter.get();@b@ }@b@ @b@ public long getTotalCounter(){@b@ return this.totalCounter.get();@b@ }@b@ @b@}