简体   繁体   English

尝试通过新的 ElasticSearch Java API 客户端索引 kafka 有效负载时出现 ListenerExecutionFailedException Nullpointer

[英]ListenerExecutionFailedException Nullpointer when trying to index kafka payload through new ElasticSearch Java API Client

I'm migrating from the HLRC to the new client, things were smooth but for some reason I cannot index a specific class/document.我正在从 HLRC 迁移到新客户端,一切都很顺利,但由于某种原因,我无法索引特定的类/文档。 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.这一切都是在带有 ES 7.17.3 的 spring boot (v2.6.8) 中完成的。 According to the debug, the payload is NOT null!根据调试,有效负载不为空! It even fetches the id correctly while stepping through.它甚至可以在单步执行时正确获取 id。 For some reason, it throws me a org.springframework.kafka.listener.ListenerExecutionFailedException: in the last line (during the .build ?).出于某种原因,它在最后一行(在.build期间?)向我抛出了org.springframework.kafka.listener.ListenerExecutionFailedException: Nothing gets indexed, but the response comes back 200. I'm lost on where I should be looking.没有任何内容被编入索引,但响应返回 200。我不知道应该在哪里寻找。 I have a different class that also writes to a different index, also getting a payload from kafka directly (all seperate consumers).我有一个不同的类,它也写入不同的索引,也直接从 kafka 获取有效负载(所有单独的消费者)。 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.请指出我正确的方向。

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".我认为这是与弹簧启动加载相关的“问题”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Java测试中创建索引时Elasticsearch本地节点客户端NPE - Elasticsearch local node client NPE when creating index in java test 带有docker的elasticsearch 5.5.3 Java客户端API NoNodeAvailableException - elasticsearch 5.5.3 Java client API NoNodeAvailableException with docker 尝试通过 SpringBoot 从 MySQL DB 获取用户列表时出现 Java Nullpointer 异常 - Java Nullpointer exception when trying to get users list from MySQL DB via SpringBoot 尝试在测试中使用方法时出现 NullPointer 错误 - NullPointer Error when trying to use method in test 使用多个有效负载在 java 中发布 API - Post API in java with Multiple payload Elasticsearch java 客户端初始化失败 - Elasticsearch java client initialization fails 尝试自动装配时在 spring 托管 class 上获取空指针 - Getting nullpointer on a spring managed class when trying to autowire Elasticsearch(6.5)高级别java rest客户端按名称删除索引不起作用 - Elasticsearch(6.5) HIgh level java rest client Delete an index by name is not working 在使用 SpringBoot 开发的 REST API 中映射到 Java 对象时,使 XML 负载字段不区分大小写 - Make XML payload fields case insensitive when mapping to Java Object in REST API developed using SpringBoot 使用SpringBoot开发的REST API映射到Java Object时使JSON payload字段不区分大小写 - Make JSON payload fields case insensitive when mapping to Java Object in REST API developed using SpringBoot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM