Controller
/**
* 回调方法中监控消息是否发送成功 或 失败时做补偿处理
*/
@GetMapping("/callback/{message}")
public String sendMessageCallback(@PathVariable("message") String callbackMessage) {
kafkaProducer.sendMessageCallback(callbackMessage);
return "niu bi a";
}
Service :kafka producer
/**
* callback
*/
public void sendMessageCallback(String callbackMessage) {
kafkaTemplate.send("topic1", callbackMessage).addCallback(new ListenableFutureCallback<SendResult<String, Object>>() {
@Override
public void onFailure(Throwable ex) {
System.out.println("发送消息失败:" + ex.getMessage());
}
@Override
public void onSuccess(SendResult<String, Object> result) {
System.out.println("发送消息成功:" + result.getRecordMetadata().topic() + "-"
+ result.getRecordMetadata().partition() + "-" + result.getRecordMetadata().offset());
}
});
}
}
消费者:
@Service
public class KafkaConsumer3 {
@KafkaListener(topics = "topic1", groupId = KafkaConstants.KAFKA_GROUP_ID_THREE)
public void receivetopicmessage(String message) {
System.out.println("KafkaConsumer3 ---> receivetopicmessage:接收消息,内容为:" + message);
}
}
结果:
=====
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。