Skip to content

Commit b17a045

Browse files
antonio-tomacartembilan
authored andcommitted
GH-2583: Make PF.createProducer argument Nullable
Resolves #2583 `ProducerFactory.createProducer` argument `txIdPrefix` is non-nullable which causes Kotlin compiler to emit warning if implementations declare `txIdPrefix` as nullable `txIdPrefix: String?` * Add `@Nullable` for `ProducerFactory.createProducer()` `txIdPrefix` argument * polish code: correct `@since` and new line at end **Cherry-pick to `2.9.x`** # Conflicts: # spring-kafka/src/main/java/org/springframework/kafka/core/ProducerFactory.java
1 parent dc026c7 commit b17a045

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

spring-kafka/src/main/java/org/springframework/kafka/core/ProducerFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 the original author or authors.
2+
* Copyright 2016-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -56,7 +56,7 @@ public interface ProducerFactory<K, V> {
5656
* @return the producer.
5757
* @since 2.3
5858
*/
59-
default Producer<K, V> createProducer(@SuppressWarnings("unused") String txIdPrefix) {
59+
default Producer<K, V> createProducer(@Nullable @SuppressWarnings("unused") String txIdPrefix) {
6060
throw new UnsupportedOperationException("This factory does not support this method");
6161
}
6262

spring-kafka/src/test/java/org/springframework/kafka/core/KafkaTemplateTransactionTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import org.springframework.kafka.test.context.EmbeddedKafka;
7474
import org.springframework.kafka.test.utils.KafkaTestUtils;
7575
import org.springframework.kafka.transaction.KafkaTransactionManager;
76+
import org.springframework.lang.Nullable;
7677
import org.springframework.transaction.TransactionDefinition;
7778
import org.springframework.transaction.TransactionException;
7879
import org.springframework.transaction.annotation.EnableTransactionManagement;
@@ -388,7 +389,7 @@ public void testQuickCloseAfterCommitTimeout() {
388389

389390
@SuppressWarnings({ "rawtypes", "unchecked" })
390391
@Override
391-
public Producer<String, String> createProducer(String txIdPrefixArg) {
392+
public Producer<String, String> createProducer(@Nullable String txIdPrefixArg) {
392393
CloseSafeProducer<String, String> closeSafeProducer = new CloseSafeProducer<>(producer,
393394
(prod, timeout) -> {
394395
prod.closeDelegate(timeout, Collections.emptyList());
@@ -424,7 +425,7 @@ void testNormalCloseAfterCommitCacheFull() {
424425

425426
@SuppressWarnings("unchecked")
426427
@Override
427-
public Producer<String, String> createProducer(String txIdPrefixArg) {
428+
public Producer<String, String> createProducer(@Nullable String txIdPrefixArg) {
428429
BlockingQueue<CloseSafeProducer<String, String>> cache = new LinkedBlockingDeque<>(1);
429430
try {
430431
cache.put(new CloseSafeProducer<>(mock(Producer.class), this::removeProducer,
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2016-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.kafka.core
18+
19+
import org.apache.kafka.clients.producer.MockProducer
20+
import org.apache.kafka.clients.producer.Producer
21+
import org.assertj.core.api.Assertions.assertThat
22+
import org.junit.jupiter.api.Test
23+
24+
/**
25+
* @author Antonio Tomac
26+
* @since 2.9.6
27+
* @see [ProducerFactory.createProducer]
28+
*/
29+
class KotlinProducerFactoryTests {
30+
31+
class KotlinProducerFactory : ProducerFactory<Any, Any> {
32+
override fun createProducer(): Producer<Any, Any> = MockProducer()
33+
34+
/**
35+
* This override checks correct nullability of [txIdPrefix] in [ProducerFactory.createProducer]
36+
* Reason, kotlin compiler warning:
37+
*
38+
* Override 'fun createProducer(txIdPrefix: String?): Producer<Any, Any>' has incorrect nullability
39+
* in its signature comparing with overridden 'fun createProducer(txIdPrefix: String): Producer<Any!, Any!>'
40+
* This warning will become an error soon. See [KT-36770](https://youtrack.jetbrains.com/issue/KT-36770) for details
41+
*/
42+
override fun createProducer(txIdPrefix: String?): Producer<Any, Any> = MockProducer()
43+
}
44+
45+
private val producerFactory: ProducerFactory<Any, Any> = KotlinProducerFactory()
46+
47+
@Test
48+
fun `test instantiation with null txIdPrefix`() {
49+
val producer = producerFactory.createProducer(null)
50+
assertThat(producer).isInstanceOf(MockProducer::class.java)
51+
}
52+
53+
@Test
54+
fun `test instantiation with non-null txIdPrefix`() {
55+
val producer = producerFactory.createProducer("foo")
56+
assertThat(producer).isInstanceOf(MockProducer::class.java)
57+
}
58+
}

0 commit comments

Comments
 (0)