Spring boot Spring Webflux(Mono/Flux),AOP在拦截时触发REST调用并使用Mono/Flux

Spring boot Spring Webflux(Mono/Flux),AOP在拦截时触发REST调用并使用Mono/Flux,spring-boot,spring-aop,spring-webflux,Spring Boot,Spring Aop,Spring Webflux,我已经写了一个@Aspect来截获以Mono/Flux返回值的被动方法。使用@AfterReturning advice,我试图通过调用Web服务来触发APNS通知 不幸的是,processNotification Mono服务在不执行调用链的情况下立即返回onComplete信号。下面是我的示例程序 @方面 @组成部分 @Slf4j 公共类通知方面{ 私人最终通知服务助手通知服务助手; @自动连线 公共NotifyAspect(NotificationServiceHelper Notific

我已经写了一个@Aspect来截获以Mono/Flux返回值的被动方法。使用@AfterReturning advice,我试图通过调用Web服务来触发APNS通知

不幸的是,processNotification Mono服务在不执行调用链的情况下立即返回onComplete信号。下面是我的示例程序

@方面
@组成部分
@Slf4j
公共类通知方面{
私人最终通知服务助手通知服务助手;
@自动连线
公共NotifyAspect(NotificationServiceHelper NotificationServiceHelper){
this.notificationServiceHelper=notificationServiceHelper;
}
@返回后(切入点=“@annotation(com.cupid9.api.common.annotations.Notify)”,返回=“returnValue”)
public void generateNotification(JoinPoint、JoinPoint、Object returnValue)抛出Throwable{
log.info(“返回通知后-拦截方法:{}”,joinPoint.getSignature().getName());
//获取截获的方法详细信息。
MethodSignature=(MethodSignature)joinPoint.getSignature();
Method=signature.getMethod();
//获取通知详细信息。
Notify myNotify=method.getAnnotation(Notify.class);
if(Mono.class.isAssignableFrom(returnValue.getClass())){
单结果=(单)返回值;
结果:doOnSubscribe(o->{
调试(“订阅时…”);
notificationServiceHelper.processNotification(myNotify.notificationType())
.doon错误(可丢弃->{
log.error(“通知处理器中的异常”,可丢弃);
});
});
}
}
}
@Slf4j
@服务
公共类NotificationServiceHelper{
私有ReactiveUserProfileRepository userProfileRepository;
@值(${services.notification.url}”)
私有字符串notificationServiceUrl;
私人RestWebClient RestWebClient;
@自动连线
public NotificationServiceHelper(RestWebClient RestWebClient,
ReactiveUserProfileRepository ReactiveUserProfileRepository){
this.restWebClient=restWebClient;
this.userProfileRepository=reactiveUserProfileRepository;
}
公共流量处理通知(NotificationSchema.NotificationType NotificationType){
/*获取用户配置文件详细信息*/
返回SessionHelper.getProfileId()
.switchIfEmpty(Mono.error(新的BadRequest(“无效的帐户1!”))
.flatMap(profileId->
Mono.zip(userProfileRepository.findByIdAndStatus(profileId,Status.Active),SessionHelper.getJwtToken())
.switchIfEmpty(Mono.error(新的BadRequest(“无效的帐户2!”))
.flatMapMany(tuple2->{
//获取用户详细信息并确保有一些有效的关联设备。
var userProfileSchema=tuple2.getT1();
info(“处理用户配置文件的通知:{}”,userProfileSchema.getId());
if(Objects.isNull(userProfileSchema.getDevices())| |(userProfileSchema.getDevices().size()<1)){
返回Flux.error(new InternalServerError(“没有与此用户关联的设备。无法发送通知”);
}
//从通知类型生成通知消息
var notificationsMap=new LinkedHashSet();
userProfileSchema.getDevices().forEach(设备->{
var notificationPayload=Notification.builder()
.notificationType(notificationType)
.接收装置(装置)
.receiverProfileRef(userProfileSchema.getId())
.build();
notificationsMap.add(notificationPayload);
});
//获取用于授权的会话令牌
var jwtToken=tuple2.getT2();
//构建进行rest调用所需的URI。
var uri=UriComponentsBuilder.FromUristing(notificationServiceUrl.build().Tori();
log.info(“URI构建字符串:{}”,URI.toString());
//构建进行rest调用所需的标头。
var headers=新的HttpHeaders();
headers.add(HttpHeaders.ACCEPT,MediaType.APPLICATION\u JSON\u VALUE);
headers.add(HttpHeaders.CONTENT\u TYPE、MediaType.APPLICATION\u JSON\u VALUE);
headers.add(HttpHeaders.AUTHORIZATION,jwtToken);
var publishers=new ArrayList();
notificationsMap.forEach(通知->{
add(restWebClient.post(uri、标题、通知));
});
返回Flux.merge(publisher).flatMap(clientResponse->{
var httpStatus=clientResponse.statusCode();
log.info(“NotificationService HTTP状态代码:{}”,httpStatus.value());
if(httpStatus.is2xxsuccesful()){
log.info(“成功接收到来自通知服务的响应…”);
返回clientResponse.bodyToMono(Notification.class);
}否则{
//返回通量.empty();
返回clientResponse.bodyToMono(Error.class)
.flatMap(错误->{
error(“调用通知服务时出错:{}”,httpStatus.getReasonPhrase());
if(httpStatus.value()=400){
返回Mono.error(ne
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Log {

     public String title() default "";

}


@SuppressWarnings({"unchecked"})
@Around("@annotation(operlog)")
public Mono<Result> doAround(ProceedingJoinPoint joinPoint, Log operlog) {
    Mono<Result> mono;

    try {
        mono = (Mono<Result>) joinPoint.proceed();
    } catch (Throwable throwable) {
        throw new RuntimeException(throwable);
    }

    return mono.doOnNext(result -> {
                //doSomething(result);
            };

}