-
Notifications
You must be signed in to change notification settings - Fork 108
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
Allow multi-threaded access to KafkaConsumer #613
Comments
Able to work around this with the following code, which bypasses MN's @Inject
KafkaConsumerFactory factory;
@Inject
SerdeRegistry registry;
@Inject @Named(TaskExecutors.MESSAGE_CONSUMER)
ExecutorService executor;
@PostConstruct
public void start() {
log.info("Configuration: {} - Factory: {}", configuration, factory);
if (configuration.getKeyDeserializer().isEmpty()) {
configuration.setKeyDeserializer(registry.pickDeserializer(Argument.of(KeyType.class)));
}
if (configuration.getValueDeserializer().isEmpty()) {
configuration.setValueDeserializer(registry.pickDeserializer(Argument.of(ValueType.class)));
}
final var consumer = factory.createConsumer(configuration);
log.info("Consumer: {}", consumer);
executor.submit(() -> ...);
} |
I don't see anywhere in the referenced documentation that says a consumer can be accessed in a multi threaded manner when all access is mutually exclusive. Introducing synchronization and locking will just slow down the implementation and introduce lock contention. |
From the first line of the documentation linked above: It is the responsibility of the user to ensure that multi-threaded access is properly synchronized The Kafka Consumer API ensures that access to a consumer is mutually exclusive by means of checking the ID of the current thread against the one referenced within the consumer and by reference counting (see: |
ok, will take a look |
Feature description
Problem:
A
KafkaConsumer
instance can be safely shared between threads when all access ismutually exclusive
. Improper access results in aConcurrentModificationException
being raised - as per the documentation.Micronaut's current implementation does not ensure that access to a
KafkaConsumer
is mutually exclusive, and as a consequence, downstream code is unable to perform operations with theKafkaConsumer
instance outside of thepoll loop
thread. This is prohibitive as it prevents multi-threaded code from utilising all of the niceties of Micronaut's Kafka implementation and life-cycle management.Feature Request:
To ensure that Micronaut's use of
KafkaConsumer
s is mutually exclusive, achieved by performing Kafka operations within asynchronized
block.Implementation:
KafkaConsumer
instances within theKafkaConsumerProcessor
when performing Kafka operations.ConsumerRegistry#getConsumer
.The text was updated successfully, but these errors were encountered: