Search code examples
spring-bootelasticsearchproject-reactorspring-data-elasticsearchreactor-netty

Spring application using ReactiveElasticsearchClient is stuck indefinitely even though Elasticsearch server is running


I have been trying to use ReactiveElasticsearchClient to index some documents in my spring application. My application is stuck indefinitely when I am trying to run the jar with command java -jar reactive-es-1.0-SNAPSHOT.jar application.ReactiveEsApplication

Here's the pom.xml file

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.5.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>reactive-es</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <java.version>1.8</java.version>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <configuration>
                            <mainClass>application.ReactiveEsApplication</mainClass>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>


        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
            <version>4.1.8</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webflux</artifactId>
            <version>5.3.6</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
            <version>2.4.5</version>
        </dependency>

        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-core</artifactId>
            <version>3.4.5</version>
        </dependency>

        <dependency>
            <groupId>io.projectreactor.netty</groupId>
            <artifactId>reactor-netty</artifactId>
            <version>1.0.6</version>
        </dependency>
    </dependencies>

</project>

Here's ESClientConfiguration class

package application.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient;
import org.springframework.data.elasticsearch.client.reactive.ReactiveRestClients;
import org.springframework.stereotype.Component;

@Component
public class ESClientConfiguration {

    @Value("${es.host}")
    private String host;

    @Bean
    public ReactiveElasticsearchClient getReactiveClient() {
        final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo(host)
                .build();
        return ReactiveRestClients.create(clientConfiguration);
    }
}

Here's ReactiveEsApplication class

package application;

import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient;

import java.util.HashMap;

@Slf4j
@SpringBootApplication
public class ReactiveEsApplication implements CommandLineRunner {
    private final ReactiveElasticsearchClient reactiveElasticsearchClient;

    @Autowired
    public ReactiveEsApplication(ReactiveElasticsearchClient reactiveElasticsearchClient) {
        this.reactiveElasticsearchClient = reactiveElasticsearchClient;
    }

    public static void main(String[] args) {
        SpringApplication.run(ReactiveEsApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("============Started running============");
        try {
            System.out.println("============Started indexing============");
            insertDocument("my-index");
        } catch (Exception e) {
            log.error("Error!", e);
        }
    }

    public void insertDocument(final String indexName) {

        IndexRequest indexRequest = new IndexRequest(indexName);
        indexRequest.source(getDocument(), XContentType.JSON);
        reactiveElasticsearchClient
                .index(indexRequest)
                .subscribe(System.out::println, System.out::println, () -> System.out.println("completed"));
    }

    private HashMap<String, String> getDocument() {
        return new HashMap<String, String>() {{
            put("name", "maria");
            put("age", "29");
            put("experience", "7");
        }};
    }
}

Here's application.properties

es.host=10.47.20.112:9200

When I run the command java -jar reactive-es-1.0-SNAPSHOT.jar application.ReactiveEsApplication, the application gets stuck and here's the console log


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.5.RELEASE)

