Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Honor transactionalId when a Kafka producer bean is injected #865

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,12 @@ public <K, V> Producer<K, V> getProducer(
properties.put(ProducerConfig.ACKS_CONFIG, acksValue);
}

return getKafkaProducer(id, null, k, v, false, properties);
final String transactionalId = annotationMetadata.stringValue(KafkaClient.class, "transactionalId")
guillermocalvo marked this conversation as resolved.
Show resolved Hide resolved
.filter(StringUtils::isNotEmpty)
.orElse(null);
final boolean transactional = transactionalId != null;

return getKafkaProducer(id, transactionalId, k, v, transactional, properties);
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import io.micronaut.configuration.kafka.annotation.Topic
import io.micronaut.context.annotation.Requires
import io.micronaut.core.type.Argument
import io.micronaut.messaging.annotation.SendTo
import jakarta.inject.Singleton
import org.apache.kafka.clients.producer.Producer
import org.apache.kafka.clients.producer.ProducerRecord
import org.apache.kafka.common.serialization.IntegerSerializer
import org.apache.kafka.common.serialization.StringSerializer
Expand Down Expand Up @@ -110,6 +112,30 @@ class KafkaTxSpec extends AbstractKafkaContainerSpec {
}
}

void "should inject a transactional producer"() {
given:
Producer<String, String> producer = context.getBean(MyService).transactionalProducer

when:
producer.beginTransaction()
producer.abortTransaction()

then:
noExceptionThrown()
}

void "should inject a non-transactional producer"() {
given:
Producer<String, String> producer = context.getBean(MyService).nonTransactionalProducer

when:
producer.beginTransaction()

then:
IllegalStateException e = thrown()
e.message == 'Transactional method invoked on a non-transactional producer.'
}

@Requires(property = 'spec.name', value = 'KafkaTxSpec')
@KafkaListener(
isolation = READ_COMMITTED,
Expand Down Expand Up @@ -226,4 +252,17 @@ class KafkaTxSpec extends AbstractKafkaContainerSpec {
@Topic("tx-strings")
void send(String strings)
}

@Singleton
@Requires(property = 'spec.name', value = 'KafkaTxSpec')
static class MyService {
final Producer<String, String> transactionalProducer
final Producer<String, String> nonTransactionalProducer
MyService(
@KafkaClient(transactionalId = "tx-string-producer") Producer<String, String> transactionalProducer,
@KafkaClient Producer<String, String> nonTransactionalProducer) {
this.transactionalProducer = transactionalProducer
this.nonTransactionalProducer = nonTransactionalProducer
}
}
}