Kafka之自定义 interceptor
1)需求:
实现一个简单的双interceptor组成的拦截链。第一个interceptor会在消息发送前将时间戳信息加到消
息value的最前部;第二个interceptor会在消息发送后更新成功发送消息数或失败发送消息数
2)案例实操
(1)增加时间戳拦截器
package com.xiaoxiao.kafka.interceptor;
import org.apache.kafka.clients.producer.ProducerInterceptor;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import java.util.Map;
/**
* 实现一个简单的双interceptor组成的拦截链。第一个interceptor会在消息发送前将时间戳信息
* 加到消息value的最前部;第二个interceptor会在消息发送后更新成功发送消息数或失败发送消息数。
* @author xiaohu
* @create 2020-08-29 11:54
*/
public class FirstInterceptor implements ProducerInterceptor<String,String> {
@Override
public ProducerRecord<String, String> onSend(ProducerRecord<String, String> record) {
//取出消息的value
String value = new Str