All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.khasanof.aop.ExecutionExceptionAspect Maven / Gradle / Ivy

There is a newer version: 1.2.2
Show newest version
package org.khasanof.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.khasanof.custom.FluentContext;
import org.khasanof.event.MethodV1Event;
import org.khasanof.event.exceptionDirector.ExceptionDirectorEvent;
import org.khasanof.utils.MethodUtils;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.bots.AbsSender;

import java.lang.reflect.InvocationTargetException;

/**
 * @author Nurislom
 * @see org.khasanof.aop
 * @since 8/13/2023 11:11 PM
 */
@Aspect
@Component
public class ExecutionExceptionAspect {

    private final ApplicationEventPublisher publisher;

    public ExecutionExceptionAspect(ApplicationEventPublisher publisher) {
        this.publisher = publisher;
    }

    @Pointcut("execution(* org.khasanof.executors.execution.Execution.run(..))")
    void executionRunMethodPointcut(){}

    @AfterThrowing(value = "executionRunMethodPointcut()", throwing = "ex")
    private void afterThrowing(JoinPoint joinPoint, Throwable ex) throws Throwable {
        FluentContext.updateExecutorBoolean.set(true);
        if (ex.getClass().equals(InvocationTargetException.class)) {
            MethodV1Event event = MethodUtils.getArg(joinPoint.getArgs(), MethodV1Event.class);
            Object[] args = event.getInvokerModel().getArgs();
            AbsSender sender = MethodUtils.getArg(args, AbsSender.class);
            Update update = MethodUtils.getArg(args, Update.class);
            publisher.publishEvent(new ExceptionDirectorEvent(this, update, sender, ex.getCause()));
        } else {
            throw ex;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy