2010-06-15 88 views
1

看起來,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

很老的問題,但我還不如回答這個問題......

休眠的搜索功能,爲確保對Lucene的IndexWriter一個目錄(這是由Lucene的要求)單線程訪問。我想象一下,使用每個目錄的單線程執行程序是處理排隊問題的一種方法。

如果您希望它在調用線程中運行,您需要重新實現LuceneBackendQueueProcessorFactory,並在您的休眠屬性中將其綁定到hibernate.search.worker.backend。不是微不足道的,但可以。