I am using KafkaTemplate#send(Message<?>)
. I am not sending a key. I was wondering if it's ok to use type parameters of KafkaTemplate<Object,Object>
? Would KafkaTemplate<Object, PlanDTO>
be a more relevant type parameter? Are there any rules associated with this? I'm using spring boot 2.5.4
and jdk 11
. Here's what I have:
@Component
public class PlanProducer
{
@Autowired
private KafkaTemplate<Object, Object> kafkaTemplate;
public void postPlanToKafka(PlanDTO plan)
{
Message<PlanDTO> message = MessageBuilder.withPayload(plan)
.setHeader(KafkaHeaders.TOPIC, "plan")
.setHeader(KafkaHeaders.TIMESTAMP, Instant.now().getEpochSecond())
.build();
ListenableFuture<SendResult<Object, Object>> future = kafkaTemplate.send(message);
future.addCallback(new KafkaSendCallback<Object, Object>()
{
@Override
public void onSuccess(SendResult<Object, Object> result)
{
RecordMetadata metadata = result.getRecordMetadata();
log.info("Message sent successfully with the following information : topic - {} , "
"partition - {} , offset - {}",metadata.topic(),
metadata.partition(),metadata.offset());
}
@Override
public void onFailure(KafkaProducerException e)
{
log.error("Error posting data to kafka ={}",e);
}
});
}
}
CodePudding user response:
It is perfectly fine to use <Object, Object>
.
There are no "rules" since the actual types are determined by the serializers.
The new receive()
methods in 2.8 will need a narrrower type, but for sending, it is not an issue.