-
Notifications
You must be signed in to change notification settings - Fork 1.6k
spring-projectsGH-3807: Necessity of KafkaHandler on single method class #3865
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
base: main
Are you sure you want to change the base?
spring-projectsGH-3807: Necessity of KafkaHandler on single method class #3865
Conversation
80ece2c
to
255ead3
Compare
Signed-off-by: chickenchickenlove <[email protected]>
255ead3
to
9511818
Compare
spring-kafka/src/test/java/org/springframework/kafka/annotation/KafkaListenerTests.java
Outdated
Show resolved
Hide resolved
spring-kafka/src/test/java/org/springframework/kafka/annotation/KafkaListenerTests.java
Outdated
Show resolved
Hide resolved
spring-kafka/src/test/java/org/springframework/kafka/annotation/KafkaListenerTests.java
Show resolved
Hide resolved
spring-kafka/src/test/java/org/springframework/kafka/annotation/AliasPropertiesTests.java
Show resolved
Hide resolved
spring-kafka/src/main/java/org/springframework/kafka/annotation/KafkaHandlerParser.java
Outdated
Show resolved
Hide resolved
Signed-off-by: chickenchickenlove <[email protected]>
ae7a406
to
13ee0d1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is very close to the merge phase.
Please, consider to mention this feature in the class-level-kafkalistener.adoc
and whats-new.adoc
.
Thanks
...main/java/org/springframework/kafka/annotation/KafkaListenerAnnotationBeanPostProcessor.java
Outdated
Show resolved
Hide resolved
spring-kafka/src/test/java/org/springframework/kafka/annotation/KafkaListenerTests.java
Outdated
Show resolved
Hide resolved
spring-kafka/src/test/java/org/springframework/kafka/annotation/KafkaListenerTests.java
Outdated
Show resolved
Hide resolved
spring-kafka/src/test/java/org/springframework/kafka/annotation/KafkaListenerTests.java
Outdated
Show resolved
Hide resolved
assertThat(this.otherClassLevelListenerWithSinglePublicMethod.latch.await(10, TimeUnit.SECONDS)).isTrue(); | ||
assertThat(this.classLevelListenerWithSinglePublicMethodAndPrivateMethod.latch.await(10, TimeUnit.SECONDS)).isTrue(); | ||
|
||
assertThat(this.classLevelListenerWithDoublePublicMethod.latch.await(10, TimeUnit.SECONDS)).isFalse(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not OK.
I'm really against any blocking in the tests for nothing.
I mean this always going to add up 10 seconds to the whole test suite execution.
Now imaging we have like 3000 tests in the project and every one would contribute some delay.
I believe the configuration when we have ambiguity with several public methods has to be reject with an IllegalStateException
.
No silent ignoring to make am impression that my configuration is OK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@artembilan thanks for your comments. 🙇♂️
This is not OK.
I'm really against any blocking in the tests for nothing.
I mean this always going to add up 10 seconds to the whole test suite execution.
Now imaging we have like 3000 tests in the project and every one would contribute some delay.
I agree.
I will remove this case from test codes.
I believe the configuration when we have ambiguity with several public methods has to be reject with an IllegalStateException.
I understand your point clearly.
However, The important thing is that the changes I made in this PR are not related to the case where there are multiple public methods.
So we don't need to consider ambiguous conditions because in that case any public method will not be registered as kafka listeners.
if (hasClassLevelListeners) {
if (methodsWithHandler.isEmpty() && publicMethods.size() == 1 && !hasMethodLevelListeners) {
// added this time.
}
else {
// original
}
}
I think we would be better not to throw an error if there are several public methods when there is no @KafkaHandler
method.
This is because after registering the class as a bean using @KafkaListener
, there might be cases where other beans inject it and call its public methods. For example, getter
methods.
If there are already users using it that way, it would be difficult to ensure backward compatibility.
@KafkaListener
class MyTakser {
// This method is for @KafkaHandler implicitly.
public void listen (String message) {
// do something with kafka messages.
}
// This method is not for @KafkaHandler.
public void getCurrentState() {
// return current state
}
...
}
IMHO, If we should something to do for this, I think it would be better to notify by using warning log.
For example,
The bean {BEAN_NAME} which has @KafkaListener is registered without a proper listener method.
WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I see the picture better now. What if our class is extended and new methods appear there? Which one to choose from hierarchy then?
I am sorry to say that , but I feel more like better to leave it as is and rely only on the @KafkaHandler
presence .
The issue simply could be turned into docs explaining why we require this or other annotation. We really cannot predict what purpose of the method is, so being explicit is much better, than trying to come up with other restrictions. I have totally ignored the fact that class could be extended 🤦.
And between us: I’m not a fun of annotations in Java. 😉
Still, that is my opinion on the problem of this issue, so I’m opened for other arguments to continue your effort with the code.
CC @sobychacko
Signed-off-by: chickenchickenlove <[email protected]>
Fixes: #3807
Issue link: #3807
From now on, the single public method without method-level
@KafkaHandler
,@KafkaListener
in class which has class-level@KafkaListener
will be registered asMethodKafkaListenerEndpoint
so that it consumes messages.