根据配置文件一步步去查看:
spring-consumer.xml
DefaultKafkaConsumerFactory就是根据参数配置生产consumer,略过。
监听器类,实现了监听类一些列接口的某一个。我这里使用了手动提交的AcknowledgingMessageListener 接口,以此为例。
首先他又继承了一个接口
public interface AcknowledgingMessageListener extends GenericAcknowledgingMessageListener> {
}
继承的接口
public interface GenericAcknowledgingMessageListener extends KafkaDataListener {
void onMessage(T var1, Acknowledgment var2);
}
我的实现类,重写发放就好,略。。。继续下一步
public class KafkaConsumerListener implements AcknowledgingMessageListener {
@Override
public void onMessage(ConsumerRecord stringStringConsumerRecord, Acknowledgment acknowledgment) {
System.out.printf("offset= %d, key= %s, value= %s,topic= %s,partition= %s\n",
stringStringConsumerRecord.offset(),
stringStringConsumerRecord.key(),
stringStringConsumerRecord.value(),
stringStringConsumerRecord.topic(),
stringStringConsumerRecord.partition());
acknowledgment.acknowledge();
}
}
topic1
topic2
org.springframework.kafka.listener.config.ContainerProperties
关注的第一个构造方法,我可以传入多个topic,也就是说我可以通过传入一个list来同时订阅多个主题的消息
public ContainerProperties(String... topics) {
this.ackMode = AckMode.BATCH;
this.pollTimeout = 1000L;
this.shutdownTimeout = 10000L;
this.syncCommits = true;
this.ackOnError = true;
Assert.notEmpty(topics, "An array of topicPartitions must be provided");
this.topics = (String[])Arrays.asList(topics).toArray(new String[topics.length]);
this.topicPattern = null;
this.topicPartitions = null;
}
关注的第二个构造方法,我可以传入一个正则表达式对象来匹配主题
public ContainerProperties(Pattern topicPattern) {
this.ackMode = AckMode.BATCH;
this.pollTimeout = 1000L;
this.shutdownTimeout = 10000L;
this.syncCommits = true;
this.ackOnError = true;
this.topics = null;
this.topicPattern = topicPattern;
this.topicPartitions = null;
}
关注的第三个构造方法,我可以传入一个或多个set,对应的键值对为主题和分区,关注特定主题特定分区的消息
public ContainerProperties(TopicPartitionInitialOffset... topicPartitions) {
this.ackMode = AckMode.BATCH;
this.pollTimeout = 1000L;
this.shutdownTimeout = 10000L;
this.syncCommits = true;
this.ackOnError = true;
this.topics = null;
this.topicPattern = null;
Assert.notEmpty(topicPartitions, "An array of topicPartitions must be provided");
this.topicPartitions = (TopicPartitionInitialOffset[])(new LinkedHashSet(Arrays.asList(topicPartitions))).toArray(new TopicPartitionInitialOffset[topicPartitions.length]);
}
关注点私有属性
/**
* 确认模式(自动确认属性为false时使用)
*
*
1.RECORD逐条确认: 每条消息被发送给监听者后确认*
2.BATCH批量确认: 当批量消息记录被消费者接收到并传送给监听器时确认*
3.TIME超时确认:当超过设置的超时时间毫秒数时确认(should be greater than* {@code #setPollTimeout(long) pollTimeout}.
*
4.COUNT计数确认: 当接收到指定数量之后确认*
5.MANUAL手动确认:由监听器负责确认(AcknowledgingMessageListener)*/
private AckMode ackMode;
//手动提交次数
private int ackCount;
//手动提交时间
private long ackTime;
//消息监听器,必须是 MessageListener或者AcknowledgingMessageListener两者中的一个
private Object messageListener;
//阻止消费者等待记录的最长时间。
private volatile long pollTimeout;
//线程执行器:轮询消费者
private AsyncListenableTaskExecutor consumerTaskExecutor;
//错误回调,当监听器抛出异常时
private GenericErrorHandler> errorHandler;
//停止容器超时时间
private long shutdownTimeout;
//用户定义的消费者再平衡监听器实现类
private ConsumerRebalanceListener consumerRebalanceListener;
//提交回调,默认记录日志。
private OffsetCommitCallback commitCallback;
private boolean syncCommits;
private boolean ackOnError;
private Long idleEventInterval;
private String groupId;
private PlatformTransactionManager transactionManager;
org.springframework.kafka.listener.KafkaMessageListenerContainer
关注点,构造函数
public KafkaMessageListenerContainer(ConsumerFactory consumerFactory, ContainerProperties containerProperties) {
this(consumerFactory, containerProperties, (TopicPartitionInitialOffset[])null);
}
略。。。。看不懂了。。