Hibernate在主线程中搜索同步执行

时间:2010-06-15 12:13:22

标签: java hibernate hibernate-search

似乎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。

1 个答案:

答案 0 :(得分:1)

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

Hibernate Search可以确保对目录(Lucene所需)的Lucene IndexWriter进行单线程访问。我想每个目录使用单线程执行程序是一种处理排队问题的方法。

如果您希望所有内容都在调用线程中运行,则需要重新实现LuceneBackendQueueProcessorFactory并将其绑定到hibernate属性中的hibernate.search.worker.backend。不是微不足道的,但可以做到。