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 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from ralf-ueberfuhr-ars/feature/reactive-interc…
…eptor Add reactive interceptor
- Loading branch information
Showing
3 changed files
with
76 additions
and
1 deletion.
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
10 changes: 10 additions & 0 deletions
10
...rc/main/java/de/sample/schulung/accounts/consumer/shared/interceptors/LogPerformance.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,10 @@ | ||
package de.sample.schulung.accounts.consumer.shared.interceptors; | ||
|
||
import java.lang.annotation.*; | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@Inherited | ||
public @interface LogPerformance { | ||
} |
62 changes: 62 additions & 0 deletions
62
...a/de/sample/schulung/accounts/consumer/shared/interceptors/LogPerformanceInterceptor.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,62 @@ | ||
package de.sample.schulung.accounts.consumer.shared.interceptors; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.aopalliance.intercept.MethodInterceptor; | ||
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.stereotype.Component; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.util.function.Consumer; | ||
|
||
@Component | ||
@Slf4j | ||
public class LogPerformanceInterceptor | ||
extends AbstractBeanFactoryAwareAdvisingPostProcessor | ||
implements InitializingBean { | ||
|
||
private final MethodInterceptor interceptor = invocation -> { | ||
final Consumer<Long> logging = startTime -> { | ||
long ts2 = System.currentTimeMillis(); | ||
log.info( | ||
"Methode {} brauchte {}ms", | ||
invocation.getMethod().getName(), | ||
ts2 - startTime | ||
); | ||
}; | ||
long ts = System.currentTimeMillis(); | ||
boolean isSync = false; | ||
try { | ||
Object result = invocation.proceed(); | ||
if (result instanceof Flux<?> flux) { | ||
return flux.doFinally(__ -> logging.accept(ts)); | ||
} | ||
if(result instanceof Mono<?> mono) { | ||
return mono.doFinally(__ -> logging.accept(ts)); | ||
} | ||
isSync = true; | ||
return result; | ||
} finally { | ||
if (isSync) { | ||
logging.accept(ts); | ||
} | ||
} | ||
}; | ||
|
||
// wird automatisch nach Dep. Inj. aufgerufen | ||
@Override | ||
public void afterPropertiesSet() throws Exception { | ||
var pointcut = new AnnotationMatchingPointcut( | ||
null, | ||
LogPerformance.class, | ||
true | ||
); | ||
this.advisor = new DefaultPointcutAdvisor( | ||
pointcut, | ||
interceptor | ||
); | ||
} | ||
} |