2021-04-30 17:51:33.244  INFO 16868 --- [           main] a.ReactiveEsApplication                  : Starting ReactiveEsApplication v1.0-SNAPSHOT on ip-172-31-46-44 with PID 16868 (/home/user/reactive-es-1.0-SNAPSHOT.jar started by user in /home/user)
2021-04-30 17:51:33.249  INFO 16868 --- [           main] a.ReactiveEsApplication                  : No active profile set, falling back to default profiles: default
2021-04-30 17:51:33.838  INFO 16868 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Elasticsearch repositories in DEFAULT mode.
2021-04-30 17:51:33.855  INFO 16868 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 13ms. Found 0 Elasticsearch repository interfaces.
2021-04-30 17:51:33.860  INFO 16868 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Reactive Elasticsearch repositories in DEFAULT mode.
2021-04-30 17:51:33.862  INFO 16868 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 1ms. Found 0 Reactive Elasticsearch repository interfaces.
2021-04-30 17:51:34.808  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from class org.springframework.data.geo.Point to interface java.util.Map as writing converter although it doesn't convert to a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.809  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from interface java.util.Map to class org.springframework.data.geo.Point as reading converter although it doesn't convert from a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.809  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from class org.springframework.data.elasticsearch.core.geo.GeoPoint to interface java.util.Map as writing converter although it doesn't convert to a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.809  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from interface java.util.Map to class org.springframework.data.elasticsearch.core.geo.GeoPoint as reading converter although it doesn't convert from a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.809  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from interface org.springframework.data.elasticsearch.core.geo.GeoJson to interface java.util.Map as writing converter although it doesn't convert to a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.809  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from interface java.util.Map to interface org.springframework.data.elasticsearch.core.geo.GeoJson as reading converter although it doesn't convert from a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.810  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from class org.springframework.data.elasticsearch.core.geo.GeoJsonPoint to interface java.util.Map as writing converter although it doesn't convert to a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.810  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from interface java.util.Map to class org.springframework.data.elasticsearch.core.geo.GeoJsonPoint as reading converter although it doesn't convert from a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.810  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from class org.springframework.data.elasticsearch.core.geo.GeoJsonMultiPoint to interface java.util.Map as writing converter although it doesn't convert to a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.810  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from interface java.util.Map to class org.springframework.data.elasticsearch.core.geo.GeoJsonMultiPoint as reading converter although it doesn't convert from a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.810  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from class org.springframework.data.elasticsearch.core.geo.GeoJsonLineString to interface java.util.Map as writing converter although it doesn't convert to a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.811  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from interface java.util.Map to class org.springframework.data.elasticsearch.core.geo.GeoJsonLineString as reading converter although it doesn't convert from a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.811  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from class org.springframework.data.elasticsearch.core.geo.GeoJsonMultiLineString to interface java.util.Map as writing converter although it doesn't convert to a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.811  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from interface java.util.Map to class org.springframework.data.elasticsearch.core.geo.GeoJsonMultiLineString as reading converter although it doesn't convert from a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.811  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from class org.springframework.data.elasticsearch.core.geo.GeoJsonPolygon to interface java.util.Map as writing converter although it doesn't convert to a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.811  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from interface java.util.Map to class org.springframework.data.elasticsearch.core.geo.GeoJsonPolygon as reading converter although it doesn't convert from a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.812  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from class org.springframework.data.elasticsearch.core.geo.GeoJsonMultiPolygon to interface java.util.Map as writing converter although it doesn't convert to a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.812  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from interface java.util.Map to class org.springframework.data.elasticsearch.core.geo.GeoJsonMultiPolygon as reading converter although it doesn't convert from a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.812  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from class org.springframework.data.elasticsearch.core.geo.GeoJsonGeometryCollection to interface java.util.Map as writing converter although it doesn't convert to a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.812  WARN 16868 --- [           main] o.s.d.c.CustomConversions                : Registering converter from interface java.util.Map to class org.springframework.data.elasticsearch.core.geo.GeoJsonGeometryCollection as reading converter although it doesn't convert from a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-04-30 17:51:34.883  INFO 16868 --- [           main] o.s.d.e.s.VersionInfo                    : Version Spring Data Elasticsearch: 4.1.8
2021-04-30 17:51:34.883  INFO 16868 --- [           main] o.s.d.e.s.VersionInfo                    : Version Elasticsearch Client in build: 7.9.3
2021-04-30 17:51:34.883  INFO 16868 --- [           main] o.s.d.e.s.VersionInfo                    : Version Elasticsearch Client used: 7.6.2
2021-04-30 17:51:34.884  WARN 16868 --- [           main] o.s.d.e.s.VersionInfo                    : Version mismatch in between Elasticsearch Clients build/use: 7.9.3 - 7.6.2
2021-04-30 17:51:35.476  INFO 16868 --- [           main] a.ReactiveEsApplication                  : Started ReactiveEsApplication in 2.679 seconds (JVM running for 3.448)
============Started running============
============Started indexing============

The code doesn't go beyond this and gets stuck here.

I confirmed whether the elasticsearch server is working on 10.47.20.112 or not; and it's working. Here's the response to http://10.47.20.112:9200/my-index/_search?pretty=true&q=*:*

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my-index",
        "_type" : "_doc",
        "_id" : "JXBYInkBorO7pC2lkzAR",
        "_score" : 1.0,
        "_source" : {
          "age" : 32,
          "experience" : 10,
          "name" : "john"
        }
      }
    ]
  }
}

Solution

  • The problem is that you are mixing versions of the libraries which do not match and do not work together.

    You define the parent as Spring Boot 2.3.5, a version which uses Spring 5.2 but later define the webflux version to 5.3.6. The Spring Data Elasticsearch version 4.1.8 also is built with Spring 5.3.2. I didn't check the recactor and netty versions in detail, but there probably is some conflict as well - it seems that in your pom you defined these dependencies with the versions from Spring Boot 2.4.5 but keep the parent version on 2.3.5.

    Your application runs with the following pom (I set the parent to 2.4.5 and removed unneeded explicit dependencies):

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.4.5</version>
            <relativePath/>
        </parent>
        <groupId>com.example</groupId>
        <artifactId>reactive-es</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>jar</packaging>
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <configuration>
                                <mainClass>application.ReactiveEsApplication</mainClass>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    
        <dependencies>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-logging</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-log4j2</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
            
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-elasticsearch</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-webflux</artifactId>
            </dependency>
    
        </dependencies>
    
    </project>