Skip to content

Commit a32b3b9

Browse files
artembilangaryrussell
authored andcommitted
Fix new Sonar smells
1 parent 5e002b5 commit a32b3b9

File tree

1 file changed

+43
-31
lines changed

1 file changed

+43
-31
lines changed

spring-kafka/src/main/java/org/springframework/kafka/listener/KafkaMessageListenerContainer.java

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -540,8 +540,6 @@ private final class ListenerConsumer implements SchedulingAwareRunnable, Consume
540540

541541
private int nackIndex;
542542

543-
private Object sample;
544-
545543
private volatile boolean consumerPaused;
546544

547545
private volatile long lastPoll = System.currentTimeMillis();
@@ -618,17 +616,7 @@ else if (listener instanceof MessageListener) {
618616
this.containerProperties.setSyncCommitTimeout(this.syncCommitTimeout);
619617
}
620618
this.maxPollInterval = obtainMaxPollInterval(consumerProperties);
621-
MicrometerHolder holder = null;
622-
try {
623-
if (MICROMETER_PRESENT && this.containerProperties.isMicrometerEnabled()) {
624-
holder = new MicrometerHolder(getApplicationContext(), getBeanName(),
625-
this.containerProperties.getMicrometerTags());
626-
}
627-
}
628-
catch (@SuppressWarnings("unused") IllegalStateException ex) {
629-
// NOSONAR - no micrometer or meter registry
630-
}
631-
this.micrometerHolder = holder;
619+
this.micrometerHolder = obtainMicrometerHolder();
632620
}
633621

634622
private long obtainMaxPollInterval(Properties consumerProperties) {
@@ -793,6 +781,21 @@ protected ErrorHandler determineErrorHandler(GenericErrorHandler<?> errHandler)
793781
: this.transactionManager != null ? null : new LoggingErrorHandler();
794782
}
795783

784+
@Nullable
785+
private MicrometerHolder obtainMicrometerHolder() {
786+
MicrometerHolder holder = null;
787+
try {
788+
if (MICROMETER_PRESENT && this.containerProperties.isMicrometerEnabled()) {
789+
holder = new MicrometerHolder(getApplicationContext(), getBeanName(),
790+
this.containerProperties.getMicrometerTags());
791+
}
792+
}
793+
catch (@SuppressWarnings("unused") IllegalStateException ex) {
794+
// NOSONAR - no micrometer or meter registry
795+
}
796+
return holder;
797+
}
798+
796799
private void seekPartitions(Collection<TopicPartition> partitions, boolean idle) {
797800
this.consumerSeekAwareListener.registerSeekCallback(this);
798801
Map<TopicPartition, Long> current = new HashMap<>();
@@ -1196,19 +1199,13 @@ private List<ConsumerRecord<K, V>> createRecordList(final ConsumerRecords<K, V>
11961199
private RuntimeException doInvokeBatchListener(final ConsumerRecords<K, V> records,
11971200
List<ConsumerRecord<K, V>> recordList, @SuppressWarnings(RAW_TYPES) Producer producer) {
11981201

1202+
Object sample = startMicrometerSample();
11991203
try {
1200-
if (this.micrometerHolder != null) {
1201-
this.sample = this.micrometerHolder.start();
1202-
}
12031204
invokeBatchOnMessage(records, recordList, producer);
1204-
if (this.sample != null) {
1205-
this.micrometerHolder.success(this.sample);
1206-
}
1205+
successTimer(sample);
12071206
}
12081207
catch (RuntimeException e) {
1209-
if (this.sample != null) {
1210-
this.micrometerHolder.failure(this.sample);
1211-
}
1208+
failureTimer(sample);
12121209
if (this.containerProperties.isAckOnError() && !this.autoCommit && producer == null) {
12131210
this.acks.addAll(getHighestOffsetRecords(records));
12141211
}
@@ -1233,6 +1230,26 @@ private RuntimeException doInvokeBatchListener(final ConsumerRecords<K, V> recor
12331230
return null;
12341231
}
12351232

1233+
@Nullable
1234+
private Object startMicrometerSample() {
1235+
if (this.micrometerHolder != null) {
1236+
return this.micrometerHolder.start();
1237+
}
1238+
return null;
1239+
}
1240+
1241+
private void successTimer(@Nullable Object sample) {
1242+
if (sample != null) {
1243+
this.micrometerHolder.success(sample);
1244+
}
1245+
}
1246+
1247+
private void failureTimer(@Nullable Object sample) {
1248+
if (sample != null) {
1249+
this.micrometerHolder.failure(sample);
1250+
}
1251+
}
1252+
12361253
private void invokeBatchOnMessage(final ConsumerRecords<K, V> records, // NOSONAR - Cyclomatic Complexity
12371254
List<ConsumerRecord<K, V>> recordList, @SuppressWarnings(RAW_TYPES) Producer producer) throws InterruptedException {
12381255

@@ -1453,19 +1470,14 @@ private RuntimeException doInvokeRecordListener(final ConsumerRecord<K, V> recor
14531470
@SuppressWarnings(RAW_TYPES) Producer producer,
14541471
Iterator<ConsumerRecord<K, V>> iterator) {
14551472

1473+
Object sample = startMicrometerSample();
1474+
14561475
try {
1457-
if (this.micrometerHolder != null) {
1458-
this.sample = this.micrometerHolder.start();
1459-
}
14601476
invokeOnMessage(record, producer);
1461-
if (this.sample != null) {
1462-
this.micrometerHolder.success(this.sample);
1463-
}
1477+
successTimer(sample);
14641478
}
14651479
catch (RuntimeException e) {
1466-
if (this.sample != null) {
1467-
this.micrometerHolder.failure(this.sample);
1468-
}
1480+
failureTimer(sample);
14691481
if (this.containerProperties.isAckOnError() && !this.autoCommit && producer == null) {
14701482
ackCurrent(record);
14711483
}

0 commit comments

Comments
 (0)