gpt4 book ai didi

java - Hibernate Search 在主线程中同步执行

转载 作者:太空宇宙 更新时间:2023-11-04 08:52:27 24 4
gpt4 key购买 nike

看来 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。

最佳答案

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

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

如果您希望所有这些都在调用线程中运行,您需要重新实现 LuceneBackendQueueProcessorFactory 并将其绑定(bind)到 hibernate 属性中的 hibernate.search.worker.backend 。并非微不足道,但可行。

关于java - Hibernate Search 在主线程中同步执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3044967/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com