VanRoy/spring-data-jest

Launch without Elasticsearch available

jgrenat opened this issue · 4 comments

Hello,

We're using Jest on our project, and we would like to be able to start the application even if Elasticsearch is not currently available, and be able to make requests to it if it becomes available.

Is it possible right now? If not, maybe we can help with that?

Thanks!
Jordane

Edit: I've forgotten to indicate that for now it fails at startup with the following stacktrace when ES is not available:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.elasticsearch.repository.support.SimpleElasticsearchRepository]: Constructor threw exception; nested exception is org.springframework.data.elasticsearch.ElasticsearchException: failed to execute action
	at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:182)
	at org.springframework.data.repository.core.support.RepositoryFactorySupport.lambda$getTargetRepositoryViaReflection$3(RepositoryFactorySupport.java:500)
	at java.util.Optional.map(Optional.java:215)
	at org.springframework.data.repository.core.support.RepositoryFactorySupport.getTargetRepositoryViaReflection(RepositoryFactorySupport.java:500)
	at org.springframework.data.repository.core.support.RepositoryFactorySupport.getTargetRepositoryViaReflection(RepositoryFactorySupport.java:485)
	at org.springframework.data.elasticsearch.repository.support.ElasticsearchRepositoryFactory.getTargetRepository(ElasticsearchRepositoryFactory.java:73)
	at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:298)
	at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$3(RepositoryFactoryBeanSupport.java:287)
	at org.springframework.data.util.Lazy.getNullable(Lazy.java:141)
	at org.springframework.data.util.Lazy.get(Lazy.java:63)
	at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:290)
	at org.springframework.data.elasticsearch.repository.support.ElasticsearchRepositoryFactoryBean.afterPropertiesSet(ElasticsearchRepositoryFactoryBean.java:67)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1761)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1698)
	... 28 common frames omitted
Caused by: org.springframework.data.elasticsearch.ElasticsearchException: failed to execute action
	at com.github.vanroy.springdata.jest.JestElasticsearchTemplate.execute(JestElasticsearchTemplate.java:1220)
	at com.github.vanroy.springdata.jest.JestElasticsearchTemplate.executeWithAcknowledge(JestElasticsearchTemplate.java:1225)
	at com.github.vanroy.springdata.jest.JestElasticsearchTemplate.indexExists(JestElasticsearchTemplate.java:848)
	at com.github.vanroy.springdata.jest.JestElasticsearchTemplate.createIndexIfNotCreated(JestElasticsearchTemplate.java:1420)
	at com.github.vanroy.springdata.jest.JestElasticsearchTemplate.createIndex(JestElasticsearchTemplate.java:175)
	at org.springframework.data.elasticsearch.repository.support.AbstractElasticsearchRepository.createIndex(AbstractElasticsearchRepository.java:95)
	at org.springframework.data.elasticsearch.repository.support.AbstractElasticsearchRepository.<init>(AbstractElasticsearchRepository.java:86)
	at org.springframework.data.elasticsearch.repository.support.SimpleElasticsearchRepository.<init>(SimpleElasticsearchRepository.java:36)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:170)
	... 41 common frames omitted
Caused by: io.searchbox.client.config.exception.CouldNotConnectException: Could not connect to http://localhost:9200
	at io.searchbox.client.http.JestHttpClient.execute(JestHttpClient.java:70)
	at io.searchbox.client.http.JestHttpClient.execute(JestHttpClient.java:60)
	at com.github.vanroy.springdata.jest.JestElasticsearchTemplate.execute(JestElasticsearchTemplate.java:1212)
	... 53 common frames omitted

@jgrenat Indeed, it would be a very nice enhancement. Do you have some times to send a PR with a POC of implementation ?

Thanks a lot.
Julien.

I may have some time to look into that in a few weeks, I'll let you in touch.

Do you already have an idea of the specific things that will need to be changed in order for it to work? That would help me a lot! :)

I may have some time to look into that in a few weeks, I'll let you in touch.

Do you already have an idea of the specific things that will need to be changed in order for it to work? That would help me a lot! :)

Do you have any news on this?

I figured out, that the startup exception rises when trying to create the index in AbstractElasticSearchRepostory.java

@jgrenat @simiii Indeed the startup exception is due to index creation.
You can disable it with the option createIndex of Documentannotation.

@Document(indexName = "index-book", type = "book", createIndex = false)
public class Book {
	@Id private String id;
...