# Question abount Hibernate Search FailureHandler (HS 6)

**URL:** <https://discourse.hibernate.org/t/question-abount-hibernate-search-failurehandler-hs-6/4812>\
**Category:** Hibernate Search\
**Created:** [November 26, 2020, 4:26am UTC](https://discourse.hibernate.org/t/question-abount-hibernate-search-failurehandler-hs-6/4812 "2020-11-26T04:26:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mason](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@mason](https://discourse.hibernate.org/u/mason)\
**Post date:** [November 26, 2020, 4:26am UTC](https://discourse.hibernate.org/t/question-abount-hibernate-search-failurehandler-hs-6/4812/1 "2020-11-26T04:26:22Z")

</div>

As stated in the documentation ([https://docs.jboss.org/hibernate/search/6.0/reference/en-US/html\_single/#mapper-orm-indexing-automatic-synchronization](https://docs.jboss.org/hibernate/search/6.0/reference/en-US/html_single/#mapper-orm-indexing-automatic-synchronization) \> Exclamation mark state failure handler), not invoked FailureHandler method but exception raised when indexing failed with write-sync setting (automatic\_indexing.synchronization.strategy)

In this circumstance,  
When indexing failed for the reason such as elasticsearch server down, time out etc,  
i want to invoke implemented FailureHandler with write-sync setting (automatic\_indexing.synchronization.strategy).  
Because it is easy to recovery failed indexing data using FailureHandler(especially EntityIndexingFailureContext argument handle method).

Is there any way to solve?

---

<div class="post-metadata">

**Author:** ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)\
**Post date:** [November 26, 2020, 7:06am UTC](https://discourse.hibernate.org/t/question-abount-hibernate-search-failurehandler-hs-6/4812/2 "2020-11-26T07:06:26Z")

</div>

Yes. Use a custom strategy: [https://docs.jboss.org/hibernate/search/6.0/reference/en-US/html\_single/#\_custom\_strategy](https://docs.jboss.org/hibernate/search/6.0/reference/en-US/html_single/#_custom_strategy)

Something like this should work:

```auto
package com.mycompany;

import ...;

public final class MyWriteSyncAutomaticIndexingSynchronizationStrategy
		implements AutomaticIndexingSynchronizationStrategy {

	public static final MyWriteSyncAutomaticIndexingSynchronizationStrategy INSTANCE = new MyWriteSyncAutomaticIndexingSynchronizationStrategy();

	private MyWriteSyncAutomaticIndexingSynchronizationStrategy() {
	}

	@Override
	public void apply(AutomaticIndexingSynchronizationConfigurationContext context) {
		context.documentCommitStrategy( DocumentCommitStrategy.FORCE );
		context.documentRefreshStrategy( DocumentRefreshStrategy.NONE );
		FailureHandler failureHandler = context.failureHandler();
		context.indexingFutureHandler( future -> {
			// Wait for the result of indexing, so that we're sure changes were committed (or failed).
			SearchIndexingPlanExecutionReport report = future.join();
			report.throwable().ifPresent( t -> {
				EntityIndexingFailureContext.Builder contextBuilder = EntityIndexingFailureContext.builder();
				contextBuilder.throwable( throwable );
				contextBuilder.failingOperation( "automatic indexing" );
				for ( EntityReference entityReference : result.failingEntities() ) {
					contextBuilder.entityReference( entityReference );
				}
				failureHandler.handle( contextBuilder.build() );
			} );
		} );
	}
}

```

Make sure you’re using version 6.0.0.CR2.

Then set configuration property `hibernate.search.automatic_indexing.synchronization.strategy` to `constructor:com.mycompany.MyWriteSyncAutomaticIndexingSynchronizationStrategy`.

---

<div class="post-metadata">

**Author:** ![mason](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@mason](https://discourse.hibernate.org/u/mason)\
**Post date:** [November 29, 2020, 1:55pm UTC](https://discourse.hibernate.org/t/question-abount-hibernate-search-failurehandler-hs-6/4812/3 "2020-11-29T13:55:53Z")

</div>

Thanks your explanation.

And i intent to use EntityManager code in FailureHandler (bean registered) like below

```auto
@Component(value = "myFailureHandler")
public class MyFailureHandler implements FailureHandler {
   ...
   @PersistenceContext
   private EntityManager entityManager;
   ...
}

```

In this case, there are some issues,

1. When `background_failure_handler` declared start with `bean:`,  
error occured like this  
**org.hibernate.search.util.common.SearchException: HSEARCH000501: Invalid value for configuration property ‘hibernate.search.background\_failure\_handler’: ‘bean:myFailureHandler’. HSEARCH000579: Unable to resolve bean reference to type ‘org.hibernate.search.engine.reporting.FailureHandler’ and name ‘myFailureHandler’. Failed to resolve bean from bean manager with exception: HSEARCH000590: No configured bean manager.**

2. When `background_failure_handler` declared start with `class:` or `constructor:`,  
`EntityManager` dependency injection in `MyFailureHandler` not effected, so that is null

In this situation, Is there a good way the hibernate search provides?

---

<div class="post-metadata">

**Author:** ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)\
**Post date:** [November 30, 2020, 7:20am UTC](https://discourse.hibernate.org/t/question-abount-hibernate-search-failurehandler-hs-6/4812/4 "2020-11-30T07:20:57Z")

</div>

> [@mason](#):
>
> In this situation, Is there a good way the hibernate search provides?

There is no built-in solution for what you’re trying to achieve, no.

Regarding the injection of an entity manager into the failure handler, that’s probably not a good idea, since the failure handler is instantiated while the entity manager factory is being built, so it can’t be used at this time.

If you really want to do something like this, you will have to implement a dedicated Spring component that performs whatever you want to do with the entity manager, and inject that service into your failure handler with `@Lazy @Autowired`. See [https://www.baeldung.com/spring-lazy-annotation](https://www.baeldung.com/spring-lazy-annotation)

---

<div class="post-metadata">

**Author:** ![Hanan\_Awwad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/hanan_awwad/32/1295_2.png) [@Hanan\_Awwad](https://discourse.hibernate.org/u/Hanan_Awwad)\
**Post date:** [November 30, 2020, 9:59pm UTC](https://discourse.hibernate.org/t/question-abount-hibernate-search-failurehandler-hs-6/4812/5 "2020-11-30T21:59:17Z")

</div>

I have the same condition here however i am trying to inject hazelcast instance so as to cache failing entities to try index them later; Can you please give us more details about how we could achieve this?
