Skip to content
This repository has been archived by the owner on Jul 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #9 from ralf-ueberfuhr-ars/feature/reactive-interc…
Browse files Browse the repository at this point in the history
…eptor

Add reactive interceptor
  • Loading branch information
ralf-ueberfuhr-ars authored Jun 26, 2024
2 parents 958e975 + 02d39a3 commit cd72624
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import de.sample.schulung.accounts.consumer.domain.client.CustomerDto;
import de.sample.schulung.accounts.consumer.domain.client.CustomersApi;
import de.sample.schulung.accounts.consumer.shared.interceptors.LogPerformance;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
Expand All @@ -12,8 +13,10 @@ public class CustomersService {

private final CustomersApi remoteApi;

@LogPerformance
public Flux<CustomerDto> findAll() {
return remoteApi.readAllCustomers();
return remoteApi
.readAllCustomers();
}

}
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 {
}
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
);
}
}

0 comments on commit cd72624

Please sign in to comment.