1

似乎 Hibernate Search 同步执行使用了除调用线程之外的其他线程进行并行执行。

如何在调用线程中串行执行 Hibernate Search 执行?

问题似乎出在org.hibernate.search.backend.impl.lucene.QueueProcessors课堂上:

private void runAllWaiting() throws InterruptedException {
        List<Future<Object>> futures = new ArrayList<Future<Object>>( dpProcessors.size() );
        // execute all work in parallel on each DirectoryProvider;
        // each DP has it's own ExecutorService.
        for ( PerDPQueueProcessor process : dpProcessors.values() ) {
            ExecutorService executor = process.getOwningExecutor();
            //wrap each Runnable in a Future
            FutureTask<Object> f = new FutureTask<Object>( process, null );
            futures.add( f );
            executor.execute( f );
        }
        // and then wait for all tasks to be finished:
        for ( Future<Object> f : futures ) {
            if ( !f.isDone() ) {
                try {
                    f.get();
                }
                catch (CancellationException ignore) {
                    // ignored, as in java.util.concurrent.AbstractExecutorService.invokeAll(Collection<Callable<T>>
                    // tasks)
                }
                catch (ExecutionException error) {
                    // rethrow cause to serviced thread - this could hide more exception:
                    Throwable cause = error.getCause();
                    throw new SearchException( cause );
                }
            }
        }
    }

串行同步执行将发生在调用线程中,并将诸如身份验证信息之类的上下文信息暴露给底层 DirectoryProvider。

4

1 回答 1

1

很老的问题,但我不妨回答一下......

Hibernate Search 这样做是为了确保对目录的 Lucene 进行单线程访问IndexWriter(这是 Lucene 所要求的)。我想每个目录使用单线程执行器是处理排队问题的一种方式。

如果您希望它全部在调用线程中运行,您需要重新实现LuceneBackendQueueProcessorFactory并将其绑定到hibernate.search.worker.backend您的休眠属性中。不是微不足道的,而是可行的。

于 2012-02-19T14:26:26.117 回答