Java 主线程中的Hibernate搜索同步执行
Hibernate搜索同步执行似乎使用调用线程以外的其他线程来并行执行 如何在调用线程中串行执行Hibernate搜索执行 问题似乎出现在Java 主线程中的Hibernate搜索同步执行,java,hibernate,hibernate-search,Java,Hibernate,Hibernate Search,Hibernate搜索同步执行似乎使用调用线程以外的其他线程来并行执行 如何在调用线程中串行执行Hibernate搜索执行 问题似乎出现在org.hibernate.search.backend.impl.lucene.QueueProcessors类中: private void runAllWaiting() throws InterruptedException { List<Future<Object>> futures = new ArrayLi
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 );
}
}
}
}
private void runAllWaiting()引发InterruptedException{
List futures=newarraylist(dpProcessors.size());
//在每个DirectoryProvider上并行执行所有工作;
//每个DP都有自己的Executor服务。
for(PerDPQueueProcessor进程:dpProcessors.values()){
ExecutorService executor=process.getOwningExecutor();
//在将来包装每个Runnable
FutureTask f=新的FutureTask(进程,空);
加入(f);
执行人。执行(f);
}
//然后等待所有任务完成:
for(未来f:未来){
如果(!f.isDone()){
试一试{
f、 get();
}
捕获(取消异常忽略){
//忽略,如java.util.concurrent.AbstractExecutorService.invokeAll(集合
//任务)
}
捕获(ExecutionException错误){
//将原因重定向到已服务线程-这可能会隐藏更多异常:
可丢弃原因=错误。getCause();
抛出新的SearchException(原因);
}
}
}
}
串行同步执行将在调用线程中发生,并将向底层DirectoryProvider公开上下文信息(如身份验证信息)。这是一个非常古老的问题,但我不妨回答它 Hibernate Search这样做是为了确保单线程访问Lucene
IndexWriter
目录(Lucene需要)。我认为每个目录使用单线程执行器是处理排队问题的一种方法
如果希望在调用线程中全部运行,则需要重新实现LuceneBackendQueueProcessorFactory,并将其绑定到hibernate属性中的hibernate.search.worker.backend。这不是小事,但很能干。很老的问题,不过我还是回答一下吧 Hibernate Search这样做是为了确保单线程访问Lucene
IndexWriter
目录(Lucene需要)。我认为每个目录使用单线程执行器是处理排队问题的一种方法
如果希望在调用线程中全部运行,则需要重新实现LuceneBackendQueueProcessorFactory,并将其绑定到hibernate属性中的hibernate.search.worker.backend。不是琐碎的,而是可以做到的