|
|
|
@ -81,14 +81,24 @@ public class TbKafkaConsumerTemplate<T extends TbQueueMsg> implements TbQueueCon |
|
|
|
|
|
|
|
@Override |
|
|
|
public void subscribe() { |
|
|
|
partitions = Collections.singleton(new TopicPartitionInfo(topic, null, null, true)); |
|
|
|
subscribed = false; |
|
|
|
consumerLock.lock(); |
|
|
|
try { |
|
|
|
partitions = Collections.singleton(new TopicPartitionInfo(topic, null, null, true)); |
|
|
|
subscribed = false; |
|
|
|
} finally { |
|
|
|
consumerLock.unlock(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void subscribe(Set<TopicPartitionInfo> partitions) { |
|
|
|
this.partitions = partitions; |
|
|
|
subscribed = false; |
|
|
|
consumerLock.lock(); |
|
|
|
try { |
|
|
|
this.partitions = partitions; |
|
|
|
subscribed = false; |
|
|
|
} finally { |
|
|
|
consumerLock.unlock(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@ -100,13 +110,11 @@ public class TbKafkaConsumerTemplate<T extends TbQueueMsg> implements TbQueueCon |
|
|
|
log.debug("Failed to await subscription", e); |
|
|
|
} |
|
|
|
} else { |
|
|
|
consumerLock.lock(); |
|
|
|
try { |
|
|
|
consumerLock.lock(); |
|
|
|
|
|
|
|
if (!subscribed) { |
|
|
|
List<String> topicNames = partitions.stream().map(TopicPartitionInfo::getFullTopicName).collect(Collectors.toList()); |
|
|
|
topicNames.forEach(admin::createTopicIfNotExists); |
|
|
|
consumer.unsubscribe(); |
|
|
|
consumer.subscribe(topicNames); |
|
|
|
subscribed = true; |
|
|
|
} |
|
|
|
@ -132,8 +140,8 @@ public class TbKafkaConsumerTemplate<T extends TbQueueMsg> implements TbQueueCon |
|
|
|
|
|
|
|
@Override |
|
|
|
public void commit() { |
|
|
|
consumerLock.lock(); |
|
|
|
try { |
|
|
|
consumerLock.lock(); |
|
|
|
consumer.commitAsync(); |
|
|
|
} finally { |
|
|
|
consumerLock.unlock(); |
|
|
|
@ -142,8 +150,8 @@ public class TbKafkaConsumerTemplate<T extends TbQueueMsg> implements TbQueueCon |
|
|
|
|
|
|
|
@Override |
|
|
|
public void unsubscribe() { |
|
|
|
consumerLock.lock(); |
|
|
|
try { |
|
|
|
consumerLock.lock(); |
|
|
|
if (consumer != null) { |
|
|
|
consumer.unsubscribe(); |
|
|
|
consumer.close(); |
|
|
|
|