0

I'm migrating from the HLRC to the new client, things were smooth but for some reason I cannot index a specific class/document. Here is my client implementation and index request:

@Configuration
public class ClientConfiguration{

    @Autowired
    private InternalProperties conf;

    public ElasticsearchClient sslClient(){
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(conf.getElasticsearchUser(), conf.getElasticsearchPassword()));
        HttpHost httpHost = new HttpHost(conf.getElasticsearchAddress(), conf.getElasticsearchPort(), "https");
        RestClientBuilder restClientBuilder = RestClient.builder(httpHost);
        try {
            SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, (x509Certificates, s) -> true).build();
            restClientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                @Override
                public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                    return httpClientBuilder.setSSLContext(sslContext)
                            .setDefaultCredentialsProvider(credentialsProvider);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        RestClient restClient=restClientBuilder.build();
        
        ElasticsearchTransport transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());
        
        ElasticsearchClient client = new ElasticsearchClient(transport);
        return client;
    }

}

@Service
public class ThisDtoIndexClass extends ConfigAndProperties{


public ThisDtoIndexClass() {

       }

//client is declared in the class it's extending from 
public ThisDtoIndexClass(@Autowired ClientConfiguration esClient) {
          this.client = esClient.sslClient();
       }

 @KafkaListener(topics = "esTopic")
   public void in(@Payload(required = false) customDto doc)
      throws ThisDtoIndexClassException, ElasticsearchException, IOException {
      if(doc!= null && doc.getId() != null) {
          IndexRequest.Builder<customDto > indexReqBuilder = new IndexRequest.Builder<>();
          indexReqBuilder.index("index-for-this-Dto");
          indexReqBuilder.id(doc.getId());
          indexReqBuilder.document(doc);

          IndexResponse response = client.index(indexReqBuilder.build());
      } else {
         throw new ThisDtoIndexClassException("document is null");
      }

   }
}

This is all done in spring boot (v2.6.8) with ES 7.17.3. According to the debug, the payload is NOT null! It even fetches the id correctly while stepping through. For some reason, it throws me a org.springframework.kafka.listener.ListenerExecutionFailedException: in the last line (during the .build?). Nothing gets indexed, but the response comes back 200. I'm lost on where I should be looking. I have a different class that also writes to a different index, also getting a payload from kafka directly (all seperate consumers). That one functions just fine.

I suspect it has something to do with the way my client is set up and/or the kafka. Please point me in the right direction.

1
  • Edit the question to show the complete stack trace. Nobody can help you without that. Commented Jul 7, 2022 at 12:51

1 Answer 1

0

I solved it by deleting the default constructor. If I put it back it overwrites the extended constructor (or straight up doesn't acknowledge the extended constructor), so my client was always null. The error message it gave me was extremely misleading since it actually wasn't the Kafka's fault!

Removing the default constructor completely initializes the correct constructor and I was able to index again. I assume this was a spring boot loading related "issue".

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.