-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Publish events after Consumer subscribes and after first polling (#677)
- Loading branch information
1 parent
dd59fed
commit 1841b6d
Showing
6 changed files
with
212 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...a/src/main/java/io/micronaut/configuration/kafka/event/AbstractKafkaApplicationEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2017-2023 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.configuration.kafka.event; | ||
|
||
import io.micronaut.context.event.ApplicationEvent; | ||
|
||
/** | ||
* Abstract base class for all kafka application events. | ||
* | ||
* @author Jorge F. Sanchez | ||
* @since 5.1.0 | ||
* @param <T> the {@link ApplicationEvent} source object type | ||
*/ | ||
public class AbstractKafkaApplicationEvent<T> extends ApplicationEvent { | ||
/** | ||
* Constructs a prototypical Event. | ||
* | ||
* @param source The object on which the Event initially occurred. | ||
* @throws IllegalArgumentException if source is null. | ||
*/ | ||
public AbstractKafkaApplicationEvent(T source) { | ||
super(source); | ||
} | ||
|
||
@Override | ||
public T getSource() { | ||
return (T) super.getSource(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...rc/main/java/io/micronaut/configuration/kafka/event/KafkaConsumerStartedPollingEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2017-2023 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.configuration.kafka.event; | ||
|
||
import org.apache.kafka.clients.consumer.Consumer; | ||
|
||
/** | ||
* An event fired after a Kafka {@link Consumer} executes the first polling. | ||
* | ||
* @author Jorge F. Sanchez | ||
* @since 5.1.0 | ||
*/ | ||
public final class KafkaConsumerStartedPollingEvent extends AbstractKafkaApplicationEvent<Consumer> { | ||
/** | ||
* Constructs an event with a given Consumer source. | ||
* | ||
* @param consumer The Consumer on which the Event initially occurred. | ||
* @throws IllegalArgumentException if source is null. | ||
*/ | ||
public KafkaConsumerStartedPollingEvent(Consumer consumer) { | ||
super(consumer); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
kafka/src/main/java/io/micronaut/configuration/kafka/event/KafkaConsumerSubscribedEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2017-2023 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.configuration.kafka.event; | ||
|
||
import org.apache.kafka.clients.consumer.Consumer; | ||
|
||
/** | ||
* An event fired after a Kafka {@link Consumer} subscribes to a set of Kafka topics. | ||
* | ||
* @author Jorge F. Sanchez | ||
* @since 5.1.0 | ||
*/ | ||
public final class KafkaConsumerSubscribedEvent extends AbstractKafkaApplicationEvent<Consumer> { | ||
/** | ||
* Constructs an event with a given Consumer source. | ||
* | ||
* @param consumer The Consumer on which the Event initially occurred. | ||
* @throws IllegalArgumentException if source is null. | ||
*/ | ||
public KafkaConsumerSubscribedEvent(Consumer consumer) { | ||
super(consumer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
kafka/src/test/groovy/io/micronaut/configuration/kafka/event/KafkaConsumerEventSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package io.micronaut.configuration.kafka.event | ||
|
||
import io.micronaut.configuration.kafka.annotation.KafkaListener | ||
import io.micronaut.configuration.kafka.annotation.Topic | ||
import io.micronaut.context.annotation.Property | ||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.context.event.ApplicationEventListener | ||
import io.micronaut.test.extensions.spock.annotation.MicronautTest | ||
import jakarta.inject.Inject | ||
import jakarta.inject.Singleton | ||
import org.apache.kafka.clients.consumer.KafkaConsumer | ||
import spock.lang.Specification | ||
import spock.util.concurrent.PollingConditions | ||
|
||
import static org.apache.kafka.clients.consumer.internals.SubscriptionState.FetchStates.FETCHING | ||
|
||
@Property(name = "spec.name", value = "KafkaConsumerEventSpec") | ||
@MicronautTest(startApplication = false) | ||
class KafkaConsumerEventSpec extends Specification { | ||
|
||
@Inject | ||
KafkaConsumerSubscribedEventListener subscribedEventListener | ||
|
||
@Inject | ||
KafkaConsumerStartedPollingEventListener startedPollingEvent | ||
|
||
void "listen to kafka consumer subscribed events"() { | ||
expect: "the event is emitted and consumed by the event listener" | ||
new PollingConditions(timeout: 10, delay: 1).eventually { | ||
subscribedEventListener.received instanceof KafkaConsumerSubscribedEvent | ||
} | ||
and: "the kafka consumer is subscribed to the expected topic" | ||
subscribedEventListener.consumer.subscriptions.subscription == ['my-nifty-topic'] as Set | ||
} | ||
|
||
void "listen to kafka consumer started polling events"() { | ||
expect: "the event is emitted and consumed by the event listener" | ||
new PollingConditions(timeout: 10, delay: 1).eventually { | ||
startedPollingEvent.received instanceof KafkaConsumerStartedPollingEvent | ||
} | ||
and: "the kafka consumer starts fetching records" | ||
new PollingConditions(timeout: 10, delay: 1).eventually { | ||
subscribedEventListener.consumer.subscriptions.assignment.partitionStateValues()[0].fetchState == FETCHING | ||
} | ||
} | ||
|
||
@KafkaListener(clientId = "my-nifty-kafka-consumer") | ||
@Requires(property = "spec.name", value = "KafkaConsumerEventSpec") | ||
static class MyKafkaConsumer { | ||
@Topic("my-nifty-topic") | ||
void consume(String event) {} | ||
} | ||
|
||
static class AbstractKafkaConsumerEventListener<T extends AbstractKafkaApplicationEvent> implements ApplicationEventListener<T> { | ||
AbstractKafkaApplicationEvent received | ||
KafkaConsumer consumer | ||
|
||
@Override | ||
void onApplicationEvent(T event) { | ||
// Skip consumers unrelated to this test | ||
if ((event.source as KafkaConsumer).clientId.startsWith('my-nifty-kafka-consumer')) { | ||
if (received != null) throw new RuntimeException("Expecting one event only") | ||
received = event | ||
consumer = event.source | ||
} | ||
} | ||
} | ||
|
||
@Singleton | ||
@Requires(property = "spec.name", value = "KafkaConsumerEventSpec") | ||
static class KafkaConsumerSubscribedEventListener extends AbstractKafkaConsumerEventListener<KafkaConsumerSubscribedEvent> { } | ||
|
||
@Singleton | ||
@Requires(property = "spec.name", value = "KafkaConsumerEventSpec") | ||
static class KafkaConsumerStartedPollingEventListener extends AbstractKafkaConsumerEventListener<KafkaConsumerStartedPollingEvent> { } | ||
} |