Hibernate Search 在主线程中同步执行

发布于 2024-09-05 13:08:21 字数 1634 浏览 4 评论 0原文

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

It seems that Hibernate Search synchronous execution uses other threads than the calling thread for parallel execution.

How do I execute the Hibernate Search executions serially in the calling thread?

The problem seems to be in the org.hibernate.search.backend.impl.lucene.QueueProcessors class :

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 );
                }
            }
        }
    }

A serial synchronous execution would happen in the calling thread and would expose context information such as authentication information to the underlying DirectoryProvider.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栩栩如生 2024-09-12 13:08:21

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

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

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

Very old question, but I might as well answer it...

Hibernate Search does that to ensure single-threaded access to the Lucene IndexWriter for a directory (which is required by Lucene). I imagine the use of an single-threaded executor per-directory was a way of dealing with the queueing problem.

If you want it all to run in the calling thread you need to re-implement the LuceneBackendQueueProcessorFactory and bind it to hibernate.search.worker.backend in your hibernate properties. Not trivial, but do-able.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文