[英]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.