This repository has been archived by the owner on Jul 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce @KafkaProducer interceptor.
- Loading branch information
Ralf Ueberfuhr
committed
Jun 27, 2024
1 parent
f548945
commit ab3c97f
Showing
4 changed files
with
114 additions
and
23 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
31 changes: 31 additions & 0 deletions
31
...e-provider/src/main/java/de/sample/schulung/accounts/kafka/interceptor/KafkaProducer.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,31 @@ | ||
package de.sample.schulung.accounts.kafka.interceptor; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* Annotate a method to get the return value | ||
* sent to a Kafka topic. The method can return a simple object that is then sent as a value | ||
* without a key, or it can return an instance of {@link KafkaRecord}. | ||
* If the method returns <tt>null</tt> (or has a void return type), no message is produced. | ||
*/ | ||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Inherited | ||
@Documented | ||
public @interface KafkaProducer { | ||
|
||
/** | ||
* The name of the topic. | ||
* | ||
* @return the name of the topic | ||
*/ | ||
String topic(); | ||
|
||
/** | ||
* The partition. Leave empty, if the Partitioner should do the job. | ||
* | ||
* @return the partition | ||
*/ | ||
int partition() default -1; | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
...src/main/java/de/sample/schulung/accounts/kafka/interceptor/KafkaProducerInterceptor.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,57 @@ | ||
package de.sample.schulung.accounts.kafka.interceptor; | ||
|
||
import org.aopalliance.intercept.MethodInterceptor; | ||
import org.springframework.aop.Pointcut; | ||
import org.springframework.aop.framework.autoproxy.AbstractBeanFactoryAwareAdvisingPostProcessor; | ||
import org.springframework.aop.support.DefaultPointcutAdvisor; | ||
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut; | ||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.core.annotation.AnnotationUtils; | ||
import org.springframework.kafka.core.KafkaTemplate; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@SuppressWarnings("unused") | ||
public class KafkaProducerInterceptor | ||
extends AbstractBeanFactoryAwareAdvisingPostProcessor | ||
implements InitializingBean { | ||
|
||
private final MethodInterceptor advice; | ||
|
||
public KafkaProducerInterceptor(final KafkaTemplate<Object, Object> kafkaTemplate) { | ||
this.advice = invocation -> { | ||
final var result = invocation.proceed(); | ||
if (result == null) { | ||
return null; | ||
} | ||
final var annotation = AnnotationUtils.findAnnotation(invocation.getMethod(), KafkaProducer.class); | ||
if (annotation != null) { | ||
final Object key; | ||
final Object value; | ||
if (result instanceof KafkaRecord<?, ?> r) { | ||
key = r.key(); | ||
value = r.value(); | ||
} else { | ||
key = null; | ||
value = result; | ||
} | ||
//noinspection DataFlowIssue | ||
kafkaTemplate.send( | ||
annotation.topic(), | ||
annotation.partition() < 0 ? null : annotation.partition(), | ||
key, | ||
value | ||
); | ||
} | ||
return result; | ||
}; | ||
} | ||
|
||
@Override | ||
public void afterPropertiesSet() { | ||
Pointcut pointcut = new AnnotationMatchingPointcut(null, KafkaProducer.class, true); | ||
this.advisor = new DefaultPointcutAdvisor(pointcut, advice); | ||
} | ||
|
||
|
||
} |
12 changes: 12 additions & 0 deletions
12
...ice-provider/src/main/java/de/sample/schulung/accounts/kafka/interceptor/KafkaRecord.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,12 @@ | ||
package de.sample.schulung.accounts.kafka.interceptor; | ||
|
||
/** | ||
* Return a KafkaRecord from a {@link KafkaProducer} method | ||
* to get a key and a value sent. | ||
*/ | ||
public record KafkaRecord<K, V>( | ||
K key, | ||
V value | ||
) { | ||
|
||
} |